Skip to content

Commit 33089ac

Browse files
committed
Pihole: Split auth and status check
1 parent 90a9e76 commit 33089ac

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

modPihole.vb

+22-6
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,9 @@ Module modPihole
4141
Return "Pi-hole module enabled"
4242
End Function
4343

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
4945
Try
46+
Dim PiholeSID As String = ""
5047
My.Application.Log.WriteEntry("Attempting to authenticate to Pi-hole")
5148
Dim PiholeAuthRequest As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create("http://" & My.Settings.Pihole_IPAddress & "/api/auth"), System.Net.HttpWebRequest)
5249
My.Application.Log.WriteEntry(PiholeAuthRequest.Address.ToString)
@@ -56,15 +53,34 @@ Module modPihole
5653
PiholeAuthRequest.ContentLength = PiholeAuthRequestBody.Length
5754
Dim ReqStream As System.IO.Stream = PiholeAuthRequest.GetRequestStream()
5855
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)
6057
Using ResStream As System.IO.Stream = PiholeAuthResponse.GetResponseStream()
6158
Dim Reader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
6259
Dim OutputJson As String = Reader.ReadToEnd()
6360
Using JsonResponse = JsonDocument.Parse(OutputJson)
6461
PiholeSID = JsonResponse.RootElement.GetProperty("session").GetProperty("sid").GetString()
6562
End Using
6663
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 = ""
6778

79+
If PiholeSID = "failed" Then
80+
Return "failed"
81+
End If
82+
83+
Try
6884
My.Application.Log.WriteEntry("Requesting Pi-hole statistics")
6985
Dim PiholeAPIRequest As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create("http://" & My.Settings.Pihole_IPAddress & "/api/dns/blocking?sid=" & PiholeSID), System.Net.HttpWebRequest)
7086
PiholeAPIRequest.Method = "GET"

0 commit comments

Comments
 (0)