From a4b19173618756e1d0cee3339e6cde734d13abb7 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Tue, 5 Feb 2019 11:47:37 -0800 Subject: Fixed a bug where UI elements were attempting to be updated in a background thread. Also code cleanup & removed confusing startup UI --- windows/WinUI/APIHandler.cs | 452 ++++++++++----------- windows/WinUI/NetworkMonitor.cs | 2 +- windows/WinUI/OnboardProcess/CreateAccount.xaml | 38 -- windows/WinUI/OnboardProcess/CreateAccount.xaml.cs | 66 --- windows/WinUI/OnboardProcess/CreateOrJoin.xaml | 49 --- windows/WinUI/OnboardProcess/CreateOrJoin.xaml.cs | 98 ----- windows/WinUI/OnboardProcess/EnterToken.xaml | 29 -- windows/WinUI/OnboardProcess/EnterToken.xaml.cs | 57 --- windows/WinUI/OnboardProcess/Finished.xaml | 27 -- windows/WinUI/OnboardProcess/Finished.xaml.cs | 37 -- windows/WinUI/OnboardProcess/LogIn.xaml | 35 -- windows/WinUI/OnboardProcess/LogIn.xaml.cs | 57 --- windows/WinUI/OnboardProcess/RegisterOrLogIn.xaml | 27 -- .../WinUI/OnboardProcess/RegisterOrLogIn.xaml.cs | 48 --- windows/WinUI/PageSwitcher.xaml | 13 - windows/WinUI/PageSwitcher.xaml.cs | 56 --- windows/WinUI/Switcher.cs | 24 -- windows/WinUI/ToolbarItem.xaml.cs | 59 +-- windows/WinUI/WinUI.csproj | 50 --- 19 files changed, 236 insertions(+), 988 deletions(-) delete mode 100644 windows/WinUI/OnboardProcess/CreateAccount.xaml delete mode 100644 windows/WinUI/OnboardProcess/CreateAccount.xaml.cs delete mode 100644 windows/WinUI/OnboardProcess/CreateOrJoin.xaml delete mode 100644 windows/WinUI/OnboardProcess/CreateOrJoin.xaml.cs delete mode 100644 windows/WinUI/OnboardProcess/EnterToken.xaml delete mode 100644 windows/WinUI/OnboardProcess/EnterToken.xaml.cs delete mode 100644 windows/WinUI/OnboardProcess/Finished.xaml delete mode 100644 windows/WinUI/OnboardProcess/Finished.xaml.cs delete mode 100644 windows/WinUI/OnboardProcess/LogIn.xaml delete mode 100644 windows/WinUI/OnboardProcess/LogIn.xaml.cs delete mode 100644 windows/WinUI/OnboardProcess/RegisterOrLogIn.xaml delete mode 100644 windows/WinUI/OnboardProcess/RegisterOrLogIn.xaml.cs delete mode 100644 windows/WinUI/PageSwitcher.xaml delete mode 100644 windows/WinUI/PageSwitcher.xaml.cs delete mode 100644 windows/WinUI/Switcher.cs (limited to 'windows') diff --git a/windows/WinUI/APIHandler.cs b/windows/WinUI/APIHandler.cs index 015415c3..6a944f0f 100644 --- a/windows/WinUI/APIHandler.cs +++ b/windows/WinUI/APIHandler.cs @@ -12,7 +12,7 @@ using System.Windows.Threading; namespace WinUI { - + public class APIHandler { @@ -58,19 +58,19 @@ namespace WinUI String authToken = ""; Int32 port = 9993; - if (resetToken) - { - instance = null; - if (File.Exists(localZtDir + "\\authtoken.secret")) - { - File.Delete(localZtDir + "\\authtoken.secret"); - } + 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 + "\\zerotier-one.port")) + { + File.Delete(localZtDir + "\\zerotier-one.port"); + } + } if (!File.Exists(localZtDir + "\\authtoken.secret") || !File.Exists(localZtDir + "\\zerotier-one.port")) { @@ -78,7 +78,7 @@ namespace WinUI String curPath = System.Reflection.Assembly.GetEntryAssembly().Location; int index = curPath.LastIndexOf("\\"); curPath = curPath.Substring(0, index); - ProcessStartInfo startInfo = new ProcessStartInfo(curPath + "\\copyutil.exe", "\""+globalZtDir+"\"" + " " + "\""+localZtDir+"\""); + ProcessStartInfo startInfo = new ProcessStartInfo(curPath + "\\copyutil.exe", "\"" + globalZtDir + "\"" + " " + "\"" + localZtDir + "\""); startInfo.Verb = "runas"; @@ -148,7 +148,7 @@ namespace WinUI this.authtoken = authtoken; } - + public void GetStatus(StatusCallback cb) { @@ -162,33 +162,33 @@ namespace WinUI try { var httpResponse = (HttpWebResponse)request.GetResponse(); - if (httpResponse.StatusCode == HttpStatusCode.OK) - { - using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) - { - var responseText = streamReader.ReadToEnd(); + if (httpResponse.StatusCode == HttpStatusCode.OK) + { + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) + { + var responseText = streamReader.ReadToEnd(); - ZeroTierStatus status = null; - try - { - status = JsonConvert.DeserializeObject(responseText); + ZeroTierStatus status = null; + try + { + status = JsonConvert.DeserializeObject(responseText); if (ZeroTierAddress != status.Address) { ZeroTierAddress = status.Address; } - } - catch (JsonReaderException e) - { - Console.WriteLine(e.ToString()); - } - cb(status); - } - } - else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized) - { - APIHandler.initHandler(true); - } + } + catch (JsonReaderException e) + { + Console.WriteLine(e.ToString()); + } + cb(status); + } + } + else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized) + { + APIHandler.initHandler(true); + } } catch (System.Net.Sockets.SocketException) { @@ -196,19 +196,19 @@ namespace WinUI } catch (System.Net.WebException e) { - HttpWebResponse res = (HttpWebResponse)e.Response; - if (res != null && res.StatusCode == HttpStatusCode.Unauthorized) - { - APIHandler.initHandler(true); - } - else - { - cb(null); - } + HttpWebResponse res = (HttpWebResponse)e.Response; + if (res != null && res.StatusCode == HttpStatusCode.Unauthorized) + { + APIHandler.initHandler(true); + } + else + { + cb(null); + } } } - + public void GetNetworks(NetworkListCallback cb) { @@ -226,33 +226,33 @@ namespace WinUI { var httpResponse = (HttpWebResponse)request.GetResponse(); - if (httpResponse.StatusCode == HttpStatusCode.OK) - { - using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) - { - var responseText = streamReader.ReadToEnd(); - - List networkList = null; - try - { - networkList = JsonConvert.DeserializeObject>(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); - } + if (httpResponse.StatusCode == HttpStatusCode.OK) + { + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) + { + var responseText = streamReader.ReadToEnd(); + + List networkList = null; + try + { + networkList = JsonConvert.DeserializeObject>(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) { @@ -260,137 +260,137 @@ namespace WinUI } catch (System.Net.WebException e) { - HttpWebResponse res = (HttpWebResponse)e.Response; - if (res != null && res.StatusCode == HttpStatusCode.Unauthorized) - { - APIHandler.initHandler(true); - } - else - { - cb(null); - } + HttpWebResponse res = (HttpWebResponse)e.Response; + if (res != null && res.StatusCode == HttpStatusCode.Unauthorized) + { + APIHandler.initHandler(true); + } + else + { + cb(null); + } } } public void JoinNetwork(Dispatcher d, string nwid, bool allowManaged = true, bool allowGlobal = false, bool allowDefault = false) { - Task.Factory.StartNew(() => - { - var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest; - if (request == null) - { - return; - } - - request.Method = "POST"; - request.ContentType = "applicaiton/json"; - 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; - } - - try - { - 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"); - } - } - catch (System.Net.Sockets.SocketException) - { - d.BeginInvoke(DispatcherPriority.Normal, new Action(() => - { - 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."); - })); - } - }); + Task.Factory.StartNew(() => + { + var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest; + if (request == null) + { + return; + } + + request.Method = "POST"; + request.ContentType = "applicaiton/json"; + 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; + } + + try + { + 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"); + } + } + catch (System.Net.Sockets.SocketException) + { + d.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + 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(Dispatcher d, string nwid) { - Task.Factory.StartNew(() => - { - var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest; - if (request == null) - { - return; - } - - request.Method = "DELETE"; - request.Timeout = 30000; - - try - { - 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"); - } - } - catch (System.Net.Sockets.SocketException) - { - d.BeginInvoke(DispatcherPriority.Normal, new Action(() => - { - 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"); - } - }); + Task.Factory.StartNew(() => + { + var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest; + if (request == null) + { + return; + } + + request.Method = "DELETE"; + request.Timeout = 30000; + + try + { + 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"); + } + } + catch (System.Net.Sockets.SocketException) + { + d.BeginInvoke(DispatcherPriority.Normal, new Action(() => + { + 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 peers); @@ -409,28 +409,28 @@ namespace WinUI try { var httpResponse = (HttpWebResponse)request.GetResponse(); - if (httpResponse.StatusCode == HttpStatusCode.OK) - { - using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) - { - var responseText = streamReader.ReadToEnd(); - //Console.WriteLine(responseText); - List peerList = null; - try - { - peerList = JsonConvert.DeserializeObject>(responseText); - } - catch (JsonReaderException e) - { - Console.WriteLine(e.ToString()); - } - cb(peerList); - } - } - else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized) - { - APIHandler.initHandler(true); - } + if (httpResponse.StatusCode == HttpStatusCode.OK) + { + using (var streamReader = new StreamReader(httpResponse.GetResponseStream())) + { + var responseText = streamReader.ReadToEnd(); + //Console.WriteLine(responseText); + List peerList = null; + try + { + peerList = JsonConvert.DeserializeObject>(responseText); + } + catch (JsonReaderException e) + { + Console.WriteLine(e.ToString()); + } + cb(peerList); + } + } + else if (httpResponse.StatusCode == HttpStatusCode.Unauthorized) + { + APIHandler.initHandler(true); + } } catch (System.Net.Sockets.SocketException) { @@ -438,15 +438,15 @@ namespace WinUI } catch (System.Net.WebException e) { - HttpWebResponse res = (HttpWebResponse)e.Response; - if (res != null && res.StatusCode == HttpStatusCode.Unauthorized) - { - APIHandler.initHandler(true); - } - else - { - cb(null); - } + HttpWebResponse res = (HttpWebResponse)e.Response; + if (res != null && res.StatusCode == HttpStatusCode.Unauthorized) + { + APIHandler.initHandler(true); + } + else + { + cb(null); + } } } diff --git a/windows/WinUI/NetworkMonitor.cs b/windows/WinUI/NetworkMonitor.cs index ce722e45..927f804c 100644 --- a/windows/WinUI/NetworkMonitor.cs +++ b/windows/WinUI/NetworkMonitor.cs @@ -156,7 +156,7 @@ namespace WinUI { Console.WriteLine("Monitor Thread Exception: " + "\n" + e.StackTrace); } - Console.WriteLine("Monitor Thread Ended"); + Console.WriteLine("Monitor Thread Ended"); } public void SubscribeStatusUpdates(StatusCallback cb) diff --git a/windows/WinUI/OnboardProcess/CreateAccount.xaml b/windows/WinUI/OnboardProcess/CreateAccount.xaml deleted file mode 100644 index ddf50252..00000000 --- a/windows/WinUI/OnboardProcess/CreateAccount.xaml +++ /dev/null @@ -1,38 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/windows/WinUI/OnboardProcess/CreateAccount.xaml.cs b/windows/WinUI/OnboardProcess/CreateAccount.xaml.cs deleted file mode 100644 index 72ba2182..00000000 --- a/windows/WinUI/OnboardProcess/CreateAccount.xaml.cs +++ /dev/null @@ -1,66 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; - -namespace WinUI.OnboardProcess -{ - /// - /// Interaction logic for CreateAccount.xaml - /// - public partial class CreateAccount : UserControl, ISwitchable - { - public CreateAccount() - { - InitializeComponent(); - } - - public void UtilizeState(object state) - { - throw new NotImplementedException(); - } - - public void CreateAccount_Click(object sender, RoutedEventArgs e) - { - DoCreateAccount(); - } - - public void BackButton_Click(object sender, RoutedEventArgs e) - { - Switcher.Switch(new RegisterOrLogIn()); - } - - public async void DoCreateAccount() - { - if (PasswordTextBox1.Password.ToString() != PasswordTextBox2.Password.ToString()) - { - ErrorText.Content = "Passwords do not match!"; - } - else - { - CentralAPI api = CentralAPI.Instance; - bool accountCreated = await api.Login(EmailAddressTextBox.Text, - PasswordTextBox1.Password.ToString(), true); - - if (accountCreated) - { - Switcher.Switch(new CreateOrJoin()); - } - else - { - ErrorText.Content = "An error ocurred while creating your account."; - } - } - } - } -} diff --git a/windows/WinUI/OnboardProcess/CreateOrJoin.xaml b/windows/WinUI/OnboardProcess/CreateOrJoin.xaml deleted file mode 100644 index 21413e84..00000000 --- a/windows/WinUI/OnboardProcess/CreateOrJoin.xaml +++ /dev/null @@ -1,49 +0,0 @@ - - - - - - - - - - - - - - - - - - diff --git a/windows/WinUI/OnboardProcess/Finished.xaml.cs b/windows/WinUI/OnboardProcess/Finished.xaml.cs deleted file mode 100644 index a34abfe2..00000000 --- a/windows/WinUI/OnboardProcess/Finished.xaml.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; - -namespace WinUI.OnboardProcess -{ - /// - /// Interaction logic for Finished.xaml - /// - public partial class Finished : UserControl, ISwitchable - { - public Finished() - { - InitializeComponent(); - } - public void UtilizeState(object state) - { - throw new NotImplementedException(); - } - - private void DoneButton_Click(object sender, RoutedEventArgs e) - { - Window.GetWindow(this).Close(); - } - } -} diff --git a/windows/WinUI/OnboardProcess/LogIn.xaml b/windows/WinUI/OnboardProcess/LogIn.xaml deleted file mode 100644 index 501f7e0f..00000000 --- a/windows/WinUI/OnboardProcess/LogIn.xaml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/windows/WinUI/OnboardProcess/LogIn.xaml.cs b/windows/WinUI/OnboardProcess/LogIn.xaml.cs deleted file mode 100644 index 8657b800..00000000 --- a/windows/WinUI/OnboardProcess/LogIn.xaml.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; -using System.Windows.Input; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; - -namespace WinUI.OnboardProcess -{ - /// - /// Interaction logic for LogIn.xaml - /// - public partial class LogIn : UserControl, ISwitchable - { - public LogIn() - { - InitializeComponent(); - } - - public void UtilizeState(object state) - { - throw new NotImplementedException(); - } - - public void LoginButton_Click(object sender, RoutedEventArgs e) - { - DoLogin(); - } - - public void BackButton_Click(object sender, RoutedEventArgs e) - { - Switcher.Switch(new RegisterOrLogIn()); - } - - private async void DoLogin() - { - CentralAPI api = CentralAPI.Instance; - bool didLogIn = await api.Login(EmailAddressTextBox.Text, PasswordTextBox.Password.ToString(), false); - if (didLogIn) - { - Switcher.Switch(new CreateOrJoin()); - } - else - { - ErrorText.Content = "Invalid username or password"; - } - } - } -} diff --git a/windows/WinUI/OnboardProcess/RegisterOrLogIn.xaml b/windows/WinUI/OnboardProcess/RegisterOrLogIn.xaml deleted file mode 100644 index 01f3ba9d..00000000 --- a/windows/WinUI/OnboardProcess/RegisterOrLogIn.xaml +++ /dev/null @@ -1,27 +0,0 @@ - - - Welcome to ZeroTier - Let's get started! - - If you haven't yet created an account, click "Create Account" below. If you have an account, you can log in with your username/password, or simply enter an API key. - - - - -