ZeroTierNetwork.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Linq;
  5. using System.Runtime.CompilerServices;
  6. using System.Runtime.Serialization;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Newtonsoft.Json;
  10. namespace WinUI
  11. {
  12. [Serializable]
  13. public class ZeroTierNetwork : ISerializable, IEquatable<ZeroTierNetwork>, IComparable<ZeroTierNetwork>, INotifyPropertyChanged
  14. {
  15. private string networkId;
  16. private string macAddress;
  17. private string networkName;
  18. private string networkStatus;
  19. private string networkType;
  20. private Int32 mtu;
  21. private bool dhcp;
  22. private bool bridge;
  23. private bool broadcastEnabled;
  24. private Int32 portError;
  25. private Int32 netconfRevision;
  26. private string[] assignedAddresses;
  27. private NetworkRoute[] routes;
  28. private string deviceName;
  29. private bool allowManaged;
  30. private bool allowGlobal;
  31. private bool allowDefault;
  32. private bool isConnected;
  33. protected ZeroTierNetwork(SerializationInfo info, StreamingContext ctx)
  34. {
  35. try
  36. {
  37. NetworkId = info.GetString("nwid");
  38. MacAddress = info.GetString("mac");
  39. NetworkName = info.GetString("name");
  40. NetworkStatus = info.GetString("status");
  41. NetworkType = info.GetString("type");
  42. MTU = info.GetInt32("mtu");
  43. DHCP = info.GetBoolean("dhcp");
  44. Bridge = info.GetBoolean("bridge");
  45. BroadcastEnabled = info.GetBoolean("broadcastEnabled");
  46. PortError = info.GetInt32("portError");
  47. NetconfRevision = info.GetInt32("netconfRevision");
  48. AssignedAddresses = (string[])info.GetValue("assignedAddresses", typeof(string[]));
  49. Routes = (NetworkRoute[])info.GetValue("routes", typeof(NetworkRoute[]));
  50. DeviceName = info.GetString("portDeviceName");
  51. AllowManaged = info.GetBoolean("allowManaged");
  52. AllowGlobal = info.GetBoolean("allowGlobal");
  53. AllowDefault = info.GetBoolean("allowDefault");
  54. }
  55. catch { }
  56. IsConnected = false;
  57. }
  58. public event PropertyChangedEventHandler PropertyChanged;
  59. public virtual void GetObjectData(SerializationInfo info, StreamingContext ctx)
  60. {
  61. info.AddValue("nwid", NetworkId);
  62. info.AddValue("mac", MacAddress);
  63. info.AddValue("name", NetworkName);
  64. info.AddValue("status", NetworkStatus);
  65. info.AddValue("type", NetworkType);
  66. info.AddValue("mtu", MTU);
  67. info.AddValue("dhcp", DHCP);
  68. info.AddValue("bridge", Bridge);
  69. info.AddValue("broadcastEnabled", BroadcastEnabled);
  70. info.AddValue("portError", PortError);
  71. info.AddValue("netconfRevision", NetconfRevision);
  72. info.AddValue("assignedAddresses", AssignedAddresses);
  73. info.AddValue("routes", Routes);
  74. info.AddValue("portDeviceName", DeviceName);
  75. info.AddValue("allowManaged", AllowManaged);
  76. info.AddValue("allowGlobal", AllowGlobal);
  77. info.AddValue("allowDefault", AllowDefault);
  78. }
  79. public void UpdateNetwork(ZeroTierNetwork network)
  80. {
  81. if (network == null)
  82. return;
  83. if (!NetworkId.Equals(network.NetworkId))
  84. {
  85. NetworkId = network.NetworkId;
  86. }
  87. if (!MacAddress.Equals(network.MacAddress))
  88. {
  89. MacAddress = network.MacAddress;
  90. }
  91. if (!NetworkName.Equals(network.NetworkName))
  92. {
  93. NetworkName = network.NetworkName;
  94. }
  95. if (!NetworkStatus.Equals(network.NetworkStatus))
  96. {
  97. NetworkStatus = network.NetworkStatus;
  98. }
  99. if (!NetworkType.Equals(network.NetworkType))
  100. {
  101. NetworkType = network.NetworkType;
  102. }
  103. if (MTU != network.MTU)
  104. {
  105. MTU = network.MTU;
  106. }
  107. if (DHCP != network.DHCP)
  108. {
  109. DHCP = network.DHCP;
  110. }
  111. if (Bridge != network.Bridge)
  112. {
  113. Bridge = network.Bridge;
  114. }
  115. if (BroadcastEnabled != network.BroadcastEnabled)
  116. {
  117. BroadcastEnabled = network.BroadcastEnabled;
  118. }
  119. if (PortError != network.PortError)
  120. {
  121. PortError = network.PortError;
  122. }
  123. if (NetconfRevision != network.NetconfRevision)
  124. {
  125. NetconfRevision = network.NetconfRevision;
  126. }
  127. AssignedAddresses = network.AssignedAddresses;
  128. Routes = network.Routes;
  129. if (!DeviceName.Equals(network.DeviceName))
  130. {
  131. DeviceName = network.DeviceName;
  132. }
  133. if (AllowManaged != network.AllowManaged)
  134. {
  135. AllowManaged = network.AllowManaged;
  136. }
  137. if (AllowGlobal != network.AllowGlobal)
  138. {
  139. AllowGlobal = network.AllowGlobal;
  140. }
  141. if (AllowDefault != network.AllowDefault)
  142. {
  143. AllowDefault = network.AllowDefault;
  144. }
  145. if (IsConnected != network.IsConnected)
  146. {
  147. IsConnected = network.IsConnected;
  148. }
  149. }
  150. protected void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
  151. {
  152. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  153. }
  154. [JsonProperty("nwid")]
  155. public string NetworkId {
  156. get
  157. {
  158. return networkId;
  159. }
  160. set
  161. {
  162. networkId = value;
  163. NotifyPropertyChanged();
  164. }
  165. }
  166. [JsonProperty("mac")]
  167. public string MacAddress
  168. {
  169. get
  170. {
  171. return macAddress;
  172. }
  173. set
  174. {
  175. macAddress = value;
  176. NotifyPropertyChanged();
  177. }
  178. }
  179. [JsonProperty("name")]
  180. public string NetworkName
  181. {
  182. get
  183. {
  184. return networkName;
  185. }
  186. set
  187. {
  188. networkName = value;
  189. NotifyPropertyChanged();
  190. }
  191. }
  192. [JsonProperty("status")]
  193. public string NetworkStatus
  194. {
  195. get
  196. {
  197. return networkStatus;
  198. }
  199. set
  200. {
  201. networkStatus = value;
  202. NotifyPropertyChanged();
  203. }
  204. }
  205. [JsonProperty("type")]
  206. public string NetworkType
  207. {
  208. get
  209. {
  210. return networkType;
  211. }
  212. set
  213. {
  214. networkType = value;
  215. NotifyPropertyChanged();
  216. }
  217. }
  218. [JsonProperty("mtu")]
  219. public int MTU
  220. {
  221. get
  222. {
  223. return mtu;
  224. }
  225. set
  226. {
  227. mtu = value;
  228. NotifyPropertyChanged();
  229. }
  230. }
  231. [JsonProperty("dhcp")]
  232. public bool DHCP
  233. {
  234. get
  235. {
  236. return dhcp;
  237. }
  238. set
  239. {
  240. dhcp = value;
  241. NotifyPropertyChanged();
  242. }
  243. }
  244. [JsonProperty("bridge")]
  245. public bool Bridge
  246. {
  247. get
  248. {
  249. return bridge;
  250. }
  251. set
  252. {
  253. bridge = value;
  254. NotifyPropertyChanged();
  255. }
  256. }
  257. [JsonProperty("broadcastEnabled")]
  258. public bool BroadcastEnabled
  259. {
  260. get
  261. {
  262. return broadcastEnabled;
  263. }
  264. set
  265. {
  266. broadcastEnabled = value;
  267. NotifyPropertyChanged();
  268. }
  269. }
  270. [JsonProperty("portError")]
  271. public int PortError
  272. {
  273. get
  274. {
  275. return portError;
  276. }
  277. set
  278. {
  279. portError = value;
  280. NotifyPropertyChanged();
  281. }
  282. }
  283. [JsonProperty("netconfRevision")]
  284. public int NetconfRevision
  285. {
  286. get
  287. {
  288. return netconfRevision;
  289. }
  290. set
  291. {
  292. netconfRevision = value;
  293. NotifyPropertyChanged();
  294. }
  295. }
  296. [JsonProperty("assignedAddresses")]
  297. public string[] AssignedAddresses
  298. {
  299. get
  300. {
  301. return assignedAddresses;
  302. }
  303. set
  304. {
  305. assignedAddresses = value;
  306. NotifyPropertyChanged();
  307. }
  308. }
  309. [JsonProperty("routes")]
  310. public NetworkRoute[] Routes
  311. {
  312. get
  313. {
  314. return routes;
  315. }
  316. set
  317. {
  318. routes = value;
  319. NotifyPropertyChanged();
  320. }
  321. }
  322. [JsonProperty("portDeviceName")]
  323. public string DeviceName
  324. {
  325. get
  326. {
  327. return deviceName;
  328. }
  329. set
  330. {
  331. deviceName = value;
  332. NotifyPropertyChanged();
  333. }
  334. }
  335. [JsonProperty("allowManaged")]
  336. public bool AllowManaged
  337. {
  338. get
  339. {
  340. return allowManaged;
  341. }
  342. set
  343. {
  344. allowManaged = value;
  345. NotifyPropertyChanged();
  346. }
  347. }
  348. [JsonProperty("allowGlobal")]
  349. public bool AllowGlobal
  350. {
  351. get
  352. {
  353. return allowGlobal;
  354. }
  355. set
  356. {
  357. allowGlobal = value;
  358. NotifyPropertyChanged();
  359. }
  360. }
  361. [JsonProperty("allowDefault")]
  362. public bool AllowDefault
  363. {
  364. get
  365. {
  366. return allowDefault;
  367. }
  368. set
  369. {
  370. allowDefault = value;
  371. NotifyPropertyChanged();
  372. }
  373. }
  374. public bool IsConnected
  375. {
  376. get
  377. {
  378. return isConnected;
  379. }
  380. set
  381. {
  382. isConnected = value;
  383. NotifyPropertyChanged();
  384. }
  385. }
  386. public String Title
  387. {
  388. get
  389. {
  390. if (NetworkName != null && NetworkName.Length > 0)
  391. {
  392. return NetworkId + " (" + NetworkName + ")";
  393. }
  394. else
  395. {
  396. return NetworkId;
  397. }
  398. }
  399. }
  400. public bool Equals(ZeroTierNetwork network)
  401. {
  402. if (NetworkId == null || network == null)
  403. return false;
  404. return NetworkId.Equals(network.NetworkId);
  405. }
  406. public int CompareTo(ZeroTierNetwork network)
  407. {
  408. if (NetworkId == null || network == null)
  409. return -1;
  410. UInt64 thisNwid = UInt64.Parse(NetworkId, System.Globalization.NumberStyles.HexNumber);
  411. UInt64 otherNwid = UInt64.Parse(network.NetworkId, System.Globalization.NumberStyles.HexNumber);
  412. if (thisNwid > otherNwid)
  413. {
  414. return 1;
  415. }
  416. else if (thisNwid < otherNwid)
  417. {
  418. return -1;
  419. }
  420. else
  421. {
  422. return 0;
  423. }
  424. }
  425. }
  426. public class NetworkEqualityComparer : IEqualityComparer<ZeroTierNetwork>
  427. {
  428. public bool Equals(ZeroTierNetwork lhs, ZeroTierNetwork rhs)
  429. {
  430. if (lhs.NetworkId.Equals(rhs.NetworkId))
  431. {
  432. lhs.UpdateNetwork(rhs);
  433. return true;
  434. }
  435. return false;
  436. }
  437. public int GetHashCode(ZeroTierNetwork obj)
  438. {
  439. return obj.NetworkId.GetHashCode();
  440. }
  441. }
  442. }