MainWindow.xaml.cs 7.0 KB

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