From 6b0543ba27511d7bd7b40cfca2f24608a6b8d9ee Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Tue, 8 Nov 2016 14:54:55 -0800 Subject: starts up to a toolbar icon with context menu. still much more to do --- windows/WinUI/ToolbarItem.xaml.cs | 127 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 windows/WinUI/ToolbarItem.xaml.cs (limited to 'windows/WinUI/ToolbarItem.xaml.cs') diff --git a/windows/WinUI/ToolbarItem.xaml.cs b/windows/WinUI/ToolbarItem.xaml.cs new file mode 100644 index 00000000..e34863dd --- /dev/null +++ b/windows/WinUI/ToolbarItem.xaml.cs @@ -0,0 +1,127 @@ +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.Shapes; +using System.Text.RegularExpressions; +using System.Timers; +using System.Windows.Threading; +using System.IO; +using System.Diagnostics; + +namespace WinUI +{ + /// + /// Interaction logic for ToolbarItem.xaml + /// + public partial class ToolbarItem : Window + { + APIHandler handler; + + public ToolbarItem() + { + InitializeComponent(); + + if (InitAPIHandler()) + { + + } + else + { + MessageBox.Show("ZeroTier API Initialization Failed"); + } + } + + private String readAuthToken(String path) + { + String authToken = ""; + + if (File.Exists(path)) + { + 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"); + } + } + + return authToken; + } + + private Int32 readPort(String path) + { + Int32 port = 9993; + + try + { + byte[] tmp = File.ReadAllBytes(path); + port = Int32.Parse(System.Text.Encoding.ASCII.GetString(tmp).Trim()); + if ((port <= 0) || (port > 65535)) + port = 9993; + } + catch + { + } + + 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; + } + + private void ToolbarItem_TrayContextMenuOpen(object sender, System.Windows.RoutedEventArgs e) + { + Console.WriteLine("TrayContextMenuOpen"); + } + + private void ToolbarItem_PreviewTrayContextMenuOpen(object sender, System.Windows.RoutedEventArgs e) + { + Console.WriteLine("PreviewTrayContextMenuOpen"); + } + } +} -- cgit v1.2.3 From e1f9f7b6dc80a5ad0db3fedc8f5b92d47cbdb1fe Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Tue, 8 Nov 2016 15:50:08 -0800 Subject: turn APIHandler into a singleton --- windows/WinUI/APIHandler.cs | 91 ++++++++++++++++++++++++++++++++++++++- windows/WinUI/ToolbarItem.xaml.cs | 85 +----------------------------------- 2 files changed, 91 insertions(+), 85 deletions(-) (limited to 'windows/WinUI/ToolbarItem.xaml.cs') diff --git a/windows/WinUI/APIHandler.cs b/windows/WinUI/APIHandler.cs index 2a8dea13..f2fb1d21 100644 --- a/windows/WinUI/APIHandler.cs +++ b/windows/WinUI/APIHandler.cs @@ -7,6 +7,7 @@ using System.Net; using System.IO; using System.Windows; using Newtonsoft.Json; +using System.Diagnostics; namespace WinUI { @@ -18,7 +19,95 @@ namespace WinUI private string url = null; - public APIHandler() + private static APIHandler instance; + + public static APIHandler Instance + { + get + { + if (instance == null) + { + + } + + return instance; + } + } + + private static bool initHandler() + { + 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"); + return false; + } + + port = readPort(localZtDir + "\\zerotier-one.port"); + instance = new APIHandler(port, authToken); + return true; + } + + private static String readAuthToken(String path) + { + String authToken = ""; + + if (File.Exists(path)) + { + 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"); + } + } + + return authToken; + } + + private static Int32 readPort(String path) + { + Int32 port = 9993; + + try + { + byte[] tmp = File.ReadAllBytes(path); + port = Int32.Parse(System.Text.Encoding.ASCII.GetString(tmp).Trim()); + if ((port <= 0) || (port > 65535)) + port = 9993; + } + catch + { + } + + return port; + } + + private APIHandler() { url = "http://127.0.0.1:9993"; } diff --git a/windows/WinUI/ToolbarItem.xaml.cs b/windows/WinUI/ToolbarItem.xaml.cs index e34863dd..9ff7e860 100644 --- a/windows/WinUI/ToolbarItem.xaml.cs +++ b/windows/WinUI/ToolbarItem.xaml.cs @@ -24,94 +24,11 @@ namespace WinUI /// public partial class ToolbarItem : Window { - APIHandler handler; + APIHandler handler = APIHandler.Instance; public ToolbarItem() { InitializeComponent(); - - if (InitAPIHandler()) - { - - } - else - { - MessageBox.Show("ZeroTier API Initialization Failed"); - } - } - - private String readAuthToken(String path) - { - String authToken = ""; - - if (File.Exists(path)) - { - 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"); - } - } - - return authToken; - } - - private Int32 readPort(String path) - { - Int32 port = 9993; - - try - { - byte[] tmp = File.ReadAllBytes(path); - port = Int32.Parse(System.Text.Encoding.ASCII.GetString(tmp).Trim()); - if ((port <= 0) || (port > 65535)) - port = 9993; - } - catch - { - } - - 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; } private void ToolbarItem_TrayContextMenuOpen(object sender, System.Windows.RoutedEventArgs e) -- cgit v1.2.3 From 7cf3d2caa19da076ebd3539c8786ce62a53d35f9 Mon Sep 17 00:00:00 2001 From: Grant Limberg Date: Tue, 8 Nov 2016 16:31:07 -0800 Subject: Network list window opens with click on the menu item --- windows/WinUI/APIHandler.cs | 14 ++- windows/WinUI/MainWindow.xaml | 132 --------------------------- windows/WinUI/MainWindow.xaml.cs | 166 ---------------------------------- windows/WinUI/NetworkInfoView.xaml.cs | 12 +-- windows/WinUI/NetworkList.xaml | 132 +++++++++++++++++++++++++++ windows/WinUI/NetworkList.xaml.cs | 153 +++++++++++++++++++++++++++++++ windows/WinUI/NetworksPage.xaml.cs | 8 -- windows/WinUI/ToolbarItem.xaml | 6 +- windows/WinUI/ToolbarItem.xaml.cs | 28 +++++- windows/WinUI/WinUI.csproj | 6 +- 10 files changed, 336 insertions(+), 321 deletions(-) delete mode 100644 windows/WinUI/MainWindow.xaml delete mode 100644 windows/WinUI/MainWindow.xaml.cs create mode 100644 windows/WinUI/NetworkList.xaml create mode 100644 windows/WinUI/NetworkList.xaml.cs (limited to 'windows/WinUI/ToolbarItem.xaml.cs') diff --git a/windows/WinUI/APIHandler.cs b/windows/WinUI/APIHandler.cs index f2fb1d21..9cddd916 100644 --- a/windows/WinUI/APIHandler.cs +++ b/windows/WinUI/APIHandler.cs @@ -19,7 +19,8 @@ namespace WinUI private string url = null; - private static APIHandler instance; + private static volatile APIHandler instance; + private static object syncRoot = new Object(); public static APIHandler Instance { @@ -27,7 +28,16 @@ namespace WinUI { if (instance == null) { - + lock (syncRoot) + { + if (instance == null) + { + if (!initHandler()) + { + return null; + } + } + } } return instance; diff --git a/windows/WinUI/MainWindow.xaml b/windows/WinUI/MainWindow.xaml deleted file mode 100644 index 9d4a9fc1..00000000 --- a/windows/WinUI/MainWindow.xaml +++ /dev/null @@ -1,132 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -