@@ -41,12 +41,9 @@ Module modPihole
41
41
Return "Pi-hole module enabled"
42
42
End Function
43
43
44
- Function GetPiholeAPI() As String
45
- Dim PiholeSID As String = ""
46
- Dim PiholeAuthResponse As System.Net.HttpWebResponse
47
- Dim strBlockingStatus As String = ""
48
-
44
+ Function AuthPiholeAPI() As String
49
45
Try
46
+ Dim PiholeSID As String = ""
50
47
My.Application.Log.WriteEntry( "Attempting to authenticate to Pi-hole" )
51
48
Dim PiholeAuthRequest As System.Net.HttpWebRequest = CType (System.Net.WebRequest.Create( "http://" & My.Settings.Pihole_IPAddress & "/api/auth" ), System.Net.HttpWebRequest)
52
49
My.Application.Log.WriteEntry(PiholeAuthRequest.Address.ToString)
@@ -56,15 +53,34 @@ Module modPihole
56
53
PiholeAuthRequest.ContentLength = PiholeAuthRequestBody.Length
57
54
Dim ReqStream As System.IO.Stream = PiholeAuthRequest.GetRequestStream()
58
55
ReqStream.Write(PiholeAuthRequestBody, 0 , PiholeAuthRequestBody.Length)
59
- PiholeAuthResponse = CType (PiholeAuthRequest.GetResponse(), System.Net.HttpWebResponse)
56
+ Dim PiholeAuthResponse As System.Net.HttpWebResponse = CType (PiholeAuthRequest.GetResponse(), System.Net.HttpWebResponse)
60
57
Using ResStream As System.IO.Stream = PiholeAuthResponse.GetResponseStream()
61
58
Dim Reader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
62
59
Dim OutputJson As String = Reader.ReadToEnd()
63
60
Using JsonResponse = JsonDocument.Parse(OutputJson)
64
61
PiholeSID = JsonResponse.RootElement.GetProperty( "session" ).GetProperty( "sid" ).GetString()
65
62
End Using
66
63
End Using
64
+ Return PiholeSID
65
+ Catch WebEx As System.Net.WebException
66
+ Using ResStream As System.IO.Stream = WebEx.Response.GetResponseStream()
67
+ Dim Reader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
68
+ Dim OutputJson As String = Reader.ReadToEnd()
69
+ My.Application.Log.WriteEntry( "Pi-hole Auth Error:" & OutputJson, TraceEventType.Error)
70
+ End Using
71
+ Return "failed"
72
+ End Try
73
+ End Function
74
+
75
+ Function GetPiholeAPI() As String
76
+ Dim PiholeSID As String = AuthPiholeAPI()
77
+ Dim strBlockingStatus As String = ""
67
78
79
+ If PiholeSID = "failed" Then
80
+ Return "failed"
81
+ End If
82
+
83
+ Try
68
84
My.Application.Log.WriteEntry( "Requesting Pi-hole statistics" )
69
85
Dim PiholeAPIRequest As System.Net.HttpWebRequest = CType (System.Net.WebRequest.Create( "http://" & My.Settings.Pihole_IPAddress & "/api/dns/blocking?sid=" & PiholeSID), System.Net.HttpWebRequest)
70
86
PiholeAPIRequest.Method = "GET"
0 commit comments