mirror of
https://github.com/zerotier/ZeroTierOne.git
synced 2025-06-06 12:33:44 +02:00
Run Join/Leave commands asynchronously so the UI doesn't lock up on thoise commands
This commit is contained in:
parent
b3dd5c0e3a
commit
73f7088b45
4 changed files with 112 additions and 90 deletions
|
@ -8,6 +8,7 @@ using System.IO;
|
||||||
using System.Windows;
|
using System.Windows;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
|
using System.Windows.Threading;
|
||||||
|
|
||||||
namespace WinUI
|
namespace WinUI
|
||||||
{
|
{
|
||||||
|
@ -264,104 +265,125 @@ namespace WinUI
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void JoinNetwork(string nwid, bool allowManaged = true, bool allowGlobal = false, bool allowDefault = false)
|
public void JoinNetwork(Dispatcher d, string nwid, bool allowManaged = true, bool allowGlobal = false, bool allowDefault = false)
|
||||||
{
|
{
|
||||||
var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest;
|
Task.Factory.StartNew(() =>
|
||||||
if (request == null)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
request.Method = "POST";
|
|
||||||
request.ContentType = "applicaiton/json";
|
|
||||||
request.Timeout = 10000;
|
|
||||||
try
|
|
||||||
{
|
{
|
||||||
using (var streamWriter = new StreamWriter(((HttpWebRequest)request).GetRequestStream()))
|
var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest;
|
||||||
|
if (request == null)
|
||||||
{
|
{
|
||||||
string json = "{\"allowManaged\":" + (allowManaged ? "true" : "false") + "," +
|
return;
|
||||||
"\"allowGlobal\":" + (allowGlobal ? "true" : "false") + "," +
|
|
||||||
"\"allowDefault\":" + (allowDefault ? "true" : "false") + "}";
|
|
||||||
streamWriter.Write(json);
|
|
||||||
streamWriter.Flush();
|
|
||||||
streamWriter.Close();
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch (System.Net.WebException)
|
|
||||||
{
|
|
||||||
MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
request.Method = "POST";
|
||||||
{
|
request.ContentType = "applicaiton/json";
|
||||||
var httpResponse = (HttpWebResponse)request.GetResponse();
|
request.Timeout = 30000;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
using (var streamWriter = new StreamWriter(((HttpWebRequest)request).GetRequestStream()))
|
||||||
|
{
|
||||||
|
string json = "{\"allowManaged\":" + (allowManaged ? "true" : "false") + "," +
|
||||||
|
"\"allowGlobal\":" + (allowGlobal ? "true" : "false") + "," +
|
||||||
|
"\"allowDefault\":" + (allowDefault ? "true" : "false") + "}";
|
||||||
|
streamWriter.Write(json);
|
||||||
|
streamWriter.Flush();
|
||||||
|
streamWriter.Close();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (System.Net.WebException)
|
||||||
|
{
|
||||||
|
d.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
||||||
|
{
|
||||||
|
MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
|
||||||
|
}));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
|
try
|
||||||
{
|
{
|
||||||
APIHandler.initHandler(true);
|
var httpResponse = (HttpWebResponse)request.GetResponse();
|
||||||
|
|
||||||
|
if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
|
||||||
|
{
|
||||||
|
APIHandler.initHandler(true);
|
||||||
|
}
|
||||||
|
else if (httpResponse.StatusCode != HttpStatusCode.OK)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Error sending join network message");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (httpResponse.StatusCode != HttpStatusCode.OK)
|
catch (System.Net.Sockets.SocketException)
|
||||||
{
|
|
||||||
Console.WriteLine("Error sending join network message");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
catch (System.Net.Sockets.SocketException)
|
|
||||||
{
|
|
||||||
MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
|
|
||||||
}
|
|
||||||
catch (System.Net.WebException e)
|
|
||||||
{
|
|
||||||
HttpWebResponse res = (HttpWebResponse)e.Response;
|
|
||||||
if (res != null && res.StatusCode == HttpStatusCode.Unauthorized)
|
|
||||||
{
|
{
|
||||||
APIHandler.initHandler(true);
|
d.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
||||||
|
{
|
||||||
|
MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
|
||||||
|
}));
|
||||||
}
|
}
|
||||||
MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
|
catch (System.Net.WebException e)
|
||||||
}
|
{
|
||||||
|
HttpWebResponse res = (HttpWebResponse)e.Response;
|
||||||
|
if (res != null && res.StatusCode == HttpStatusCode.Unauthorized)
|
||||||
|
{
|
||||||
|
APIHandler.initHandler(true);
|
||||||
|
}
|
||||||
|
d.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
||||||
|
{
|
||||||
|
MessageBox.Show("Error Joining Network: Cannot connect to ZeroTier service.");
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public void LeaveNetwork(string nwid)
|
public void LeaveNetwork(Dispatcher d, string nwid)
|
||||||
{
|
{
|
||||||
var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest;
|
Task.Factory.StartNew(() =>
|
||||||
if (request == null)
|
{
|
||||||
{
|
var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest;
|
||||||
return;
|
if (request == null)
|
||||||
}
|
|
||||||
|
|
||||||
request.Method = "DELETE";
|
|
||||||
request.Timeout = 10000;
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
var httpResponse = (HttpWebResponse)request.GetResponse();
|
|
||||||
|
|
||||||
if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
|
|
||||||
{
|
{
|
||||||
APIHandler.initHandler(true);
|
return;
|
||||||
}
|
}
|
||||||
else if (httpResponse.StatusCode != HttpStatusCode.OK)
|
|
||||||
{
|
request.Method = "DELETE";
|
||||||
Console.WriteLine("Error sending leave network message");
|
request.Timeout = 30000;
|
||||||
}
|
|
||||||
}
|
try
|
||||||
catch (System.Net.Sockets.SocketException)
|
|
||||||
{
|
|
||||||
MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
|
|
||||||
}
|
|
||||||
catch (System.Net.WebException e)
|
|
||||||
{
|
|
||||||
HttpWebResponse res = (HttpWebResponse)e.Response;
|
|
||||||
if (res != null && res.StatusCode == HttpStatusCode.Unauthorized)
|
|
||||||
{
|
{
|
||||||
APIHandler.initHandler(true);
|
var httpResponse = (HttpWebResponse)request.GetResponse();
|
||||||
|
|
||||||
|
if (httpResponse.StatusCode == HttpStatusCode.Unauthorized)
|
||||||
|
{
|
||||||
|
APIHandler.initHandler(true);
|
||||||
|
}
|
||||||
|
else if (httpResponse.StatusCode != HttpStatusCode.OK)
|
||||||
|
{
|
||||||
|
Console.WriteLine("Error sending leave network message");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
|
catch (System.Net.Sockets.SocketException)
|
||||||
}
|
{
|
||||||
catch
|
d.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
||||||
{
|
{
|
||||||
Console.WriteLine("Error leaving network: Unknown error");
|
MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
|
||||||
}
|
}));
|
||||||
|
}
|
||||||
|
catch (System.Net.WebException e)
|
||||||
|
{
|
||||||
|
HttpWebResponse res = (HttpWebResponse)e.Response;
|
||||||
|
if (res != null && res.StatusCode == HttpStatusCode.Unauthorized)
|
||||||
|
{
|
||||||
|
APIHandler.initHandler(true);
|
||||||
|
}
|
||||||
|
d.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
||||||
|
{
|
||||||
|
MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
Console.WriteLine("Error leaving network: Unknown error");
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public delegate void PeersCallback(List<ZeroTierPeer> peers);
|
public delegate void PeersCallback(List<ZeroTierPeer> peers);
|
||||||
|
|
|
@ -118,7 +118,7 @@ namespace WinUI
|
||||||
bool allowGlobal = allowGlobalCheckbox.IsChecked.Value;
|
bool allowGlobal = allowGlobalCheckbox.IsChecked.Value;
|
||||||
bool allowManaged = allowManagedCheckbox.IsChecked.Value;
|
bool allowManaged = allowManagedCheckbox.IsChecked.Value;
|
||||||
|
|
||||||
APIHandler.Instance.JoinNetwork(joinNetworkBox.Text, allowManaged, allowGlobal, allowDefault);
|
APIHandler.Instance.JoinNetwork(this.Dispatcher, joinNetworkBox.Text, allowManaged, allowGlobal, allowDefault);
|
||||||
|
|
||||||
Close();
|
Close();
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,14 +106,14 @@ namespace WinUI
|
||||||
|
|
||||||
private void deleteButton_Click(object sender, RoutedEventArgs e)
|
private void deleteButton_Click(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
APIHandler.Instance.LeaveNetwork(network.NetworkId);
|
APIHandler.Instance.LeaveNetwork(this.Dispatcher, network.NetworkId);
|
||||||
NetworkMonitor.Instance.RemoveNetwork(network.NetworkId);
|
NetworkMonitor.Instance.RemoveNetwork(network.NetworkId);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void AllowManaged_CheckStateChanged(object sender, RoutedEventArgs e)
|
private void AllowManaged_CheckStateChanged(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
CheckBox cb = sender as CheckBox;
|
CheckBox cb = sender as CheckBox;
|
||||||
APIHandler.Instance.JoinNetwork(network.NetworkId,
|
APIHandler.Instance.JoinNetwork(this.Dispatcher, network.NetworkId,
|
||||||
allowManaged.IsChecked ?? false,
|
allowManaged.IsChecked ?? false,
|
||||||
allowGlobal.IsChecked ?? false,
|
allowGlobal.IsChecked ?? false,
|
||||||
allowDefault.IsChecked ?? false);
|
allowDefault.IsChecked ?? false);
|
||||||
|
@ -122,7 +122,7 @@ namespace WinUI
|
||||||
private void AllowGlobal_CheckStateChanged(object sender, RoutedEventArgs e)
|
private void AllowGlobal_CheckStateChanged(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
CheckBox cb = sender as CheckBox;
|
CheckBox cb = sender as CheckBox;
|
||||||
APIHandler.Instance.JoinNetwork(network.NetworkId,
|
APIHandler.Instance.JoinNetwork(this.Dispatcher, network.NetworkId,
|
||||||
allowManaged.IsChecked ?? false,
|
allowManaged.IsChecked ?? false,
|
||||||
allowGlobal.IsChecked ?? false,
|
allowGlobal.IsChecked ?? false,
|
||||||
allowDefault.IsChecked ?? false);
|
allowDefault.IsChecked ?? false);
|
||||||
|
@ -131,7 +131,7 @@ namespace WinUI
|
||||||
private void AllowDefault_CheckStateChanged(object sender, RoutedEventArgs e)
|
private void AllowDefault_CheckStateChanged(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
CheckBox cb = sender as CheckBox;
|
CheckBox cb = sender as CheckBox;
|
||||||
APIHandler.Instance.JoinNetwork(network.NetworkId,
|
APIHandler.Instance.JoinNetwork(this.Dispatcher, network.NetworkId,
|
||||||
allowManaged.IsChecked ?? false,
|
allowManaged.IsChecked ?? false,
|
||||||
allowGlobal.IsChecked ?? false,
|
allowGlobal.IsChecked ?? false,
|
||||||
allowDefault.IsChecked ?? false);
|
allowDefault.IsChecked ?? false);
|
||||||
|
@ -155,11 +155,11 @@ namespace WinUI
|
||||||
bool managed = allowManaged.IsChecked.Value;
|
bool managed = allowManaged.IsChecked.Value;
|
||||||
bool defRoute = allowDefault.IsChecked.Value;
|
bool defRoute = allowDefault.IsChecked.Value;
|
||||||
|
|
||||||
APIHandler.Instance.JoinNetwork(networkId.Text, managed, global, defRoute);
|
APIHandler.Instance.JoinNetwork(this.Dispatcher, networkId.Text, managed, global, defRoute);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
APIHandler.Instance.LeaveNetwork(networkId.Text);
|
APIHandler.Instance.LeaveNetwork(this.Dispatcher, networkId.Text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -269,11 +269,11 @@ namespace WinUI
|
||||||
ZeroTierNetwork network = item.DataContext as ZeroTierNetwork;
|
ZeroTierNetwork network = item.DataContext as ZeroTierNetwork;
|
||||||
if (item.IsChecked)
|
if (item.IsChecked)
|
||||||
{
|
{
|
||||||
APIHandler.Instance.LeaveNetwork(network.NetworkId);
|
APIHandler.Instance.LeaveNetwork(this.Dispatcher, network.NetworkId);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
APIHandler.Instance.JoinNetwork(network.NetworkId, network.AllowManaged, network.AllowGlobal, network.AllowDefault);
|
APIHandler.Instance.JoinNetwork(this.Dispatcher, network.NetworkId, network.AllowManaged, network.AllowGlobal, network.AllowDefault);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue