summaryrefslogtreecommitdiff
path: root/windows/WinUI/APIHandler.cs
diff options
context:
space:
mode:
authorGrant Limberg <grant.limberg@zerotier.com>2017-03-22 10:48:42 -0700
committerGrant Limberg <grant.limberg@zerotier.com>2017-03-22 10:48:42 -0700
commit0c69fc719f692141b0c3d41a100de7cb253c2ef7 (patch)
tree216e238ab6e7e9d2971d228efb29ab538658b0bb /windows/WinUI/APIHandler.cs
parent03b48a4ad4894b6dff5220e32d21bdce1b762f56 (diff)
downloadinfinitytier-0c69fc719f692141b0c3d41a100de7cb253c2ef7.tar.gz
infinitytier-0c69fc719f692141b0c3d41a100de7cb253c2ef7.zip
Windows UI will attempt to re-copy the auth token if a 401 error is received
Diffstat (limited to 'windows/WinUI/APIHandler.cs')
-rw-r--r--windows/WinUI/APIHandler.cs199
1 files changed, 136 insertions, 63 deletions
diff --git a/windows/WinUI/APIHandler.cs b/windows/WinUI/APIHandler.cs
index 81c5b775..419a11cd 100644
--- a/windows/WinUI/APIHandler.cs
+++ b/windows/WinUI/APIHandler.cs
@@ -47,7 +47,7 @@ namespace WinUI
}
}
- private static bool initHandler()
+ private static bool initHandler(bool resetToken = false)
{
String localZtDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\ZeroTier\\One";
String globalZtDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\ZeroTier\\One";
@@ -55,6 +55,20 @@ namespace WinUI
String authToken = "";
Int32 port = 9993;
+ if (resetToken)
+ {
+ instance = null;
+ if (File.Exists(localZtDir + "\\authtoken.secret"))
+ {
+ File.Delete(localZtDir + "\\authtoken.secret");
+ }
+
+ if (File.Exists(localZtDir + "\\zerotier-one.port"))
+ {
+ File.Delete(localZtDir + "\\zerotier-one.port");
+ }
+ }
+
if (!File.Exists(localZtDir + "\\authtoken.secret") || !File.Exists(localZtDir + "\\zerotier-one.port"))
{
// launch external process to copy file into place
@@ -127,7 +141,7 @@ namespace WinUI
public APIHandler(int port, string authtoken)
{
- url = "http://localhost:" + port;
+ url = "http://127.0.0.1:" + port;
this.authtoken = authtoken;
}
@@ -145,29 +159,43 @@ namespace WinUI
try
{
var httpResponse = (HttpWebResponse)request.GetResponse();
- using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
- {
- var responseText = streamReader.ReadToEnd();
-
- ZeroTierStatus status = null;
- try
- {
- status = JsonConvert.DeserializeObject<ZeroTierStatus>(responseText);
- }
- catch (JsonReaderException e)
- {
- Console.WriteLine(e.ToString());
- }
- cb(status);
- }
+ if (httpResponse.StatusCode == HttpStatusCode.OK)
+ {
+ using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
+ {
+ var responseText = streamReader.ReadToEnd();
+
+ ZeroTierStatus status = null;
+ try
+ {
+ status = JsonConvert.DeserializeObject<ZeroTierStatus>(responseText);
+ }
+ catch (JsonReaderException e)
+ {
+ Console.WriteLine(e.ToString());
+ }
+ cb(status);
+ }
+ }
+ else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
+ {
+ APIHandler.initHandler(true);
+ }
}
catch (System.Net.Sockets.SocketException)
{
cb(null);
}
- catch (System.Net.WebException)
+ catch (System.Net.WebException e)
{
- cb(null);
+ if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
+ {
+ APIHandler.initHandler(true);
+ }
+ else
+ {
+ cb(null);
+ }
}
}
@@ -188,34 +216,49 @@ namespace WinUI
try
{
var httpResponse = (HttpWebResponse)request.GetResponse();
- using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
- {
- var responseText = streamReader.ReadToEnd();
- List<ZeroTierNetwork> networkList = null;
- try
- {
- networkList = JsonConvert.DeserializeObject<List<ZeroTierNetwork>>(responseText);
- foreach (ZeroTierNetwork n in networkList)
- {
- // all networks received via JSON are connected by definition
- n.IsConnected = true;
- }
- }
- catch (JsonReaderException e)
- {
- Console.WriteLine(e.ToString());
- }
- cb(networkList);
- }
+ if (httpResponse.StatusCode == HttpStatusCode.OK)
+ {
+ using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
+ {
+ var responseText = streamReader.ReadToEnd();
+
+ List<ZeroTierNetwork> networkList = null;
+ try
+ {
+ networkList = JsonConvert.DeserializeObject<List<ZeroTierNetwork>>(responseText);
+ foreach (ZeroTierNetwork n in networkList)
+ {
+ // all networks received via JSON are connected by definition
+ n.IsConnected = true;
+ }
+ }
+ catch (JsonReaderException e)
+ {
+ Console.WriteLine(e.ToString());
+ }
+ cb(networkList);
+ }
+ }
+ else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
+ {
+ APIHandler.initHandler(true);
+ }
}
catch (System.Net.Sockets.SocketException)
{
cb(null);
}
- catch (System.Net.WebException)
+ catch (System.Net.WebException e)
{
- cb(null);
+ if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
+ {
+ APIHandler.initHandler(true);
+ }
+ else
+ {
+ cb(null);
+ }
}
}
@@ -252,7 +295,11 @@ namespace WinUI
{
var httpResponse = (HttpWebResponse)request.GetResponse();
- if (httpResponse.StatusCode != HttpStatusCode.OK)
+ if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
+ {
+ APIHandler.initHandler(true);
+ }
+ else if (httpResponse.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine("Error sending join network message");
}
@@ -261,9 +308,13 @@ namespace WinUI
{
MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
}
- catch (System.Net.WebException)
+ catch (System.Net.WebException e)
{
- MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
+ if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
+ {
+ APIHandler.initHandler(true);
+ }
+ MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
}
}
@@ -282,7 +333,11 @@ namespace WinUI
{
var httpResponse = (HttpWebResponse)request.GetResponse();
- if (httpResponse.StatusCode != HttpStatusCode.OK)
+ if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
+ {
+ APIHandler.initHandler(true);
+ }
+ else if (httpResponse.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine("Error sending leave network message");
}
@@ -291,9 +346,13 @@ namespace WinUI
{
MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
}
- catch (System.Net.WebException)
+ catch (System.Net.WebException e)
{
- MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
+ if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
+ {
+ APIHandler.initHandler(true);
+ }
+ MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
}
catch
{
@@ -317,29 +376,43 @@ namespace WinUI
try
{
var httpResponse = (HttpWebResponse)request.GetResponse();
- using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
- {
- var responseText = streamReader.ReadToEnd();
- //Console.WriteLine(responseText);
- List<ZeroTierPeer> peerList = null;
- try
- {
- peerList = JsonConvert.DeserializeObject<List<ZeroTierPeer>>(responseText);
- }
- catch (JsonReaderException e)
- {
- Console.WriteLine(e.ToString());
- }
- cb(peerList);
- }
+ if (httpResponse.StatusCode == HttpStatusCode.OK)
+ {
+ using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
+ {
+ var responseText = streamReader.ReadToEnd();
+ //Console.WriteLine(responseText);
+ List<ZeroTierPeer> peerList = null;
+ try
+ {
+ peerList = JsonConvert.DeserializeObject<List<ZeroTierPeer>>(responseText);
+ }
+ catch (JsonReaderException e)
+ {
+ Console.WriteLine(e.ToString());
+ }
+ cb(peerList);
+ }
+ }
+ else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
+ {
+ APIHandler.initHandler(true);
+ }
}
catch (System.Net.Sockets.SocketException)
{
cb(null);
}
- catch (System.Net.WebException)
+ catch (System.Net.WebException e)
{
- cb(null);
+ if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
+ {
+ APIHandler.initHandler(true);
+ }
+ else
+ {
+ cb(null);
+ }
}
}
}