Skip to content

Commit 4b5a36f

Browse files
committed
Pihole: Turn blocking on and off
1 parent 33089ac commit 4b5a36f

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

modConverse.vb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ Module modConverse
101101
strCommandResponse = modOpenWeatherMap.Disable()
102102
Case "pihole"
103103
strCommandResponse = modPihole.Disable()
104+
Case "piholeblocking"
105+
strCommandResponse = modPihole.SetPiholeBlocking("false")
104106
Case "ping"
105107
strCommandResponse = modPing.Disable()
106108
Case "smartcom"
@@ -157,6 +159,8 @@ Module modConverse
157159
strCommandResponse = modOpenWeatherMap.Enable()
158160
Case "pihole"
159161
strCommandResponse = modPihole.Enable()
162+
Case "piholeblocking"
163+
strCommandResponse = modPihole.SetPiholeBlocking("true")
160164
Case "ping"
161165
strCommandResponse = modPing.Enable()
162166
Case "smartcom"

modPihole.vb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,44 @@ Module modPihole
105105
End Try
106106
End Function
107107

108+
Function SetPiholeBlocking(ByVal strSetBlock As String) As String
109+
Dim PiholeSID As String = AuthPiholeAPI()
110+
Dim strBlockingStatus As String = ""
111+
112+
If PiholeSID = "failed" Then
113+
Return "failed"
114+
End If
115+
116+
Try
117+
My.Application.Log.WriteEntry("Requesting Pi-hole statistics")
118+
Dim PiholeAPIRequest As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create("http://" & My.Settings.Pihole_IPAddress & "/api/dns/blocking?sid=" & PiholeSID), System.Net.HttpWebRequest)
119+
PiholeAPIRequest.Method = "POST"
120+
Dim PiholeAPIRequestBody As Byte() = Text.Encoding.UTF8.GetBytes("{""blocking"":" & strSetBlock & "}")
121+
PiholeAPIRequest.ContentType = "application/json"
122+
PiholeAPIRequest.ContentLength = PiholeAPIRequestBody.Length
123+
Dim ReqStream As System.IO.Stream = PiholeAPIRequest.GetRequestStream()
124+
ReqStream.Write(PiholeAPIRequestBody, 0, PiholeAPIRequestBody.Length)
125+
Dim PiholeAPIResponse As System.Net.HttpWebResponse = CType(PiholeAPIRequest.GetResponse(), System.Net.HttpWebResponse)
126+
Dim PiholeAPIResponseStream As New System.IO.StreamReader(PiholeAPIResponse.GetResponseStream(), System.Text.Encoding.UTF8)
127+
Dim PiholeAPIJSON As String = PiholeAPIResponseStream.ReadToEnd()
128+
PiholeAPIResponse.Close()
129+
PiholeAPIResponseStream.Close()
130+
My.Application.Log.WriteEntry("Response received: " & PiholeAPIJSON, TraceEventType.Verbose)
131+
Using JsonResponse = JsonDocument.Parse(PiholeAPIJSON)
132+
strBlockingStatus = JsonResponse.RootElement.GetProperty("blocking").GetString()
133+
End Using
134+
135+
Return strBlockingStatus
136+
Catch WebEx As System.Net.WebException
137+
Using ResStream As System.IO.Stream = WebEx.Response.GetResponseStream()
138+
Dim Reader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
139+
Dim OutputJson As String = Reader.ReadToEnd()
140+
My.Application.Log.WriteEntry("Pi-hole Request Error:" & OutputJson, TraceEventType.Error)
141+
End Using
142+
Return "failed"
143+
End Try
144+
End Function
145+
108146
Function Load() As String
109147
If My.Settings.Pihole_Enable = True Then
110148
My.Application.Log.WriteEntry("Loading Pi-hole module")

0 commit comments

Comments
 (0)