Merge branch 'dev' into systemtray

This commit is contained in:
Grant Limberg 2016-11-16 16:23:56 -08:00
commit b4bacd50a1
36 changed files with 1781 additions and 1139 deletions

View file

@ -0,0 +1,233 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Timers;
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;
using System.Windows.Threading;
namespace WinUI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
APIHandler handler;
Regex charRegex = new Regex("[0-9a-fxA-FX]");
Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$");
Timer timer = new Timer();
bool connected = false;
public MainWindow()
{
InitializeComponent();
if (InitAPIHandler())
{
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 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 updateStatus()
{
var status = handler.GetStatus();
if (status != null)
{
connected = true;
networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
this.networkId.Text = status.Address;
}));
versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
this.versionString.Content = status.Version;
}));
onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
this.onlineStatus.Content = (status.Online ? "ONLINE" : "OFFLINE");
}));
}
else
{
connected = false;
networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
this.networkId.Text = "";
}));
versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
this.versionString.Content = "0";
}));
onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
this.onlineStatus.Content = "OFFLINE";
}));
}
}
private void updateNetworks()
{
var networks = handler.GetNetworks();
networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
networksPage.setNetworks(networks);
}));
}
private void updatePeers()
{
//var peers = handler.GetPeers();
//peersPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
//{
// peersPage.SetPeers(peers);
//}));
}
private void OnUpdateTimer(object source, ElapsedEventArgs e)
{
updateStatus();
updateNetworks();
//updatePeers();
}
private void joinButton_Click(object sender, RoutedEventArgs e)
{
if (joinNetworkID.Text.Length < 16)
{
MessageBox.Show("Invalid Network ID");
}
else
{
handler.JoinNetwork(joinNetworkID.Text);
}
}
private void OnNetworkEntered(object sender, TextCompositionEventArgs e)
{
e.Handled = !charRegex.IsMatch(e.Text);
}
private void OnPaste(object sender, DataObjectPastingEventArgs e)
{
var isText = e.SourceDataObject.GetDataPresent(DataFormats.UnicodeText, true);
if (!isText) return;
var text = e.SourceDataObject.GetData(DataFormats.UnicodeText) as string;
if (!wholeStringRegex.IsMatch(text))
{
e.CancelCommand();
}
}
}
}

View file

@ -64,7 +64,7 @@
<TextBlock x:Name="broadcastEnabled" FontFamily="Lucida Console" TextWrapping="Wrap" Text="ENABLED" HorizontalAlignment="Right" Grid.Column="2" Grid.Row="6" Foreground="#FF000000"/>
<TextBlock x:Name="bridgingEnabled" FontFamily="Lucida Console" TextWrapping="Wrap" Text="DISABLED" HorizontalAlignment="Right" Grid.Column="2" Grid.Row="7" Background="#FFEEEEEE" Foreground="#FF000000"/>
<TextBlock x:Name="deviceName" FontFamily="Lucida Console" TextWrapping="Wrap" HorizontalAlignment="Right" Grid.Column="2" Grid.Row="8" Foreground="#FF000000"><Span><Run Text="ethernet_32771"/></Span></TextBlock>
<TextBlock x:Name="managedIps" TextWrapping="Wrap" FontFamily="Lucida Console" HorizontalAlignment="Right" TextAlignment="Right" Grid.Column="2" Grid.Row="9" Foreground="#FF000000"><Span><Run Text="28.2.169.248/7 "/></Span><LineBreak/><Span><Run Text="fd80:56c2:e21c:0000:0199:9383:4a02:a9f8/88"/></Span></TextBlock>
<TextBox x:Name="managedIps" TextWrapping="Wrap" FontFamily="Lucida Console" HorizontalAlignment="Right" TextAlignment="Right" Grid.Column="2" Grid.Row="9" Foreground="#FF000000" IsReadOnly="True" BorderThickness="0" Background="#FFEEEEEE" Text="28.2.169.248/7&#x0a;fd80:56c2:e21c:0000:0199:9383:4a02:a9f8/88"/>
<CheckBox x:Name="allowGlobal" HorizontalAlignment="Right" Grid.Column="2" Grid.Row="10" />
<CheckBox x:Name="allowManaged" HorizontalAlignment="Right" Grid.Column="2" Grid.Row="11" />
<CheckBox x:Name="allowDefault" HorizontalAlignment="Right" Grid.Column="2" Grid.Row="12" />

View file

@ -20,7 +20,7 @@ namespace WinUI
/// </summary>
public partial class NetworkInfoView : UserControl
{
private ZeroTierNetwork network;
public ZeroTierNetwork network;
public NetworkInfoView(ZeroTierNetwork network)
{
@ -29,19 +29,41 @@ namespace WinUI
this.network = network;
UpdateNetworkData();
allowDefault.Checked += AllowDefault_CheckStateChanged;
allowDefault.Unchecked += AllowDefault_CheckStateChanged;
allowGlobal.Checked += AllowGlobal_CheckStateChanged;
allowGlobal.Unchecked += AllowGlobal_CheckStateChanged;
allowManaged.Checked += AllowManaged_CheckStateChanged;
allowManaged.Unchecked += AllowManaged_CheckStateChanged;
}
private void UpdateNetworkData()
{
this.networkId.Text = network.NetworkId;
this.networkName.Text = network.NetworkName;
this.networkStatus.Text = network.NetworkStatus;
this.networkType.Text = network.NetworkType;
this.macAddress.Text = network.MacAddress;
this.mtu.Text = network.MTU.ToString();
if (this.networkId.Text != network.NetworkId)
this.networkId.Text = network.NetworkId;
if (this.networkName.Text != network.NetworkName)
this.networkName.Text = network.NetworkName;
if (this.networkStatus.Text != network.NetworkStatus)
this.networkStatus.Text = network.NetworkStatus;
if (this.networkType.Text != network.NetworkType)
this.networkType.Text = network.NetworkType;
if (this.macAddress.Text != network.MacAddress)
this.macAddress.Text = network.MacAddress;
if (this.mtu.Text != network.MTU.ToString())
this.mtu.Text = network.MTU.ToString();
this.broadcastEnabled.Text = (network.BroadcastEnabled ? "ENABLED" : "DISABLED");
this.bridgingEnabled.Text = (network.Bridge ? "ENABLED" : "DISABLED");
this.deviceName.Text = network.DeviceName;
if (this.deviceName.Text != network.DeviceName)
this.deviceName.Text = network.DeviceName;
string iplist = "";
for (int i = 0; i < network.AssignedAddresses.Length; ++i)
@ -51,19 +73,12 @@ namespace WinUI
iplist += "\n";
}
this.managedIps.Text = iplist;
if (this.managedIps.Text != iplist)
this.managedIps.Text = iplist;
this.allowDefault.IsChecked = network.AllowDefault;
this.allowGlobal.IsChecked = network.AllowGlobal;
this.allowManaged.IsChecked = network.AllowManaged;
allowDefault.Checked += AllowDefault_CheckStateChanged;
allowDefault.Unchecked += AllowDefault_CheckStateChanged;
allowGlobal.Checked += AllowGlobal_CheckStateChanged;
allowGlobal.Unchecked += AllowGlobal_CheckStateChanged;
allowManaged.Checked += AllowManaged_CheckStateChanged;
allowManaged.Unchecked += AllowManaged_CheckStateChanged;
}
public bool HasNetwork(ZeroTierNetwork network)
@ -74,6 +89,13 @@ namespace WinUI
return false;
}
public void SetNetworkInfo(ZeroTierNetwork network)
{
this.network = network;
UpdateNetworkData();
}
private void leaveButton_Click(object sender, RoutedEventArgs e)
{
APIHandler.Instance.LeaveNetwork(network.NetworkId);

View file

@ -93,7 +93,9 @@
</Grid>
</ItemsPanelTemplate>
</StatusBar.ItemsPanel>
<StatusBarItem Grid.Column="0" x:Name="networkId" Content="deadbeef00" Foreground="White" FontFamily="Lucida Console"/>
<StatusBarItem Grid.Column="0" x:Name="networkId_placeholder">
<TextBox x:Name="networkId" Text="deadbeef00" HorizontalAlignment="Left" Grid.Column="0" Foreground="White" FontFamily="Lucida Console" BorderThickness="0" IsReadOnly="true" Background="Transparent"/>
</StatusBarItem>
<StatusBarItem Grid.Column="1" x:Name="onlineStatus" Content="ONLINE" Foreground="White" FontFamily="Lucida Console"/>
<StatusBarItem Grid.Column="2" x:Name="versionString" Content="1.0.5" Foreground="White" FontFamily="Lucida Console"/>
<StatusBarItem Grid.Column="3" x:Name="blank" Content="" Height="43" Foreground="White"/>

View file

@ -63,7 +63,7 @@ namespace WinUI
networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
this.networkId.Content = status.Address;
this.networkId.Text = status.Address;
}));
versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
@ -80,7 +80,7 @@ namespace WinUI
networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{
this.networkId.Content = "";
this.networkId.Text = "";
}));
versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
{

View file

@ -27,18 +27,73 @@ namespace WinUI
public void setNetworks(List<ZeroTierNetwork> networks)
{
this.wrapPanel.Children.Clear();
if (networks == null)
{
this.wrapPanel.Children.Clear();
return;
}
for (int i = 0; i < networks.Count; ++i)
foreach (ZeroTierNetwork network in networks)
{
this.wrapPanel.Children.Add(
new NetworkInfoView(
networks.ElementAt<ZeroTierNetwork>(i)));
NetworkInfoView view = ChildWithNetwork(network);
if (view != null)
{
view.SetNetworkInfo(network);
}
else
{
wrapPanel.Children.Add(
new NetworkInfoView(
network));
}
}
// remove networks we're no longer joined to.
List<ZeroTierNetwork> tmpList = GetNetworksFromChildren();
foreach (ZeroTierNetwork n in networks)
{
if (tmpList.Contains(n))
{
tmpList.Remove(n);
}
}
foreach (ZeroTierNetwork n in tmpList)
{
NetworkInfoView view = ChildWithNetwork(n);
if (view != null)
{
wrapPanel.Children.Remove(view);
}
}
}
private NetworkInfoView ChildWithNetwork(ZeroTierNetwork network)
{
List<NetworkInfoView> list = wrapPanel.Children.OfType<NetworkInfoView>().ToList();
foreach (NetworkInfoView view in list)
{
if (view.HasNetwork(network))
{
return view;
}
}
return null;
}
private List<ZeroTierNetwork> GetNetworksFromChildren()
{
List<ZeroTierNetwork> networks = new List<ZeroTierNetwork>(wrapPanel.Children.Count);
List<NetworkInfoView> list = wrapPanel.Children.OfType<NetworkInfoView>().ToList();
foreach (NetworkInfoView n in list)
{
networks.Add(n.network);
}
return networks;
}
}
}

View file

@ -20,6 +20,7 @@
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\controller\EmbeddedNetworkController.cpp" />
<ClCompile Include="..\..\controller\JSONDB.cpp" />
<ClCompile Include="..\..\ext\http-parser\http_parser.c" />
<ClCompile Include="..\..\ext\libnatpmp\getgateway.c" />
<ClCompile Include="..\..\ext\libnatpmp\natpmp.c" />
@ -269,7 +270,7 @@
<SDLCheck>true</SDLCheck>
<AdditionalIncludeDirectories>
</AdditionalIncludeDirectories>
<PreprocessorDefinitions>NOMINMAX;STATICLIB;WIN32;ZT_TRACE;ZT_USE_MINIUPNPC;MINIUPNP_STATICLIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>NOMINMAX;STATICLIB;WIN32;ZT_TRACE;ZT_RULES_ENGINE_DEBUGGING;ZT_USE_MINIUPNPC;MINIUPNP_STATICLIB;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>false</MultiProcessorCompilation>
<DisableSpecificWarnings>4996</DisableSpecificWarnings>
</ClCompile>

View file

@ -261,6 +261,9 @@
<ClCompile Include="..\..\controller\EmbeddedNetworkController.cpp">
<Filter>Source Files\controller</Filter>
</ClCompile>
<ClCompile Include="..\..\controller\JSONDB.cpp">
<Filter>Source Files\controller</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="resource.h">