From 86c74d8a65ae45d6ff0433da7d4d4acb6cba8fcb Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Wed, 4 Nov 2015 20:34:49 -0800 Subject: [PATCH] set manifest to require administrator privileges and read the authtoken and port from disk --- windows/WinUI/APIHandler.cs | 7 ++-- windows/WinUI/MainWindow.xaml.cs | 68 ++++++++++++++++++++++++++------ windows/WinUI/WinUI.csproj | 4 ++ windows/WinUI/app.manifest | 55 ++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 16 deletions(-) create mode 100644 windows/WinUI/app.manifest diff --git a/windows/WinUI/APIHandler.cs b/windows/WinUI/APIHandler.cs index 2968c963a..92b830215 100644 --- a/windows/WinUI/APIHandler.cs +++ b/windows/WinUI/APIHandler.cs @@ -14,7 +14,7 @@ namespace WinUI public class APIHandler { - static string authtoken = "p3ptrzds5jkr2hbx5ipbyf04"; // delete me! + private string authtoken; private string url = null; @@ -23,9 +23,10 @@ namespace WinUI url = "http://127.0.0.1:9993"; } - public APIHandler(string host, int port) + public APIHandler(int port, string authtoken) { - url = "http://" + host + ":" + port; + url = "http://localhost:" + port; + this.authtoken = authtoken; } public ZeroTierStatus GetStatus() diff --git a/windows/WinUI/MainWindow.xaml.cs b/windows/WinUI/MainWindow.xaml.cs index 103165b3d..535cb386b 100644 --- a/windows/WinUI/MainWindow.xaml.cs +++ b/windows/WinUI/MainWindow.xaml.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; @@ -23,7 +24,7 @@ namespace WinUI /// public partial class MainWindow : Window { - APIHandler handler = new APIHandler(); + APIHandler handler; Regex charRegex = new Regex("[0-9a-fxA-FX]"); Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$"); @@ -35,22 +36,63 @@ namespace WinUI { InitializeComponent(); - networksPage.SetAPIHandler(handler); - - updateStatus(); - if (!connected) + if (InitAPIHandler()) { - MessageBox.Show("Unable to connect to ZeroTier Service."); + networksPage.SetAPIHandler(handler); + + updateStatus(); + if (!connected) + { + MessageBox.Show("Unable to connect to ZeroTier Service."); + } + + updateNetworks(); + updatePeers(); + + DataObject.AddPastingHandler(joinNetworkID, OnPaste); + + timer.Elapsed += new ElapsedEventHandler(OnUpdateTimer); + timer.Interval = 2000; + timer.Enabled = true; + } + } + + private bool InitAPIHandler() + { + String ztDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\ZeroTier\\One"; + String authToken = ""; + Int32 port = 9993; + try + { + byte[] tmp = File.ReadAllBytes(ztDir + "\\authtoken.secret"); + authToken = System.Text.Encoding.ASCII.GetString(tmp).Trim(); + } + catch + { + MessageBox.Show("Unable to read ZeroTier One authtoken.secret from:\r\n" + ztDir, "ZeroTier One"); + this.Close(); + return false; } - updateNetworks(); - updatePeers(); + if ((authToken == null) || (authToken.Length <= 0)) + { + MessageBox.Show("Unable to read ZeroTier One authtoken.secret from:\r\n" + ztDir, "ZeroTier One"); + this.Close(); + return false; + } + try + { + byte[] tmp = File.ReadAllBytes(ztDir + "\\zerotier-one.port"); + port = Int32.Parse(System.Text.Encoding.ASCII.GetString(tmp).Trim()); + if ((port <= 0) || (port > 65535)) + port = 9993; + } + catch + { + } - DataObject.AddPastingHandler(joinNetworkID, OnPaste); - - timer.Elapsed += new ElapsedEventHandler(OnUpdateTimer); - timer.Interval = 2000; - timer.Enabled = true; + handler = new APIHandler(port, authToken); + return true; } private void updateStatus() diff --git a/windows/WinUI/WinUI.csproj b/windows/WinUI/WinUI.csproj index 5ebfedcf8..c3eeaba4e 100644 --- a/windows/WinUI/WinUI.csproj +++ b/windows/WinUI/WinUI.csproj @@ -63,6 +63,9 @@ false + + app.manifest + @@ -161,6 +164,7 @@ ResXFileCodeGenerator Resources.Designer.cs + SettingsSingleFileGenerator diff --git a/windows/WinUI/app.manifest b/windows/WinUI/app.manifest new file mode 100644 index 000000000..b537bf491 --- /dev/null +++ b/windows/WinUI/app.manifest @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +