Windows UI will attempt to re-copy the auth token if a 401 error is received

This commit is contained in:
Grant Limberg 2017-03-22 10:48:42 -07:00
parent 03b48a4ad4
commit 0c69fc719f

View File

@ -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 localZtDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\ZeroTier\\One";
String globalZtDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\ZeroTier\\One"; String globalZtDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\ZeroTier\\One";
@ -55,6 +55,20 @@ namespace WinUI
String authToken = ""; String authToken = "";
Int32 port = 9993; 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")) if (!File.Exists(localZtDir + "\\authtoken.secret") || !File.Exists(localZtDir + "\\zerotier-one.port"))
{ {
// launch external process to copy file into place // launch external process to copy file into place
@ -127,7 +141,7 @@ namespace WinUI
public APIHandler(int port, string authtoken) public APIHandler(int port, string authtoken)
{ {
url = "http://localhost:" + port; url = "http://127.0.0.1:" + port;
this.authtoken = authtoken; this.authtoken = authtoken;
} }
@ -145,6 +159,8 @@ namespace WinUI
try try
{ {
var httpResponse = (HttpWebResponse)request.GetResponse(); var httpResponse = (HttpWebResponse)request.GetResponse();
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{ {
var responseText = streamReader.ReadToEnd(); var responseText = streamReader.ReadToEnd();
@ -161,15 +177,27 @@ namespace WinUI
cb(status); cb(status);
} }
} }
else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
{
APIHandler.initHandler(true);
}
}
catch (System.Net.Sockets.SocketException) catch (System.Net.Sockets.SocketException)
{ {
cb(null); cb(null);
} }
catch (System.Net.WebException) catch (System.Net.WebException e)
{
if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
{
APIHandler.initHandler(true);
}
else
{ {
cb(null); cb(null);
} }
} }
}
@ -188,6 +216,9 @@ namespace WinUI
try try
{ {
var httpResponse = (HttpWebResponse)request.GetResponse(); var httpResponse = (HttpWebResponse)request.GetResponse();
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{ {
var responseText = streamReader.ReadToEnd(); var responseText = streamReader.ReadToEnd();
@ -209,15 +240,27 @@ namespace WinUI
cb(networkList); cb(networkList);
} }
} }
else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
{
APIHandler.initHandler(true);
}
}
catch (System.Net.Sockets.SocketException) catch (System.Net.Sockets.SocketException)
{ {
cb(null); cb(null);
} }
catch (System.Net.WebException) catch (System.Net.WebException e)
{
if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
{
APIHandler.initHandler(true);
}
else
{ {
cb(null); cb(null);
} }
} }
}
public void JoinNetwork(string nwid, bool allowManaged = true, bool allowGlobal = false, bool allowDefault = false) public void JoinNetwork(string nwid, bool allowManaged = true, bool allowGlobal = false, bool allowDefault = false)
{ {
@ -252,7 +295,11 @@ namespace WinUI
{ {
var httpResponse = (HttpWebResponse)request.GetResponse(); 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"); Console.WriteLine("Error sending join network message");
} }
@ -261,8 +308,12 @@ namespace WinUI
{ {
MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service."); MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
} }
catch (System.Net.WebException) catch (System.Net.WebException e)
{ {
if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
{
APIHandler.initHandler(true);
}
MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service."); MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
} }
} }
@ -282,7 +333,11 @@ namespace WinUI
{ {
var httpResponse = (HttpWebResponse)request.GetResponse(); 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"); Console.WriteLine("Error sending leave network message");
} }
@ -291,8 +346,12 @@ namespace WinUI
{ {
MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service."); MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
} }
catch (System.Net.WebException) catch (System.Net.WebException e)
{ {
if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
{
APIHandler.initHandler(true);
}
MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service."); MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
} }
catch catch
@ -317,6 +376,8 @@ namespace WinUI
try try
{ {
var httpResponse = (HttpWebResponse)request.GetResponse(); var httpResponse = (HttpWebResponse)request.GetResponse();
if (httpResponse.StatusCode == HttpStatusCode.OK)
{
using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{ {
var responseText = streamReader.ReadToEnd(); var responseText = streamReader.ReadToEnd();
@ -333,14 +394,26 @@ namespace WinUI
cb(peerList); cb(peerList);
} }
} }
else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
{
APIHandler.initHandler(true);
}
}
catch (System.Net.Sockets.SocketException) catch (System.Net.Sockets.SocketException)
{ {
cb(null); cb(null);
} }
catch (System.Net.WebException) catch (System.Net.WebException e)
{
if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
{
APIHandler.initHandler(true);
}
else
{ {
cb(null); cb(null);
} }
} }
} }
}
} }