2015-10-20 03:20:42 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2015-11-05 04:34:49 +00:00
|
|
|
|
using System.IO;
|
2015-10-20 03:20:42 +00:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
2015-10-23 22:49:04 +00:00
|
|
|
|
using System.Text.RegularExpressions;
|
2015-10-27 01:31:10 +00:00
|
|
|
|
using System.Timers;
|
2015-10-20 03:20:42 +00:00
|
|
|
|
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;
|
2015-10-27 01:31:10 +00:00
|
|
|
|
using System.Windows.Threading;
|
2015-10-20 03:20:42 +00:00
|
|
|
|
|
|
|
|
|
namespace WinUI
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Interaction logic for MainWindow.xaml
|
|
|
|
|
/// </summary>
|
|
|
|
|
public partial class MainWindow : Window
|
|
|
|
|
{
|
2015-11-05 04:34:49 +00:00
|
|
|
|
APIHandler handler;
|
2015-10-27 01:08:44 +00:00
|
|
|
|
Regex charRegex = new Regex("[0-9a-fxA-FX]");
|
|
|
|
|
Regex wholeStringRegex = new Regex("^[0-9a-fxA-FX]+$");
|
2015-10-22 03:29:03 +00:00
|
|
|
|
|
2015-10-27 01:31:10 +00:00
|
|
|
|
Timer timer = new Timer();
|
|
|
|
|
|
2015-11-05 02:28:07 +00:00
|
|
|
|
bool connected = false;
|
|
|
|
|
|
2015-10-20 03:20:42 +00:00
|
|
|
|
public MainWindow()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
2015-10-22 03:29:03 +00:00
|
|
|
|
|
2015-11-05 04:34:49 +00:00
|
|
|
|
if (InitAPIHandler())
|
2015-11-05 02:28:07 +00:00
|
|
|
|
{
|
2015-11-05 04:34:49 +00:00
|
|
|
|
networksPage.SetAPIHandler(handler);
|
|
|
|
|
|
|
|
|
|
updateStatus();
|
|
|
|
|
if (!connected)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Unable to connect to ZeroTier Service.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateNetworks();
|
2015-11-06 03:39:17 +00:00
|
|
|
|
//updatePeers();
|
2015-11-05 04:34:49 +00:00
|
|
|
|
|
|
|
|
|
DataObject.AddPastingHandler(joinNetworkID, OnPaste);
|
|
|
|
|
|
|
|
|
|
timer.Elapsed += new ElapsedEventHandler(OnUpdateTimer);
|
|
|
|
|
timer.Interval = 2000;
|
|
|
|
|
timer.Enabled = true;
|
2015-11-05 02:28:07 +00:00
|
|
|
|
}
|
2015-11-05 04:34:49 +00:00
|
|
|
|
}
|
2015-11-05 02:28:07 +00:00
|
|
|
|
|
2015-11-05 04:34:49 +00:00
|
|
|
|
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;
|
|
|
|
|
}
|
2015-10-27 01:08:44 +00:00
|
|
|
|
|
2015-11-05 04:34:49 +00:00
|
|
|
|
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
|
|
|
|
|
{
|
|
|
|
|
}
|
2015-10-27 01:31:10 +00:00
|
|
|
|
|
2015-11-05 04:34:49 +00:00
|
|
|
|
handler = new APIHandler(port, authToken);
|
|
|
|
|
return true;
|
2015-10-22 03:29:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateStatus()
|
|
|
|
|
{
|
2015-10-23 22:36:42 +00:00
|
|
|
|
var status = handler.GetStatus();
|
2015-10-22 03:29:03 +00:00
|
|
|
|
|
2015-11-05 02:28:07 +00:00
|
|
|
|
if (status != null)
|
2015-10-27 01:31:10 +00:00
|
|
|
|
{
|
2015-11-05 02:28:07 +00:00
|
|
|
|
connected = true;
|
|
|
|
|
|
|
|
|
|
networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
|
|
|
|
{
|
|
|
|
|
this.networkId.Content = 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
|
2015-10-27 01:31:10 +00:00
|
|
|
|
{
|
2015-11-05 02:28:07 +00:00
|
|
|
|
connected = false;
|
|
|
|
|
|
|
|
|
|
networkId.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
|
|
|
|
{
|
|
|
|
|
this.networkId.Content = "";
|
|
|
|
|
}));
|
|
|
|
|
versionString.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
|
|
|
|
{
|
|
|
|
|
this.versionString.Content = "0";
|
|
|
|
|
}));
|
|
|
|
|
onlineStatus.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
|
|
|
|
{
|
|
|
|
|
this.onlineStatus.Content = "OFFLINE";
|
|
|
|
|
}));
|
|
|
|
|
}
|
2015-10-22 03:29:03 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateNetworks()
|
|
|
|
|
{
|
2015-10-23 22:36:42 +00:00
|
|
|
|
var networks = handler.GetNetworks();
|
2015-10-22 03:29:03 +00:00
|
|
|
|
|
2015-10-27 01:31:10 +00:00
|
|
|
|
networksPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
|
|
|
|
{
|
|
|
|
|
networksPage.setNetworks(networks);
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updatePeers()
|
|
|
|
|
{
|
2015-11-06 03:39:17 +00:00
|
|
|
|
//var peers = handler.GetPeers();
|
2015-10-27 01:31:10 +00:00
|
|
|
|
|
2015-11-06 03:39:17 +00:00
|
|
|
|
//peersPage.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(() =>
|
|
|
|
|
//{
|
|
|
|
|
// peersPage.SetPeers(peers);
|
|
|
|
|
//}));
|
2015-10-27 01:31:10 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void OnUpdateTimer(object source, ElapsedEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
updateStatus();
|
|
|
|
|
updateNetworks();
|
2015-11-06 03:39:17 +00:00
|
|
|
|
//updatePeers();
|
2015-10-20 03:20:42 +00:00
|
|
|
|
}
|
2015-10-23 22:36:42 +00:00
|
|
|
|
|
|
|
|
|
private void joinButton_Click(object sender, RoutedEventArgs e)
|
|
|
|
|
{
|
2015-10-23 22:50:49 +00:00
|
|
|
|
if (joinNetworkID.Text.Length < 16)
|
|
|
|
|
{
|
|
|
|
|
MessageBox.Show("Invalid Network ID");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
handler.JoinNetwork(joinNetworkID.Text);
|
|
|
|
|
}
|
2015-10-23 22:36:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-23 22:49:04 +00:00
|
|
|
|
private void OnNetworkEntered(object sender, TextCompositionEventArgs e)
|
2015-10-23 22:36:42 +00:00
|
|
|
|
{
|
2015-10-27 01:08:44 +00:00
|
|
|
|
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();
|
|
|
|
|
}
|
2015-10-23 22:36:42 +00:00
|
|
|
|
}
|
2015-10-20 03:20:42 +00:00
|
|
|
|
}
|
|
|
|
|
}
|