updated ZTO version

This commit is contained in:
Joseph Henry
2017-03-07 11:08:02 -08:00
parent ce42dd4815
commit 9016bc8385
132 changed files with 11902 additions and 10793 deletions

View File

@@ -7,6 +7,7 @@ using System.Net;
using System.IO;
using System.Windows;
using Newtonsoft.Json;
using System.Diagnostics;
namespace WinUI
{
@@ -18,7 +19,108 @@ namespace WinUI
private string url = null;
public APIHandler()
private static volatile APIHandler instance;
private static object syncRoot = new Object();
public delegate void NetworkListCallback(List<ZeroTierNetwork> networks);
public delegate void StatusCallback(ZeroTierStatus status);
public static APIHandler Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
{
if (!initHandler())
{
return 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";
}
@@ -29,7 +131,9 @@ namespace WinUI
this.authtoken = authtoken;
}
public ZeroTierStatus GetStatus()
public void GetStatus(StatusCallback cb)
{
var request = WebRequest.Create(url + "/status" + "?auth=" + authtoken) as HttpWebRequest;
if (request != null)
@@ -54,29 +158,32 @@ namespace WinUI
{
Console.WriteLine(e.ToString());
}
return status;
cb(status);
}
}
catch (System.Net.Sockets.SocketException)
{
return null;
cb(null);
}
catch (System.Net.WebException)
{
return null;
cb(null);
}
}
public List<ZeroTierNetwork> GetNetworks()
public void GetNetworks(NetworkListCallback cb)
{
var request = WebRequest.Create(url + "/network" + "?auth=" + authtoken) as HttpWebRequest;
if (request == null)
{
return null;
cb(null);
}
request.Method = "GET";
request.ContentType = "application/json";
request.Timeout = 10000;
try
{
@@ -89,25 +196,30 @@ namespace WinUI
try
{
networkList = JsonConvert.DeserializeObject<List<ZeroTierNetwork>>(responseText);
foreach (ZeroTierNetwork n in networkList)
{
// all networks received via JSON are connected by definition
n.IsConnected = true;
}
}
catch (JsonReaderException e)
{
Console.WriteLine(e.ToString());
}
return networkList;
cb(networkList);
}
}
catch (System.Net.Sockets.SocketException)
{
return null;
cb(null);
}
catch (System.Net.WebException)
{
return null;
cb(null);
}
}
public void JoinNetwork(string nwid)
public void JoinNetwork(string nwid, bool allowManaged = true, bool allowGlobal = false, bool allowDefault = false)
{
var request = WebRequest.Create(url + "/network/" + nwid + "?auth=" + authtoken) as HttpWebRequest;
if (request == null)
@@ -116,6 +228,18 @@ namespace WinUI
}
request.Method = "POST";
request.ContentType = "applicaiton/json";
request.Timeout = 10000;
using (var streamWriter = new StreamWriter(((HttpWebRequest)request).GetRequestStream()))
{
string json = "{\"allowManaged\":" + (allowManaged ? "true" : "false") + "," +
"\"allowGlobal\":" + (allowGlobal ? "true" : "false") + "," +
"\"allowDefault\":" + (allowDefault ? "true" : "false") + "}";
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
try
{
@@ -145,6 +269,7 @@ namespace WinUI
}
request.Method = "DELETE";
request.Timeout = 10000;
try
{
@@ -163,14 +288,20 @@ namespace WinUI
{
MessageBox.Show("Error Leaving Network: Cannot connect to ZeroTier service.");
}
catch
{
Console.WriteLine("Error leaving network: Unknown error");
}
}
public List<ZeroTierPeer> GetPeers()
public delegate void PeersCallback(List<ZeroTierPeer> peers);
public void GetPeers(PeersCallback cb)
{
var request = WebRequest.Create(url + "/peer" + "?auth=" + authtoken) as HttpWebRequest;
if (request == null)
{
return null;
cb(null);
}
request.Method = "GET";
@@ -192,16 +323,16 @@ namespace WinUI
{
Console.WriteLine(e.ToString());
}
return peerList;
cb(peerList);
}
}
catch (System.Net.Sockets.SocketException)
{
return null;
cb(null);
}
catch (System.Net.WebException)
{
return null;
cb(null);
}
}
}