-
Notifications
You must be signed in to change notification settings - Fork 1
/
modSync.vb
163 lines (146 loc) · 8.14 KB
/
modSync.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
Imports System.Text.Json
Module modSync
Dim tmrSyncHeartbeatTimer As System.Timers.Timer
Sub InitialHeartbeatHandler()
If My.Settings.Sync_LocalQueueMode = False Then
SendMessage("server", "fetch", "none")
Else
GetLocalMessage()
End If
End Sub
Sub SendHeartbeatHandler(sender As Object, e As EventArgs)
If My.Settings.Sync_LocalQueueMode = False Then
SendMessage("server", "fetch", "none")
Else
GetLocalMessage()
End If
End Sub
Function ClearSyncCredentials() As String
If My.Settings.Sync_Enable = True Then
My.Application.Log.WriteEntry("Cannot clear sync credentials, sync is enabled", TraceEventType.Warning)
Return "Cannot clear sync credentials, sync is enabled"
Else
My.Settings.Sync_ServerURL = ""
My.Settings.Sync_AccessKey = ""
My.Settings.Sync_SandstormToken = ""
My.Settings.Sync_CryptoKey = ""
Return "Sync credentials cleared"
End If
End Function
Function Disable() As String
Unload()
My.Settings.Sync_Enable = False
My.Application.Log.WriteEntry("Sync module is disabled")
Return "Sync module disabled"
End Function
Function Enable() As String
My.Settings.Sync_Enable = True
My.Application.Log.WriteEntry("Sync module is enabled")
Load()
Return "Sync module enabled"
End Function
Function GetLocalMessage() As String
Dim strLocalMesg As String = ""
modDatabase.ExecuteReader("SELECT Mesg FROM LOCALQUEUE WHERE Src = 'sync' AND Auth = 'server' AND Dest = 'hac' AND Recv = 0 LIMIT 1", strLocalMesg)
modDatabase.Execute("UPDATE LOCALQUEUE SET Recv = 1 WHERE Src = 'sync' AND Auth = 'server' AND Dest = 'hac' AND Recv = 0 AND Mesg = '" + strLocalMesg + "'")
modConverse.Interpret(strLocalMesg, True, False)
Return "OK"
End Function
Function Load() As String
If My.Settings.Sync_Enable = True Then
My.Application.Log.WriteEntry("Loading sync module")
If My.Settings.Sync_ServerURL = "" Then
My.Application.Log.WriteEntry("No sync server URL set, asking for it")
My.Settings.Sync_ServerURL = InputBox("Enter sync server URL.", "Sync Server URL")
End If
If My.Settings.Sync_SandstormToken = "" AndAlso (My.Settings.Sync_ServerURL.StartsWith("http://api-") Or My.Settings.Sync_ServerURL.StartsWith("https://api-")) Then
My.Application.Log.WriteEntry("Sandstorm Sync URL identified, asking for token")
My.Settings.Sync_SandstormToken = InputBox("Sandstorm requires an additional token for access.", "Sandstorm Access Token")
End If
If My.Settings.Sync_AccessKey = "" Then
My.Application.Log.WriteEntry("No sync server access key set, asking for it")
My.Settings.Sync_AccessKey = InputBox("Enter sync server access key.", "Sync Server Access Key")
End If
If My.Settings.Sync_CryptoKey = "" Then
My.Application.Log.WriteEntry("No sync server crypto key set, asking for it")
My.Settings.Sync_CryptoKey = InputBox("Enter cryptographic key for synced nodes", "Sync Crypto Key")
End If
tmrSyncHeartbeatTimer = New System.Timers.Timer
My.Application.Log.WriteEntry("Scheduling automatic heartbeats to sync server")
AddHandler tmrSyncHeartbeatTimer.Elapsed, AddressOf SendHeartbeatHandler
tmrSyncHeartbeatTimer.Interval = 300000 ' 5min
tmrSyncHeartbeatTimer.Enabled = True
Dim InitialSyncHeartbeat As New Threading.Thread(AddressOf InitialHeartbeatHandler)
InitialSyncHeartbeat.Start()
Return "Sync module loaded"
Else
My.Application.Log.WriteEntry("Sync module is disabled, module not loaded")
Return "Sync module is disabled, module not loaded"
End If
End Function
Function SendMessage(ByVal strDestination As String, ByVal strMessageType As String, ByVal strMessage As String) As String
If My.Settings.Sync_Enable = True Then
If modGlobal.IsOnline = True Then
My.Application.Log.WriteEntry("Sending " & strMessageType & " to " & strDestination, TraceEventType.Verbose)
Dim strWinVer As String = ""
modDatabase.ExecuteReader("SELECT Value FROM CONFIG WHERE Key = 'System_LastKnownWindowsVersion'", strWinVer)
Dim Req As System.Net.HttpWebRequest
Dim TargetUri As New Uri(My.Settings.Sync_ServerURL & "?message_type=" & strMessageType & "&destination=" & strDestination & "&access_key=" & My.Settings.Sync_AccessKey & "&message=" & strMessage & "&user_agent=HAController/" & My.Application.Info.Version.ToString & "&ip_address=" & My.Settings.Ping_LastKnownPublicIP & "&windows_version=" & strWinVer)
Dim Output As System.Net.HttpWebResponse
Req = DirectCast(System.Net.HttpWebRequest.Create(TargetUri), System.Net.HttpWebRequest)
Req.UserAgent = "HAController/" & My.Application.Info.Version.ToString
Req.KeepAlive = False
Req.Timeout = 10000
Req.Proxy = Nothing
Req.ServicePoint.ConnectionLeaseTimeout = 10000
Req.ServicePoint.MaxIdleTime = 10000
If My.Settings.Sync_SandstormToken <> "" Then
Dim EncodedCreds As String = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes("sandstorm:" + My.Settings.Sync_SandstormToken))
Req.Headers.Add("Authorization", "Basic " + EncodedCreds)
End If
Try
Output = Req.GetResponse()
Using ResStream As System.IO.Stream = Output.GetResponseStream()
Dim Reader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
Dim OutputStream As String = Reader.ReadToEnd()
My.Application.Log.WriteEntry("Sync Response: " & CStr(CInt(Output.StatusCode)) & " " & Output.StatusCode.ToString & " " & OutputStream)
If OutputStream <> "[]" Then
Using MessagesReceived = JsonDocument.Parse(OutputStream)
For Each element As JsonElement In MessagesReceived.RootElement.EnumerateArray()
Dim strSource As String = element.GetProperty("source").ToString()
Dim strMesg As String = element.GetProperty("mesg").ToString()
modConverse.Interpret(strMesg, True, False)
Next
End Using
End If
End Using
Output.Close()
Return "OK"
Catch WebEx As System.Net.WebException
If WebEx.Response IsNot Nothing Then
Using ResStream As System.IO.Stream = WebEx.Response.GetResponseStream()
Dim Reader As System.IO.StreamReader = New System.IO.StreamReader(ResStream)
Dim OutputStream As String = Reader.ReadToEnd()
My.Application.Log.WriteEntry("Sync Error: " & OutputStream, TraceEventType.Error)
End Using
Else
My.Application.Log.WriteException(WebEx)
End If
Return "Failed"
End Try
Else
Return "Offline"
End If
Else
Return "Disabled"
End If
End Function
Function Unload() As String
My.Application.Log.WriteEntry("Unloading sync module")
If tmrSyncHeartbeatTimer IsNot Nothing Then
tmrSyncHeartbeatTimer.Enabled = False
RemoveHandler tmrSyncHeartbeatTimer.Elapsed, AddressOf SendHeartbeatHandler
End If
Return "Sync module unloaded"
End Function
End Module