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.
This commit is contained in:
parent
5f63d5039b
commit
c802811ad2
8 changed files with 320 additions and 22 deletions
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue