ZeroTierNetwork.cs 13 KB

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