APIHandler.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Net;
  7. using System.IO;
  8. using System.Windows;
  9. using Newtonsoft.Json;
  10. using System.Diagnostics;
  11. namespace WinUI
  12. {
  13. public class APIHandler
  14. {
  15. private string authtoken;
  16. private string url = null;
  17. private static APIHandler instance;
  18. public static APIHandler Instance
  19. {
  20. get
  21. {
  22. if (instance == null)
  23. {
  24. }
  25. return instance;
  26. }
  27. }
  28. private static bool initHandler()
  29. {
  30. String localZtDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\ZeroTier\\One";
  31. String globalZtDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\ZeroTier\\One";
  32. String authToken = "";
  33. Int32 port = 9993;
  34. if (!File.Exists(localZtDir + "\\authtoken.secret") || !File.Exists(localZtDir + "\\zerotier-one.port"))
  35. {
  36. // launch external process to copy file into place
  37. String curPath = System.Reflection.Assembly.GetEntryAssembly().Location;
  38. int index = curPath.LastIndexOf("\\");
  39. curPath = curPath.Substring(0, index);
  40. ProcessStartInfo startInfo = new ProcessStartInfo(curPath + "\\copyutil.exe", globalZtDir + " " + localZtDir);
  41. startInfo.Verb = "runas";
  42. var process = Process.Start(startInfo);
  43. process.WaitForExit();
  44. }
  45. authToken = readAuthToken(localZtDir + "\\authtoken.secret");
  46. if ((authToken == null) || (authToken.Length <= 0))
  47. {
  48. MessageBox.Show("Unable to read ZeroTier One authtoken", "ZeroTier One");
  49. return false;
  50. }
  51. port = readPort(localZtDir + "\\zerotier-one.port");
  52. instance = new APIHandler(port, authToken);
  53. return true;
  54. }
  55. private static String readAuthToken(String path)
  56. {
  57. String authToken = "";
  58. if (File.Exists(path))
  59. {
  60. try
  61. {
  62. byte[] tmp = File.ReadAllBytes(path);
  63. authToken = System.Text.Encoding.UTF8.GetString(tmp).Trim();
  64. }
  65. catch
  66. {
  67. MessageBox.Show("Unable to read ZeroTier One Auth Token from:\r\n" + path, "ZeroTier One");
  68. }
  69. }
  70. return authToken;
  71. }
  72. private static Int32 readPort(String path)
  73. {
  74. Int32 port = 9993;
  75. try
  76. {
  77. byte[] tmp = File.ReadAllBytes(path);
  78. port = Int32.Parse(System.Text.Encoding.ASCII.GetString(tmp).Trim());
  79. if ((port <= 0) || (port > 65535))
  80. port = 9993;
  81. }
  82. catch
  83. {
  84. }
  85. return port;
  86. }
  87. private APIHandler()
  88. {
  89. url = "http://127.0.0.1:9993";
  90. }
  91. public APIHandler(int port, string authtoken)
  92. {
  93. url = "http://localhost:" + port;
  94. this.authtoken = authtoken;
  95. }
  96. public ZeroTierStatus GetStatus()
  97. {
  98. var request = WebRequest.Create(url + "/status" + "?auth=" + authtoken) as HttpWebRequest;
  99. if (request != null)
  100. {
  101. request.Method = "GET";
  102. request.ContentType = "application/json";
  103. }
  104. try
  105. {
  106. var httpResponse = (HttpWebResponse)request.GetResponse();
  107. using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  108. {
  109. var responseText = streamReader.ReadToEnd();
  110. ZeroTierStatus status = null;
  111. try
  112. {
  113. status = JsonConvert.DeserializeObject<ZeroTierStatus>(responseText);
  114. }
  115. catch (JsonReaderException e)
  116. {
  117. Console.WriteLine(e.ToString());
  118. }
  119. return status;
  120. }
  121. }
  122. catch (System.Net.Sockets.SocketException)
  123. {
  124. return null;
  125. }
  126. catch (System.Net.WebException)
  127. {
  128. return null;
  129. }
  130. }
  131. public List<ZeroTierNetwork> GetNetworks()
  132. {
  133. var request = WebRequest.Create(url + "/network" + "?auth=" + authtoken) as HttpWebRequest;
  134. if (request == null)
  135. {
  136. return null;
  137. }
  138. request.Method = "GET";
  139. request.ContentType = "application/json";
  140. try
  141. {
  142. var httpResponse = (HttpWebResponse)request.GetResponse();
  143. using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  144. {
  145. var responseText = streamReader.ReadToEnd();
  146. List<ZeroTierNetwork> networkList = null;
  147. try
  148. {
  149. networkList = JsonConvert.DeserializeObject<List<ZeroTierNetwork>>(responseText);
  150. }
  151. catch (JsonReaderException e)
  152. {
  153. Console.WriteLine(e.ToString());
  154. }
  155. return networkList;
  156. }
  157. }
  158. catch (System.Net.Sockets.SocketException)
  159. {
  160. return null;
  161. }
  162. catch (System.Net.WebException)
  163. {
  164. return null;
  165. }
  166. }
  167. public void JoinNetwork(string nwid, bool allowManaged = true, bool allowGlobal = false, bool allowDefault = false)
  168. {
  169. var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest;
  170. if (request == null)
  171. {
  172. return;
  173. }
  174. request.Method = "POST";
  175. request.ContentType = "applicaiton/json";
  176. using (var streamWriter = new StreamWriter(((HttpWebRequest)request).GetRequestStream()))
  177. {
  178. string json = "{\"allowManaged\":" + (allowManaged ? "true" : "false") + "," +
  179. "\"allowGlobal\":" + (allowGlobal ? "true" : "false") + "," +
  180. "\"allowDefault\":" + (allowDefault ? "true" : "false") + "}";
  181. streamWriter.Write(json);
  182. streamWriter.Flush();
  183. streamWriter.Close();
  184. }
  185. try
  186. {
  187. var httpResponse = (HttpWebResponse)request.GetResponse();
  188. if (httpResponse.StatusCode != HttpStatusCode.OK)
  189. {
  190. Console.WriteLine("Error sending join network message");
  191. }
  192. }
  193. catch (System.Net.Sockets.SocketException)
  194. {
  195. MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
  196. }
  197. catch (System.Net.WebException)
  198. {
  199. MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
  200. }
  201. }
  202. public void LeaveNetwork(string nwid)
  203. {
  204. var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest;
  205. if (request == null)
  206. {
  207. return;
  208. }
  209. request.Method = "DELETE";
  210. try
  211. {
  212. var httpResponse = (HttpWebResponse)request.GetResponse();
  213. if (httpResponse.StatusCode != HttpStatusCode.OK)
  214. {
  215. Console.WriteLine("Error sending leave network message");
  216. }
  217. }
  218. catch (System.Net.Sockets.SocketException)
  219. {
  220. MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
  221. }
  222. catch (System.Net.WebException)
  223. {
  224. MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
  225. }
  226. }
  227. public List<ZeroTierPeer> GetPeers()
  228. {
  229. var request = WebRequest.Create(url + "/peer" + "?auth=" + authtoken) as HttpWebRequest;
  230. if (request == null)
  231. {
  232. return null;
  233. }
  234. request.Method = "GET";
  235. request.ContentType = "application/json";
  236. try
  237. {
  238. var httpResponse = (HttpWebResponse)request.GetResponse();
  239. using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  240. {
  241. var responseText = streamReader.ReadToEnd();
  242. //Console.WriteLine(responseText);
  243. List<ZeroTierPeer> peerList = null;
  244. try
  245. {
  246. peerList = JsonConvert.DeserializeObject<List<ZeroTierPeer>>(responseText);
  247. }
  248. catch (JsonReaderException e)
  249. {
  250. Console.WriteLine(e.ToString());
  251. }
  252. return peerList;
  253. }
  254. }
  255. catch (System.Net.Sockets.SocketException)
  256. {
  257. return null;
  258. }
  259. catch (System.Net.WebException)
  260. {
  261. return null;
  262. }
  263. }
  264. }
  265. }