1
0
mirror of https://github.com/Ylianst/MeshCentralRouter synced 2025-12-06 00:13:33 +00:00

Added automatic mapping as URL argument.

This commit is contained in:
Ylian Saint-Hilaire
2020-10-28 16:08:46 -07:00
parent 2f9aebb56e
commit 25328d3ff9
6 changed files with 176 additions and 42 deletions

View File

@@ -1,5 +1,5 @@
/*
Copyright 2009-2017 Intel Corporation
Copyright 2009-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -184,6 +184,38 @@ namespace MeshCentralRouter
}
autoLogin = (argflags == 7);
// Set automatic port map values
if (authLoginUrl != null) {
string autoNodeId = null;
int autoRemotePort = 0;
int autoProtocol = 0;
int autoAppId = 0;
bool autoExit = false;
try
{
// Automatic mappings
autoNodeId = getValueFromQueryString(authLoginUrl.Query, "nodeid");
autoRemotePort = int.Parse(getValueFromQueryString(authLoginUrl.Query, "remoteport"));
autoProtocol = int.Parse(getValueFromQueryString(authLoginUrl.Query, "protocol"));
autoAppId = int.Parse(getValueFromQueryString(authLoginUrl.Query, "appid"));
autoExit = (getValueFromQueryString(authLoginUrl.Query, "autoexit") == "1");
}
catch (Exception) { }
if ((autoRemotePort != 0) && (autoProtocol != 0) && (autoNodeId != null)) {
Dictionary<string, object> map = new Dictionary<string, object>();
map.Add("nodeId", autoNodeId);
map.Add("remotePort", autoRemotePort);
map.Add("localPort", 0);
map.Add("protocol", autoProtocol);
map.Add("appId", autoAppId);
map.Add("autoExit", autoExit);
map.Add("launch", 1);
mappingsToSetup = new ArrayList();
mappingsToSetup.Add(map);
devicesTabControl.SelectedIndex = 1;
}
}
// Check MeshCentral .mcrouter hook
installButton.Visible = !isRouterHooked();
}
@@ -285,7 +317,7 @@ namespace MeshCentralRouter
meshcentral.disconnect();
}
private string getValueFromQueryString(string query, string name)
private static string getValueFromQueryString(string query, string name)
{
if ((query == null) || (name == null)) return null;
int i = query.IndexOf("?" + name + "=");
@@ -389,15 +421,29 @@ namespace MeshCentralRouter
Uri serverurl = null;
if (authLoginUrl != null) {
meshcentral.okCertHash2 = getValueFromQueryString(authLoginUrl.Query, "t");
serverurl = new Uri("wss://" + authLoginUrl.Host + ":" + ((authLoginUrl.Port > 0) ? authLoginUrl.Port : 443) + authLoginUrl.LocalPath + "?auth=" + getValueFromQueryString(authLoginUrl.Query, "c"));
string loginkey = getValueFromQueryString(authLoginUrl.Query, "key");
string urlstring = "wss://" + authLoginUrl.Host + ":" + ((authLoginUrl.Port > 0) ? authLoginUrl.Port : 443) + authLoginUrl.LocalPath + "?auth=" + getValueFromQueryString(authLoginUrl.Query, "c");
if (loginkey != null) { urlstring += ("&key=" + loginkey); }
serverurl = new Uri(urlstring);
meshcentral.connect(serverurl, null, null, null);
} else {
// Load two factor cookie if present
string twoFactorCookie = getRegValue("TwoFactorCookie", null);
if ((twoFactorCookie != null) && (twoFactorCookie != "")) { twoFactorCookie = "cookie=" + twoFactorCookie; } else { twoFactorCookie = null; }
serverurl = new Uri("wss://" + serverNameComboBox.Text + "/control.ashx");
int keyIndex = serverNameComboBox.Text.IndexOf("?key=");
if (keyIndex >= 0)
{
string hostname = serverNameComboBox.Text.Substring(0, keyIndex);
string loginkey = serverNameComboBox.Text.Substring(keyIndex + 5);
try { serverurl = new Uri("wss://" + hostname + "/control.ashx?key=" + loginkey); } catch (Exception) { }
meshcentral.connect(serverurl, userNameTextBox.Text, passwordTextBox.Text, twoFactorCookie);
}
else
{
try { serverurl = new Uri("wss://" + serverNameComboBox.Text + "/control.ashx"); } catch (Exception) { }
meshcentral.connect(serverurl, userNameTextBox.Text, passwordTextBox.Text, twoFactorCookie);
}
}
}
private void Meshcentral_onLoginTokenChanged()
@@ -915,10 +961,23 @@ namespace MeshCentralRouter
Uri serverurl = null;
if (authLoginUrl != null) {
serverurl = new Uri("wss://" + authLoginUrl.Host + ":" + ((authLoginUrl.Port > 0) ? authLoginUrl.Port : 443) + authLoginUrl.LocalPath + "?auth=" + getValueFromQueryString(authLoginUrl.Query, "c"));
string loginkey = getValueFromQueryString(authLoginUrl.Query, "key");
string urlstring = "wss://" + authLoginUrl.Host + ":" + ((authLoginUrl.Port > 0) ? authLoginUrl.Port : 443) + authLoginUrl.LocalPath + "?auth=" + getValueFromQueryString(authLoginUrl.Query, "c");
if (loginkey != null) { urlstring += ("&key=" + loginkey); }
serverurl = new Uri(urlstring);
} else {
int keyIndex = serverNameComboBox.Text.IndexOf("?key=");
if (keyIndex >= 0)
{
string hostname = serverNameComboBox.Text.Substring(0, keyIndex);
string loginkey = serverNameComboBox.Text.Substring(keyIndex + 5);
serverurl = new Uri("wss://" + hostname + "/control.ashx?key=" + loginkey);
}
else
{
serverurl = new Uri("wss://" + serverNameComboBox.Text + "/control.ashx");
}
}
meshcentral = new MeshCentralServer();
meshcentral.debug = debug;
@@ -1318,6 +1377,7 @@ namespace MeshCentralRouter
if (x.ContainsKey("remoteIP")) { map.remoteIP = (string)x["remoteIP"]; }
map.remotePort = (int)x["remotePort"];
map.appId = (int)x["appId"];
if (x.ContainsKey("autoExit")) { map.autoexit = (bool)x["autoExit"]; }
map.node = node;
if (authLoginUrl != null) { map.host = authLoginUrl.Host + ":" + ((authLoginUrl.Port > 0) ? authLoginUrl.Port : 443); } else { map.host = serverNameComboBox.Text; }
map.authCookie = meshcentral.authCookie;
@@ -1331,11 +1391,17 @@ namespace MeshCentralRouter
// Launch any executable
if (x.ContainsKey("launch")) {
try {
if (x["launch"].GetType() == typeof(int)) { map.appButton_Click(this, null); }
if (x["launch"].GetType() == typeof(string))
{
try
{
string lanuchString = (string)x["launch"];
lanuchString = lanuchString.Replace("{port}", x["localPort"].ToString());
System.Diagnostics.Process.Start(lanuchString);
} catch (Exception) { }
}
catch (Exception) { }
}
}
}
mappingsToSetup = null;

View File

@@ -38,6 +38,7 @@
this.deviceImageList = new System.Windows.Forms.ImageList(this.components);
this.mainContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
this.statsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.autoExitTimer = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.devicePictureBox)).BeginInit();
this.mainContextMenuStrip.SuspendLayout();
this.SuspendLayout();
@@ -99,6 +100,11 @@
resources.ApplyResources(this.statsToolStripMenuItem, "statsToolStripMenuItem");
this.statsToolStripMenuItem.Click += new System.EventHandler(this.statsToolStripMenuItem_Click);
//
// autoExitTimer
//
this.autoExitTimer.Interval = 1000;
this.autoExitTimer.Tick += new System.EventHandler(this.autoExitTimer_Tick);
//
// MapUserControl
//
resources.ApplyResources(this, "$this");
@@ -128,5 +134,6 @@
private System.Windows.Forms.ImageList deviceImageList;
private System.Windows.Forms.ContextMenuStrip mainContextMenuStrip;
private System.Windows.Forms.ToolStripMenuItem statsToolStripMenuItem;
private System.Windows.Forms.Timer autoExitTimer;
}
}

View File

@@ -29,6 +29,8 @@ namespace MeshCentralRouter
public bool xdebug = false;
public bool inaddrany = false;
public MappingStats stats = null;
public bool autoexit = false;
System.Diagnostics.Process autoExitProc = null;
public static void saveToRegistry(string name, string value)
{
@@ -104,6 +106,9 @@ namespace MeshCentralRouter
public void appButton_Click(object sender, EventArgs e)
{
bool shift = false;
if (Control.ModifierKeys == Keys.Shift) { shift = true; }
if (appId == 1) { System.Diagnostics.Process.Start("http://localhost:" + mapper.localport); }
if (appId == 2) { System.Diagnostics.Process.Start("https://localhost:" + mapper.localport); }
if (appId == 3)
@@ -129,40 +134,81 @@ namespace MeshCentralRouter
// Launch the process
try { proc = System.Diagnostics.Process.Start(cmd, args); }
catch (System.ComponentModel.Win32Exception) { }
// Setup auto-exit
if ((autoexit == true) && (autoExitProc == null)) { autoExitProc = proc; autoExitTimer.Enabled = true; }
}
if (appId == 4)
{
string puttyPath = loadFromRegistry("PuttyPath");
if ((shift == false) && (File.Exists(puttyPath)))
{
// Launch the process
System.Diagnostics.Process proc = null;
string args = "-ssh 127.0.0.1 -P " + mapper.localport;
try { proc = System.Diagnostics.Process.Start(puttyPath, args); }
catch (System.ComponentModel.Win32Exception) { }
// Setup auto-exit
if ((autoexit == true) && (autoExitProc == null)) { autoExitProc = proc; autoExitTimer.Enabled = true; }
}
else
{
using (AppLaunchForm f = new AppLaunchForm())
{
System.Diagnostics.Process proc = null;
f.SetAppName(Properties.Resources.PuttyAppName);
f.SetAppLink("http://www.chiark.greenend.org.uk/~sgtatham/putty/");
f.SetAppPath(loadFromRegistry("PuttyPath"));
f.SetAppPath(puttyPath);
if (f.ShowDialog(this) == DialogResult.OK)
{
saveToRegistry("PuttyPath", f.GetAppPath());
string args = "-ssh 127.0.0.1 -P " + mapper.localport;
// Launch the process
try { proc = System.Diagnostics.Process.Start(f.GetAppPath(), args); }
catch (System.ComponentModel.Win32Exception) { }
// Setup auto-exit
if ((autoexit == true) && (autoExitProc == null)) { autoExitProc = proc; autoExitTimer.Enabled = true; }
}
}
}
}
if (appId == 5)
{
string winScpPath = loadFromRegistry("WinSCPPath");
if ((shift == false) && (File.Exists(winScpPath)))
{
// Launch the process
System.Diagnostics.Process proc = null;
string args = "scp://127.0.0.1:" + mapper.localport;
try { proc = System.Diagnostics.Process.Start(winScpPath, args); }
catch (System.ComponentModel.Win32Exception) { }
// Setup auto-exit
if ((autoexit == true) && (autoExitProc == null)) { autoExitProc = proc; autoExitTimer.Enabled = true; }
}
else
{
using (AppLaunchForm f = new AppLaunchForm())
{
System.Diagnostics.Process proc = null;
f.SetAppName(Properties.Resources.WinscpAppName);
f.SetAppLink("http://winscp.net/");
f.SetAppPath(loadFromRegistry("WinSCPPath"));
f.SetAppPath(winScpPath);
if (f.ShowDialog(this) == DialogResult.OK)
{
saveToRegistry("WinSCPPath", f.GetAppPath());
string args = "scp://127.0.0.1:" + mapper.localport;
// Launch the process
try { proc = System.Diagnostics.Process.Start(f.GetAppPath(), args); }
catch (System.ComponentModel.Win32Exception) { }
// Setup auto-exit
if ((autoexit == true) && (autoExitProc == null)) { autoExitProc = proc; autoExitTimer.Enabled = true; }
}
}
}
}
@@ -191,5 +237,11 @@ namespace MeshCentralRouter
stats.Close();
stats = null;
}
private void autoExitTimer_Tick(object sender, EventArgs e)
{
if (autoExitProc == null) return;
if (autoExitProc.HasExited == true) { Application.Exit(); }
}
}
}

View File

@@ -266,7 +266,7 @@
AAEAAAD/////AQAAAAAAAAAMAgAAAFdTeXN0ZW0uV2luZG93cy5Gb3JtcywgVmVyc2lvbj00LjAuMC4w
LCBDdWx0dXJlPW5ldXRyYWwsIFB1YmxpY0tleVRva2VuPWI3N2E1YzU2MTkzNGUwODkFAQAAACZTeXN0
ZW0uV2luZG93cy5Gb3Jtcy5JbWFnZUxpc3RTdHJlYW1lcgEAAAAERGF0YQcCAgAAAAkDAAAADwMAAAAy
mgAAAk1TRnQBSQFMAgEBCAEAAVABAAFQAQABMgEAATIBAAT/AREBAAj/AUIBTQE2BwABNgMAASgDAAHI
mgAAAk1TRnQBSQFMAgEBCAEAAVgBAAFYAQABMgEAATIBAAT/AREBAAj/AUIBTQE2BwABNgMAASgDAAHI
AwABlgMAAQEBAAEQBQABYAHq/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A
/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A
/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A
@@ -929,6 +929,12 @@
<metadata name="mainContextMenuStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>156, 17</value>
</metadata>
<data name="statsToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>108, 22</value>
</data>
<data name="statsToolStripMenuItem.Text" xml:space="preserve">
<value>Stats...</value>
</data>
<data name="mainContextMenuStrip.Size" type="System.Drawing.Size, System.Drawing">
<value>109, 26</value>
</data>
@@ -938,12 +944,9 @@
<data name="&gt;&gt;mainContextMenuStrip.Type" xml:space="preserve">
<value>System.Windows.Forms.ContextMenuStrip, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="statsToolStripMenuItem.Size" type="System.Drawing.Size, System.Drawing">
<value>108, 22</value>
</data>
<data name="statsToolStripMenuItem.Text" xml:space="preserve">
<value>Stats...</value>
</data>
<metadata name="autoExitTimer.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>334, 17</value>
</metadata>
<metadata name="$this.Localizable" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
@@ -951,7 +954,7 @@
<value>6, 13</value>
</data>
<data name="$this.Size" type="System.Drawing.Size, System.Drawing">
<value>425, 58</value>
<value>421, 54</value>
</data>
<data name="&gt;&gt;deviceImageList.Name" xml:space="preserve">
<value>deviceImageList</value>
@@ -965,6 +968,12 @@
<data name="&gt;&gt;statsToolStripMenuItem.Type" xml:space="preserve">
<value>System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;autoExitTimer.Name" xml:space="preserve">
<value>autoExitTimer</value>
</data>
<data name="&gt;&gt;autoExitTimer.Type" xml:space="preserve">
<value>System.Windows.Forms.Timer, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</data>
<data name="&gt;&gt;$this.Name" xml:space="preserve">
<value>MapUserControl</value>
</data>

View File

@@ -1,5 +1,5 @@
/*
Copyright 2009-2018 Intel Corporation
Copyright 2009-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.

View File

@@ -31,6 +31,6 @@ using System.Runtime.InteropServices;
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.1.*")]
//[assembly: AssemblyVersion("1.0.0.0")]
//[assembly: AssemblyFileVersion("1.0.0.0")]