MainWindow.xaml.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Timers;
  8. using System.Threading.Tasks;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11. using System.Windows.Data;
  12. using System.Windows.Documents;
  13. using System.Windows.Input;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using System.Windows.Navigation;
  17. using System.Windows.Shapes;
  18. using System.Windows.Threading;
  19. namespace WinUI
  20. {
  21. /// <summary>
  22. /// Interaction logic for MainWindow.xaml
  23. /// </summary>
  24. public partial class MainWindow : Window
  25. {
  26. APIHandler handler;
  27. Regex charRegex = new Regex("[0-9a-fxA-FX]");
  28. Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$");
  29. Timer timer = new Timer();
  30. bool connected = false;
  31. public MainWindow()
  32. {
  33. InitializeComponent();
  34. if (InitAPIHandler())
  35. {
  36. networksPage.SetAPIHandler(handler);
  37. updateStatus();
  38. if (!connected)
  39. {
  40. MessageBox.Show("Unable to connect to ZeroTier Service.");
  41. }
  42. updateNetworks();
  43. //updatePeers();
  44. DataObject.AddPastingHandler(joinNetworkID, OnPaste);
  45. timer.Elapsed += new ElapsedEventHandler(OnUpdateTimer);
  46. timer.Interval = 2000;
  47. timer.Enabled = true;
  48. }
  49. }
  50. private bool InitAPIHandler()
  51. {
  52. String ztDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\ZeroTier\\One";
  53. String authToken = "";
  54. Int32 port = 9993;
  55. try
  56. {
  57. byte[] tmp = File.ReadAllBytes(ztDir + "\\authtoken.secret");
  58. authToken = System.Text.Encoding.ASCII.GetString(tmp).Trim();
  59. }
  60. catch
  61. {
  62. MessageBox.Show("Unable to read ZeroTier One authtoken.secret from:\r\n" + ztDir, "ZeroTier One");
  63. this.Close();
  64. return false;
  65. }
  66. if ((authToken == null) || (authToken.Length <= 0))
  67. {
  68. MessageBox.Show("Unable to read ZeroTier One authtoken.secret from:\r\n" + ztDir, "ZeroTier One");
  69. this.Close();
  70. return false;
  71. }
  72. try
  73. {
  74. byte[] tmp = File.ReadAllBytes(ztDir + "\\zerotier-one.port");
  75. port = Int32.Parse(System.Text.Encoding.ASCII.GetString(tmp).Trim());
  76. if ((port <= 0) || (port > 65535))
  77. port = 9993;
  78. }
  79. catch
  80. {
  81. }
  82. handler = new APIHandler(port, authToken);
  83. return true;
  84. }
  85. private void updateStatus()
  86. {
  87. var status = handler.GetStatus();
  88. if (status != null)
  89. {
  90. connected = true;
  91. networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  92. {
  93. this.networkId.Content = status.Address;
  94. }));
  95. versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  96. {
  97. this.versionString.Content = status.Version;
  98. }));
  99. onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  100. {
  101. this.onlineStatus.Content = (status.Online ? "ONLINE" : "OFFLINE");
  102. }));
  103. }
  104. else
  105. {
  106. connected = false;
  107. networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  108. {
  109. this.networkId.Content = "";
  110. }));
  111. versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  112. {
  113. this.versionString.Content = "0";
  114. }));
  115. onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  116. {
  117. this.onlineStatus.Content = "OFFLINE";
  118. }));
  119. }
  120. }
  121. private void updateNetworks()
  122. {
  123. var networks = handler.GetNetworks();
  124. networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  125. {
  126. networksPage.setNetworks(networks);
  127. }));
  128. }
  129. private void updatePeers()
  130. {
  131. //var peers = handler.GetPeers();
  132. //peersPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
  133. //{
  134. // peersPage.SetPeers(peers);
  135. //}));
  136. }
  137. private void OnUpdateTimer(object source, ElapsedEventArgs e)
  138. {
  139. updateStatus();
  140. updateNetworks();
  141. //updatePeers();
  142. }
  143. private void joinButton_Click(object sender, RoutedEventArgs e)
  144. {
  145. if (joinNetworkID.Text.Length < 16)
  146. {
  147. MessageBox.Show("Invalid Network ID");
  148. }
  149. else
  150. {
  151. handler.JoinNetwork(joinNetworkID.Text);
  152. }
  153. }
  154. private void OnNetworkEntered(object sender, TextCompositionEventArgs e)
  155. {
  156. e.Handled = !charRegex.IsMatch(e.Text);
  157. }
  158. private void OnPaste(object sender, DataObjectPastingEventArgs e)
  159. {
  160. var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
  161. if (!isText) return;
  162. var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
  163. if (!wholeStringRegex.IsMatch(text))
  164. {
  165. e.CancelCommand();
  166. }
  167. }
  168. }
  169. }