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