-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodPihole.vb
168 lines (154 loc) · 8.74 KB
/
modPihole.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
Imports System.Text.Json
Module modPihole
Function CheckPiholeStatus() As String
If My.Settings.Pihole_Enable = True Then
My.Application.Log.WriteEntry("Pinging Pi-hole", TraceEventType.Verbose)
Dim strPingResponse As String = modPing.Ping(My.Settings.Pihole_IPAddress)
If strPingResponse.StartsWith("Reply from") Then
My.Application.Log.WriteEntry(strPingResponse, TraceEventType.Verbose)
Dim PiholeData = GetPiholeAPI()
If PiholeData = "enabled" Then
Return "Pi-hole is enabled"
ElseIf PiholeData = "disabled" Then
Return "Pi-hole is in disabled mode"
Else
Return "Pi-hole is not responding"
End If
ElseIf strPingResponse = "Ping disabled" Then
My.Application.Log.WriteEntry("Could not ping Pi-hole, ping module is disabled", TraceEventType.Warning)
Return "Pi-hole not contacted because ping module is disabled"
Else
My.Application.Log.WriteEntry(strPingResponse, TraceEventType.Warning)
Return "Pi-hole was not reachable via ping"
End If
Else
Return "Pi-hole module is disabled"
End If
End Function
Function Disable() As String
Unload()
My.Settings.Pihole_Enable = False
My.Application.Log.WriteEntry("Pi-hole module is disabled")
Return "Pi-hole module disabled"
End Function
Function Enable() As String
My.Settings.Pihole_Enable = True
My.Application.Log.WriteEntry("Pi-hole module is enabled")
Load()
Return "Pi-hole module enabled"
End Function
Function AuthPiholeAPI() As String
Try
Dim PiholeSID As String = ""
My.Application.Log.WriteEntry("Attempting to authenticate to Pi-hole")
Dim PiholeAuthRequest As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create("http://" & My.Settings.Pihole_IPAddress & "/api/auth"), System.Net.HttpWebRequest)
My.Application.Log.WriteEntry(PiholeAuthRequest.Address.ToString)
PiholeAuthRequest.Method = "POST"
Dim PiholeAuthRequestBody As Byte() = Text.Encoding.UTF8.GetBytes("{""password"":""" & My.Settings.Pihole_APIKey & """}")
PiholeAuthRequest.ContentType = "application/json"
PiholeAuthRequest.ContentLength = PiholeAuthRequestBody.Length
Dim ReqStream As System.IO.Stream = PiholeAuthRequest.GetRequestStream()
ReqStream.Write(PiholeAuthRequestBody, 0, PiholeAuthRequestBody.Length)
Dim PiholeAuthResponse As System.Net.HttpWebResponse = CType(PiholeAuthRequest.GetResponse(), System.Net.HttpWebResponse)
Using ResStream As System.IO.Stream = PiholeAuthResponse.GetResponseStream()
Dim Reader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
Dim OutputJson As String = Reader.ReadToEnd()
Using JsonResponse = JsonDocument.Parse(OutputJson)
PiholeSID = JsonResponse.RootElement.GetProperty("session").GetProperty("sid").GetString()
End Using
End Using
Return PiholeSID
Catch WebEx As System.Net.WebException
Using ResStream As System.IO.Stream = WebEx.Response.GetResponseStream()
Dim Reader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
Dim OutputJson As String = Reader.ReadToEnd()
My.Application.Log.WriteEntry("Pi-hole Auth Error:" & OutputJson, TraceEventType.Error)
End Using
Return "failed"
End Try
End Function
Function GetPiholeAPI() As String
Dim PiholeSID As String = AuthPiholeAPI()
Dim strBlockingStatus As String = ""
If PiholeSID = "failed" Then
Return "failed"
End If
Try
My.Application.Log.WriteEntry("Requesting Pi-hole statistics")
Dim PiholeAPIRequest As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create("http://" & My.Settings.Pihole_IPAddress & "/api/dns/blocking?sid=" & PiholeSID), System.Net.HttpWebRequest)
PiholeAPIRequest.Method = "GET"
Dim PiholeAPIResponse As System.Net.HttpWebResponse = CType(PiholeAPIRequest.GetResponse(), System.Net.HttpWebResponse)
Dim PiholeAPIResponseStream As New System.IO.StreamReader(PiholeAPIResponse.GetResponseStream(), System.Text.Encoding.UTF8)
Dim PiholeAPIJSON As String = PiholeAPIResponseStream.ReadToEnd()
PiholeAPIResponse.Close()
PiholeAPIResponseStream.Close()
My.Application.Log.WriteEntry("Response received: " & PiholeAPIJSON, TraceEventType.Verbose)
Using JsonResponse = JsonDocument.Parse(PiholeAPIJSON)
strBlockingStatus = JsonResponse.RootElement.GetProperty("blocking").GetString()
End Using
Return strBlockingStatus
Catch WebEx As System.Net.WebException
Using ResStream As System.IO.Stream = WebEx.Response.GetResponseStream()
Dim Reader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
Dim OutputJson As String = Reader.ReadToEnd()
My.Application.Log.WriteEntry("Pi-hole Request Error:" & OutputJson, TraceEventType.Error)
End Using
Return "failed"
End Try
End Function
Function SetPiholeBlocking(ByVal strSetBlock As String) As String
Dim PiholeSID As String = AuthPiholeAPI()
Dim strBlockingStatus As String = ""
If PiholeSID = "failed" Then
Return "failed"
End If
Try
My.Application.Log.WriteEntry("Requesting Pi-hole statistics")
Dim PiholeAPIRequest As System.Net.HttpWebRequest = CType(System.Net.WebRequest.Create("http://" & My.Settings.Pihole_IPAddress & "/api/dns/blocking?sid=" & PiholeSID), System.Net.HttpWebRequest)
PiholeAPIRequest.Method = "POST"
Dim PiholeAPIRequestBody As Byte() = Text.Encoding.UTF8.GetBytes("{""blocking"":" & strSetBlock & "}")
PiholeAPIRequest.ContentType = "application/json"
PiholeAPIRequest.ContentLength = PiholeAPIRequestBody.Length
Dim ReqStream As System.IO.Stream = PiholeAPIRequest.GetRequestStream()
ReqStream.Write(PiholeAPIRequestBody, 0, PiholeAPIRequestBody.Length)
Dim PiholeAPIResponse As System.Net.HttpWebResponse = CType(PiholeAPIRequest.GetResponse(), System.Net.HttpWebResponse)
Dim PiholeAPIResponseStream As New System.IO.StreamReader(PiholeAPIResponse.GetResponseStream(), System.Text.Encoding.UTF8)
Dim PiholeAPIJSON As String = PiholeAPIResponseStream.ReadToEnd()
PiholeAPIResponse.Close()
PiholeAPIResponseStream.Close()
My.Application.Log.WriteEntry("Response received: " & PiholeAPIJSON, TraceEventType.Verbose)
Using JsonResponse = JsonDocument.Parse(PiholeAPIJSON)
strBlockingStatus = JsonResponse.RootElement.GetProperty("blocking").GetString()
End Using
Return strBlockingStatus
Catch WebEx As System.Net.WebException
Using ResStream As System.IO.Stream = WebEx.Response.GetResponseStream()
Dim Reader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
Dim OutputJson As String = Reader.ReadToEnd()
My.Application.Log.WriteEntry("Pi-hole Request Error:" & OutputJson, TraceEventType.Error)
End Using
Return "failed"
End Try
End Function
Function Load() As String
If My.Settings.Pihole_Enable = True Then
My.Application.Log.WriteEntry("Loading Pi-hole module")
If My.Settings.Pihole_IPAddress = "0.0.0.0" OrElse My.Settings.Pihole_IPAddress = "" Then
My.Application.Log.WriteEntry("No Pi-hole IP address set, asking for it")
My.Settings.Pihole_IPAddress = InputBox("Enter Pi-hole IP address.", "Pi-hole")
End If
If My.Settings.Pihole_APIKey = "" Then
My.Application.Log.WriteEntry("No Pi-hole API key set, asking for it")
My.Settings.Pihole_APIKey = InputBox("Enter Pi-hole API key.", "Pi-hole")
End If
Return "Pi-hole module loaded"
Else
My.Application.Log.WriteEntry("Pi-hole module is disabled, module not loaded")
Return "Pi-hole module is disabled, module not loaded"
End If
End Function
Function Unload() As String
My.Application.Log.WriteEntry("Unloading Pi-hole module")
Return "Pi-hole module unloaded"
End Function
End Module