1
1
using System ;
2
+ using System . Collections . Generic ;
2
3
using System . Diagnostics ;
3
4
using System . IO ;
5
+ using System . Linq ;
4
6
using System . Net ;
5
7
using System . Net . NetworkInformation ;
8
+ using System . Net . Sockets ;
6
9
using System . Text ;
7
10
using System . Threading ;
8
11
using System . Timers ;
@@ -41,8 +44,8 @@ public static void Load()
41
44
PingTimer . Enabled = true ;
42
45
modLogging . LogEvent ( "Connectivity checks scheduled" , EventLogEntryType . Information ) ;
43
46
44
- Thread InitialGetPublicIP = new Thread ( InitialGetPublicIPHandler ) ;
45
- InitialGetPublicIP . Start ( ) ;
47
+ Thread InitialGetIP = new Thread ( InitialGetIPHandler ) ;
48
+ InitialGetIP . Start ( ) ;
46
49
}
47
50
48
51
/// <summary>
@@ -56,6 +59,42 @@ public static void Unload()
56
59
}
57
60
}
58
61
62
+ /// <summary>
63
+ /// Gets this agent's local network address
64
+ /// </summary>
65
+ /// <returns>(string) Local IP address</returns>
66
+ public static string GetLocalIPAddress ( )
67
+ {
68
+ if ( IsOnline == true )
69
+ {
70
+ // https://stackoverflow.com/a/44226831
71
+ // TODO: We need to account for wireless adapters too.
72
+ List < string > iplist = NetworkInterface . GetAllNetworkInterfaces ( )
73
+ . Where ( x => x . NetworkInterfaceType == NetworkInterfaceType . Ethernet && x . OperationalStatus == OperationalStatus . Up )
74
+ . SelectMany ( x => x . GetIPProperties ( ) . UnicastAddresses )
75
+ . Where ( x => x . Address . AddressFamily == AddressFamily . InterNetwork )
76
+ . Select ( x => x . Address . ToString ( ) )
77
+ . ToList ( ) ;
78
+
79
+ foreach ( string ip in iplist )
80
+ {
81
+ string newLocalIP = ip ;
82
+
83
+ string oldLocalIP = modDatabase . GetConfig ( "Ping_LastKnownLocalIP" ) ;
84
+ if ( oldLocalIP != newLocalIP )
85
+ {
86
+ modLogging . LogEvent ( "Local IP address changed from " + oldLocalIP + " to " + newLocalIP , EventLogEntryType . Warning ) ;
87
+ modDatabase . AddOrUpdateConfig ( new modDatabase . Config { Key = "Ping_LastKnownLocalIP" , Value = newLocalIP } ) ;
88
+ }
89
+
90
+ return newLocalIP ;
91
+ }
92
+
93
+ return "No valid local IP address found" ;
94
+ }
95
+ else return "Not online" ;
96
+ }
97
+
59
98
/// <summary>
60
99
/// Checks a public service for this agent's public IP address
61
100
/// </summary>
@@ -141,8 +180,9 @@ public static string SendPing(string Host, int repeat = 1)
141
180
/// <summary>
142
181
/// Handler to launch initial public IP check on a new Thread
143
182
/// </summary>
144
- private static void InitialGetPublicIPHandler ( )
183
+ private static void InitialGetIPHandler ( )
145
184
{
185
+ GetLocalIPAddress ( ) ;
146
186
GetPublicIPAddress ( ) ;
147
187
}
148
188
0 commit comments