APIHandler.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. namespace WinUI
  11. {
  12. public class APIHandler
  13. {
  14. private string authtoken;
  15. private string url = null;
  16. public APIHandler()
  17. {
  18. url = "http://127.0.0.1:9993";
  19. }
  20. public APIHandler(int port, string authtoken)
  21. {
  22. url = "http://localhost:" + port;
  23. this.authtoken = authtoken;
  24. }
  25. public ZeroTierStatus GetStatus()
  26. {
  27. var request = WebRequest.Create(url + "/status" + "?auth=" + authtoken) as HttpWebRequest;
  28. if (request != null)
  29. {
  30. request.Method = "GET";
  31. request.ContentType = "application/json";
  32. }
  33. try
  34. {
  35. var httpResponse = (HttpWebResponse)request.GetResponse();
  36. using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  37. {
  38. var responseText = streamReader.ReadToEnd();
  39. ZeroTierStatus status = null;
  40. try
  41. {
  42. status = JsonConvert.DeserializeObject<ZeroTierStatus>(responseText);
  43. }
  44. catch (JsonReaderException e)
  45. {
  46. Console.WriteLine(e.ToString());
  47. }
  48. return status;
  49. }
  50. }
  51. catch (System.Net.Sockets.SocketException)
  52. {
  53. return null;
  54. }
  55. catch (System.Net.WebException)
  56. {
  57. return null;
  58. }
  59. }
  60. public List<ZeroTierNetwork> GetNetworks()
  61. {
  62. var request = WebRequest.Create(url + "/network" + "?auth=" + authtoken) as HttpWebRequest;
  63. if (request == null)
  64. {
  65. return null;
  66. }
  67. request.Method = "GET";
  68. request.ContentType = "application/json";
  69. try
  70. {
  71. var httpResponse = (HttpWebResponse)request.GetResponse();
  72. using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  73. {
  74. var responseText = streamReader.ReadToEnd();
  75. List<ZeroTierNetwork> networkList = null;
  76. try
  77. {
  78. networkList = JsonConvert.DeserializeObject<List<ZeroTierNetwork>>(responseText);
  79. }
  80. catch (JsonReaderException e)
  81. {
  82. Console.WriteLine(e.ToString());
  83. }
  84. return networkList;
  85. }
  86. }
  87. catch (System.Net.Sockets.SocketException)
  88. {
  89. return null;
  90. }
  91. catch (System.Net.WebException)
  92. {
  93. return null;
  94. }
  95. }
  96. public void JoinNetwork(string nwid)
  97. {
  98. var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest;
  99. if (request == null)
  100. {
  101. return;
  102. }
  103. request.Method = "POST";
  104. try
  105. {
  106. var httpResponse = (HttpWebResponse)request.GetResponse();
  107. if (httpResponse.StatusCode != HttpStatusCode.OK)
  108. {
  109. Console.WriteLine("Error sending join network message");
  110. }
  111. }
  112. catch (System.Net.Sockets.SocketException)
  113. {
  114. MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
  115. }
  116. catch (System.Net.WebException)
  117. {
  118. MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
  119. }
  120. }
  121. public void LeaveNetwork(string nwid)
  122. {
  123. var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest;
  124. if (request == null)
  125. {
  126. return;
  127. }
  128. request.Method = "DELETE";
  129. try
  130. {
  131. var httpResponse = (HttpWebResponse)request.GetResponse();
  132. if (httpResponse.StatusCode != HttpStatusCode.OK)
  133. {
  134. Console.WriteLine("Error sending leave network message");
  135. }
  136. }
  137. catch (System.Net.Sockets.SocketException)
  138. {
  139. MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
  140. }
  141. catch (System.Net.WebException)
  142. {
  143. MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
  144. }
  145. }
  146. public List<ZeroTierPeer> GetPeers()
  147. {
  148. var request = WebRequest.Create(url + "/peer" + "?auth=" + authtoken) as HttpWebRequest;
  149. if (request == null)
  150. {
  151. return null;
  152. }
  153. request.Method = "GET";
  154. request.ContentType = "application/json";
  155. try
  156. {
  157. var httpResponse = (HttpWebResponse)request.GetResponse();
  158. using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
  159. {
  160. var responseText = streamReader.ReadToEnd();
  161. //Console.WriteLine(responseText);
  162. List<ZeroTierPeer> peerList = null;
  163. try
  164. {
  165. peerList = JsonConvert.DeserializeObject<List<ZeroTierPeer>>(responseText);
  166. }
  167. catch (JsonReaderException e)
  168. {
  169. Console.WriteLine(e.ToString());
  170. }
  171. return peerList;
  172. }
  173. }
  174. catch (System.Net.Sockets.SocketException)
  175. {
  176. return null;
  177. }
  178. catch (System.Net.WebException)
  179. {
  180. return null;
  181. }
  182. }
  183. }
  184. }