ZeroTierNetwork.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.Serialization;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using Newtonsoft.Json;
  8. namespace WinUI
  9. {
  10. [Serializable]
  11. public class ZeroTierNetwork : ISerializable, IEquatable<ZeroTierNetwork>, IComparable<ZeroTierNetwork>
  12. {
  13. protected ZeroTierNetwork(SerializationInfo info, StreamingContext ctx)
  14. {
  15. try
  16. {
  17. NetworkId = info.GetString("nwid");
  18. MacAddress = info.GetString("mac");
  19. NetworkName = info.GetString("name");
  20. NetworkStatus = info.GetString("status");
  21. NetworkType = info.GetString("type");
  22. MTU = info.GetInt32("mtu");
  23. DHCP = info.GetBoolean("dhcp");
  24. Bridge = info.GetBoolean("bridge");
  25. BroadcastEnabled = info.GetBoolean("broadcastEnabled");
  26. PortError = info.GetInt32("portError");
  27. NetconfRevision = info.GetInt32("netconfRevision");
  28. AssignedAddresses = (string[])info.GetValue("assignedAddresses", typeof(string[]));
  29. Routes = (NetworkRoute[])info.GetValue("routes", typeof(NetworkRoute[]));
  30. DeviceName = info.GetString("portDeviceName");
  31. AllowManaged = info.GetBoolean("allowManaged");
  32. AllowGlobal = info.GetBoolean("allowGlobal");
  33. AllowDefault = info.GetBoolean("allowDefault");
  34. }
  35. catch { }
  36. IsConnected = false;
  37. }
  38. public virtual void GetObjectData(SerializationInfo info, StreamingContext ctx)
  39. {
  40. info.AddValue("nwid", NetworkId);
  41. info.AddValue("mac", MacAddress);
  42. info.AddValue("name", NetworkName);
  43. info.AddValue("status", NetworkStatus);
  44. info.AddValue("type", NetworkType);
  45. info.AddValue("mtu", MTU);
  46. info.AddValue("dhcp", DHCP);
  47. info.AddValue("bridge", Bridge);
  48. info.AddValue("broadcastEnabled", BroadcastEnabled);
  49. info.AddValue("portError", PortError);
  50. info.AddValue("netconfRevision", NetconfRevision);
  51. info.AddValue("assignedAddresses", AssignedAddresses);
  52. info.AddValue("routes", Routes);
  53. info.AddValue("portDeviceName", DeviceName);
  54. info.AddValue("allowManaged", AllowManaged);
  55. info.AddValue("allowGlobal", AllowGlobal);
  56. info.AddValue("allowDefault", AllowDefault);
  57. }
  58. [JsonProperty("nwid")]
  59. public string NetworkId { get; set; }
  60. [JsonProperty("mac")]
  61. public string MacAddress { get; set; }
  62. [JsonProperty("name")]
  63. public string NetworkName { get; set; }
  64. [JsonProperty("status")]
  65. public string NetworkStatus { get; set; }
  66. [JsonProperty("type")]
  67. public string NetworkType { get; set; }
  68. [JsonProperty("mtu")]
  69. public int MTU { get; set; }
  70. [JsonProperty("dhcp")]
  71. public bool DHCP { get; set; }
  72. [JsonProperty("bridge")]
  73. public bool Bridge { get; set ; }
  74. [JsonProperty("broadcastEnabled")]
  75. public bool BroadcastEnabled { get ; set; }
  76. [JsonProperty("portError")]
  77. public int PortError { get; set; }
  78. [JsonProperty("netconfRevision")]
  79. public int NetconfRevision { get; set; }
  80. [JsonProperty("assignedAddresses")]
  81. public string[] AssignedAddresses { get; set; }
  82. [JsonProperty("routes")]
  83. public NetworkRoute[] Routes { get; set; }
  84. [JsonProperty("portDeviceName")]
  85. public string DeviceName { get; set; }
  86. [JsonProperty("allowManaged")]
  87. public bool AllowManaged { get; set; }
  88. [JsonProperty("allowGlobal")]
  89. public bool AllowGlobal { get; set; }
  90. [JsonProperty("allowDefault")]
  91. public bool AllowDefault { get; set; }
  92. public bool IsConnected { get; set; } = false;
  93. public String Title
  94. {
  95. get
  96. {
  97. if (NetworkName != null && NetworkName.Length > 0)
  98. {
  99. return NetworkId + " (" + NetworkName + ")";
  100. }
  101. else
  102. {
  103. return NetworkId;
  104. }
  105. }
  106. }
  107. public bool Equals(ZeroTierNetwork network)
  108. {
  109. if (NetworkId == null || network == null)
  110. return false;
  111. return NetworkId.Equals(network.NetworkId);
  112. }
  113. public int CompareTo(ZeroTierNetwork network)
  114. {
  115. if (NetworkId == null || network == null)
  116. return -1;
  117. UInt64 thisNwid = UInt64.Parse(NetworkId, System.Globalization.NumberStyles.HexNumber);
  118. UInt64 otherNwid = UInt64.Parse(network.NetworkId, System.Globalization.NumberStyles.HexNumber);
  119. if (thisNwid > otherNwid)
  120. {
  121. return 1;
  122. }
  123. else if (thisNwid < otherNwid)
  124. {
  125. return -1;
  126. }
  127. else
  128. {
  129. return 0;
  130. }
  131. }
  132. }
  133. }