mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-01-31 00:23:58 +00:00
Windows UI will attempt to re-copy the auth token if a 401 error is received
This commit is contained in:
parent
03b48a4ad4
commit
0c69fc719f
@ -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,6 +159,8 @@ namespace WinUI
|
||||
try
|
||||
{
|
||||
var httpResponse = (HttpWebResponse)request.GetResponse();
|
||||
if (httpResponse.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
|
||||
{
|
||||
var responseText = streamReader.ReadToEnd();
|
||||
@ -161,15 +177,27 @@ namespace WinUI
|
||||
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)
|
||||
{
|
||||
if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
APIHandler.initHandler(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
cb(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -188,6 +216,9 @@ namespace WinUI
|
||||
try
|
||||
{
|
||||
var httpResponse = (HttpWebResponse)request.GetResponse();
|
||||
|
||||
if (httpResponse.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
|
||||
{
|
||||
var responseText = streamReader.ReadToEnd();
|
||||
@ -209,15 +240,27 @@ namespace WinUI
|
||||
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)
|
||||
{
|
||||
if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
APIHandler.initHandler(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
cb(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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,8 +308,12 @@ namespace WinUI
|
||||
{
|
||||
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.");
|
||||
}
|
||||
}
|
||||
@ -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,8 +346,12 @@ namespace WinUI
|
||||
{
|
||||
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.");
|
||||
}
|
||||
catch
|
||||
@ -317,6 +376,8 @@ namespace WinUI
|
||||
try
|
||||
{
|
||||
var httpResponse = (HttpWebResponse)request.GetResponse();
|
||||
if (httpResponse.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
|
||||
{
|
||||
var responseText = streamReader.ReadToEnd();
|
||||
@ -333,14 +394,26 @@ namespace WinUI
|
||||
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)
|
||||
{
|
||||
if (((HttpWebResponse)e.Response).StatusCode == HttpStatusCode.Unauthorized)
|
||||
{
|
||||
APIHandler.initHandler(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
cb(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user