-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodNetwork.cs
More file actions
283 lines (260 loc) · 11.8 KB
/
modNetwork.cs
File metadata and controls
283 lines (260 loc) · 11.8 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Text.Json;
using System.Threading;
using System.Timers;
namespace XRFAgent
{
internal class modNetwork
{
private static System.Timers.Timer PingTimer;
private static string Ping_InternetCheckAddress;
private static string Ping_PublicIPSource;
public static bool IsOnline = true; // assume until tested
/// <summary>
/// Loads the network module: Schedules connectivity checks and checks the public IP
/// </summary>
public static void Load()
{
Ping_InternetCheckAddress = modDatabase.GetConfig("Ping_InternetCheckAddress");
if (string.IsNullOrEmpty(Ping_InternetCheckAddress))
{
modDatabase.AddConfig(new modDatabase.Config { Key = "Ping_InternetCheckAddress", Value = "4.2.2.2" });
Ping_InternetCheckAddress = "4.2.2.2";
}
Ping_PublicIPSource = modDatabase.GetConfig("Ping_PublicIPSource");
if (string.IsNullOrEmpty(Ping_PublicIPSource))
{
modDatabase.AddConfig(new modDatabase.Config { Key = "Ping_PublicIPSource", Value = "dyndns" });
Ping_PublicIPSource = "dyndns";
}
PingTimer = new System.Timers.Timer();
PingTimer.Elapsed += new ElapsedEventHandler(PingInternetHandler);
PingTimer.Interval = 60000; // 1 min
PingTimer.Enabled = true;
modLogging.LogEvent("Connectivity checks scheduled", EventLogEntryType.Information);
Thread InitialGetIP = new Thread(InitialGetIPHandler);
InitialGetIP.Start();
}
/// <summary>
/// Unloads the network module: Stops connectivity checks
/// </summary>
public static void Unload()
{
if (PingTimer != null)
{
PingTimer.Stop();
}
}
/// <summary>
/// Gets this agent's local network address
/// </summary>
/// <returns>(string) Local IP address</returns>
public static string GetLocalIPAddress()
{
if (IsOnline == true)
{
// https://stackoverflow.com/a/44226831
List<string> iplist = NetworkInterface.GetAllNetworkInterfaces()
.Where(x => (x.NetworkInterfaceType == NetworkInterfaceType.Ethernet || x.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) && x.OperationalStatus == OperationalStatus.Up)
.SelectMany(x => x.GetIPProperties().UnicastAddresses)
.Where(x => x.Address.AddressFamily == AddressFamily.InterNetwork)
.Select(x => x.Address.ToString())
.ToList();
foreach (string ip in iplist)
{
string newLocalIP = ip;
string oldLocalIP = modDatabase.GetConfig("Ping_LastKnownLocalIP");
if (oldLocalIP != newLocalIP)
{
modLogging.LogEvent("Local IP address changed from " + oldLocalIP + " to " + newLocalIP, EventLogEntryType.Warning, 6014);
modDatabase.AddOrUpdateConfig(new modDatabase.Config { Key = "Ping_LastKnownLocalIP", Value = newLocalIP });
}
return newLocalIP;
}
return "No valid local IP address found";
}
else return "Not online";
}
/// <summary>
/// Checks a public service for this agent's public IP address
/// </summary>
/// <returns>(string) Public IP address</returns>
public static string GetPublicIPAddress()
{
if (IsOnline == true)
{
try
{
if (Ping_PublicIPSource == "dyndns")
{
string reqUrl = "http://checkip.dyndns.org";
WebRequest req = WebRequest.Create(reqUrl);
WebResponse resp = req.GetResponse();
StreamReader sr = new StreamReader(resp.GetResponseStream());
string response = sr.ReadToEnd().Trim();
string[] responseArray = response.Split(':');
string[] responseArray2 = responseArray[1].Split('<');
string newPublicIP = responseArray2[0].Trim();
string oldPublicIP = modDatabase.GetConfig("Ping_LastKnownPublicIP");
if (oldPublicIP != newPublicIP)
{
modLogging.LogEvent("Public IP address changed from " + oldPublicIP + " to " + newPublicIP, EventLogEntryType.Warning, 6013);
modDatabase.AddOrUpdateConfig(new modDatabase.Config { Key = "Ping_LastKnownPublicIP", Value = newPublicIP });
}
return newPublicIP;
}
else return "No valid public IP source set";
}
catch (Exception err)
{
modLogging.LogEvent(err.Message, EventLogEntryType.Error, 6012);
return "Error";
}
}
else return "Not online";
}
/// <summary>
/// Runs Ookla Speedtest, installs Speedtest CLI tool if it is not present
/// </summary>
/// <returns></returns>
public static int RunSpeedTest()
{
try
{
if (File.Exists(Properties.Settings.Default.Tools_FolderURI + "speedtest.exe") == false)
{
int installResult = modUpdate.InstallOoklaSpeedtest();
if (installResult == -1)
{
return -1;
}
}
Process SpeedTestRunner = new Process();
SpeedTestRunner.StartInfo.UseShellExecute = false;
SpeedTestRunner.StartInfo.RedirectStandardOutput = true;
SpeedTestRunner.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
SpeedTestRunner.StartInfo.FileName = Properties.Settings.Default.Tools_FolderURI + "speedtest.exe";
SpeedTestRunner.StartInfo.Arguments = "-f json --accept-license";
SpeedTestRunner.Start();
SpeedTestRunner.WaitForExit();
string speedJson = SpeedTestRunner.StandardOutput.ReadToEnd();
modLogging.LogEvent("Speed test results: " + speedJson, EventLogEntryType.Information, 6072);
using (JsonDocument speedResults = JsonDocument.Parse(speedJson))
{
string timestamp = speedResults.RootElement.GetProperty("timestamp").GetString();
double downSpeed = Math.Round(speedResults.RootElement.GetProperty("download").GetProperty("bandwidth").GetInt64() / 125000D, 2);
double upSpeed = Math.Round(speedResults.RootElement.GetProperty("upload").GetProperty("bandwidth").GetInt64() / 125000D, 2);
modLogging.LogEvent("Speed test results: " + downSpeed.ToString() + " Mbps Down, " + upSpeed.ToString() + " Mbps Up", EventLogEntryType.Information, 6072);
string SpeedTestResultsJSON = "{\"systemdetails\":[";
modDatabase.Config ConfigObj;
ConfigObj = new modDatabase.Config { Key = "Speedtest_Download", Value = downSpeed.ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SpeedTestResultsJSON = SpeedTestResultsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
ConfigObj = new modDatabase.Config { Key = "Speedtest_Upload", Value = upSpeed.ToString() };
modDatabase.AddOrUpdateConfig(ConfigObj);
SpeedTestResultsJSON = SpeedTestResultsJSON + JsonSerializer.Serialize(ConfigObj) + ",";
ConfigObj = new modDatabase.Config { Key = "Speedtest_LastRun", Value = timestamp };
modDatabase.AddOrUpdateConfig(ConfigObj);
SpeedTestResultsJSON = SpeedTestResultsJSON + JsonSerializer.Serialize(ConfigObj) + "]}";
modSync.SendMessage("server", "nodedata", "systemdetails", SpeedTestResultsJSON);
}
return SpeedTestRunner.ExitCode;
}
catch (Exception err)
{
modLogging.LogEvent("Speed test error: " + err.Message, EventLogEntryType.Error, 6071);
return -1;
}
}
/// <summary>
/// Attempts a specified number of pings until it receives a successful response
/// </summary>
/// <param name="Host">(string) Destination</param>
/// <param name="repeat">(int) Number of times to try</param>
/// <returns>(string) Result of ping attempt</returns>
public static string SendPing(string Host, int repeat = 1)
{
try
{
Ping a = new Ping();
PingReply b;
string responseData = "";
PingOptions c = new PingOptions();
c.DontFragment = true;
c.Ttl = 64;
string data = "aaaaaaaaaaaaaaaa";
Byte[] bt = Encoding.ASCII.GetBytes(data);
Int16 i;
for (i = 1; i < repeat; i++)
{
b = a.Send(Host, 2000, bt, c);
if (b.Status == IPStatus.Success)
{
responseData = "Reply from " + Host + " in " + b.RoundtripTime.ToString() + " ms, ttl " + b.Options.Ttl;
return responseData;
}
else
{
responseData = b.Status.ToString();
}
}
return responseData;
}
catch (Exception err)
{
if (err.InnerException != null)
{
modLogging.LogEvent(err.InnerException.Message, EventLogEntryType.Error, 6011);
} else
{
modLogging.LogEvent(err.Message, EventLogEntryType.Error, 6011);
}
return "Ping error";
}
}
/// <summary>
/// Handler to launch initial public IP check on a new Thread
/// </summary>
private static void InitialGetIPHandler()
{
GetLocalIPAddress();
GetPublicIPAddress();
}
/// <summary>
/// Handler to launch scheduled connectivity checks
/// </summary>
/// <param name="sender">(object) Sender</param>
/// <param name="e">(EventArgs) Event Arguments</param>
private static void PingInternetHandler(object sender, EventArgs e)
{
string response = "";
response = SendPing(Ping_InternetCheckAddress, 4);
if (response.StartsWith("Reply from"))
{
if (IsOnline == false)
{
modLogging.LogEvent("System is now connected to the Internet", EventLogEntryType.Information);
GetLocalIPAddress();
GetPublicIPAddress();
}
IsOnline = true;
}
else
{
if (IsOnline == true)
{
modLogging.LogEvent("System is not connected to the Internet", EventLogEntryType.Warning);
}
IsOnline = false;
}
}
}
}