From c802811ad298d20315e358b0fc7964a16e591aa6 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Mon, 7 Nov 2016 14:08:26 -0800 Subject: [PATCH] Added a file copy util for Windows UI Copies the authtoken.secret file into a private local folder for the user so that the UI doesnt have to be run with Admin privileges. --- .gitignore | 2 + windows/WinUI/MainWindow.xaml.cs | 76 +++++++++---- windows/WinUI/WinUI.csproj | 7 +- windows/ZeroTierOne.sln | 115 ++++++++++++++++++++ windows/copyutil/App.config | 6 + windows/copyutil/Program.cs | 40 +++++++ windows/copyutil/Properties/AssemblyInfo.cs | 36 ++++++ windows/copyutil/copyutil.csproj | 60 ++++++++++ 8 files changed, 320 insertions(+), 22 deletions(-) create mode 100644 windows/copyutil/App.config create mode 100644 windows/copyutil/Program.cs create mode 100644 windows/copyutil/Properties/AssemblyInfo.cs create mode 100644 windows/copyutil/copyutil.csproj diff --git a/.gitignore b/.gitignore index d3ef1581d..d368697c5 100755 --- a/.gitignore +++ b/.gitignore @@ -37,6 +37,8 @@ Thumbs.db /windows/*.db /windows/*.opendb enc_temp_folder +/windows/copyutil/bin +/windows/copyutil/obj # *nix/Mac build droppings /build-* diff --git a/windows/WinUI/MainWindow.xaml.cs b/windows/WinUI/MainWindow.xaml.cs index 47f00bfe1..25534b465 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.Diagnostics; using System.IO; using System.Linq; using System.Text; @@ -57,32 +58,34 @@ namespace WinUI } } - private bool InitAPIHandler() + + private String readAuthToken(String path) { - String ztDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\ZeroTier\\One"; String authToken = ""; - Int32 port = 9993; - try + + if (File.Exists(path)) { - 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; + try + { + byte[] tmp = File.ReadAllBytes(path); + authToken = System.Text.Encoding.UTF8.GetString(tmp).Trim(); + } + catch + { + MessageBox.Show("Unable to read ZeroTier One Auth Token from:\r\n" + path, "ZeroTier One"); + } } - 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; - } + return authToken; + } + + private Int32 readPort(String path) + { + Int32 port = 9993; + try { - byte[] tmp = File.ReadAllBytes(ztDir + "\\zerotier-one.port"); + byte[] tmp = File.ReadAllBytes(path); port = Int32.Parse(System.Text.Encoding.ASCII.GetString(tmp).Trim()); if ((port <= 0) || (port > 65535)) port = 9993; @@ -91,6 +94,41 @@ namespace WinUI { } + return port; + } + + private bool InitAPIHandler() + { + String localZtDir = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\ZeroTier\\One"; + String globalZtDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\ZeroTier\\One"; + + String authToken = ""; + Int32 port = 9993; + + if (!File.Exists(localZtDir + "\\authtoken.secret") || !File.Exists(localZtDir + "\\zerotier-one.port")) + { + // launch external process to copy file into place + 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); + startInfo.Verb = "runas"; + + + var process = Process.Start(startInfo); + process.WaitForExit(); + } + + authToken = readAuthToken(localZtDir + "\\authtoken.secret"); + + if ((authToken == null) || (authToken.Length <= 0)) + { + MessageBox.Show("Unable to read ZeroTier One authtoken", "ZeroTier One"); + this.Close(); + return false; + } + + port = readPort(localZtDir + "\\zerotier-one.port"); handler = new APIHandler(port, authToken); return true; } diff --git a/windows/WinUI/WinUI.csproj b/windows/WinUI/WinUI.csproj index 9e3527c98..76b4862ab 100644 --- a/windows/WinUI/WinUI.csproj +++ b/windows/WinUI/WinUI.csproj @@ -63,9 +63,7 @@ false - - app.manifest - + @@ -218,6 +216,9 @@ + + copy $(SolutionDir)\copyutil\bin\$(ConfigurationName)\copyutil.exe $(ProjectDir)\$(OutDir)\copyutil.exe + + \ No newline at end of file