diff --git a/MainForm.cs b/MainForm.cs index 0e5a49b..573e2ea 100644 --- a/MainForm.cs +++ b/MainForm.cs @@ -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 map = new Dictionary(); + 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,14 +421,28 @@ 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"); - meshcentral.connect(serverurl, userNameTextBox.Text, passwordTextBox.Text, twoFactorCookie); + 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); + } } } @@ -915,9 +961,22 @@ 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 { - 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); + serverurl = new Uri("wss://" + hostname + "/control.ashx?key=" + loginkey); + } + else + { + serverurl = new Uri("wss://" + serverNameComboBox.Text + "/control.ashx"); + } } meshcentral = new MeshCentralServer(); @@ -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 { - string lanuchString = (string)x["launch"]; - lanuchString = lanuchString.Replace("{port}", x["localPort"].ToString()); - System.Diagnostics.Process.Start(lanuchString); - } catch (Exception) { } + 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) { } + } } } mappingsToSetup = null; diff --git a/MapUserControl.Designer.cs b/MapUserControl.Designer.cs index 2c07d27..5a53e55 100644 --- a/MapUserControl.Designer.cs +++ b/MapUserControl.Designer.cs @@ -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; } } diff --git a/MapUserControl.cs b/MapUserControl.cs index a9e3c87..e1ac510 100644 --- a/MapUserControl.cs +++ b/MapUserControl.cs @@ -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) { - using (AppLaunchForm f = new AppLaunchForm()) + string puttyPath = loadFromRegistry("PuttyPath"); + if ((shift == false) && (File.Exists(puttyPath))) { + // Launch the process System.Diagnostics.Process proc = null; - f.SetAppName(Properties.Resources.PuttyAppName); - f.SetAppLink("http://www.chiark.greenend.org.uk/~sgtatham/putty/"); - f.SetAppPath(loadFromRegistry("PuttyPath")); - if (f.ShowDialog(this) == DialogResult.OK) + 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()) { - 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) { } + System.Diagnostics.Process proc = null; + f.SetAppName(Properties.Resources.PuttyAppName); + f.SetAppLink("http://www.chiark.greenend.org.uk/~sgtatham/putty/"); + 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) { - using (AppLaunchForm f = new AppLaunchForm()) + string winScpPath = loadFromRegistry("WinSCPPath"); + if ((shift == false) && (File.Exists(winScpPath))) { + // Launch the process System.Diagnostics.Process proc = null; - f.SetAppName(Properties.Resources.WinscpAppName); - f.SetAppLink("http://winscp.net/"); - f.SetAppPath(loadFromRegistry("WinSCPPath")); - if (f.ShowDialog(this) == DialogResult.OK) + 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()) { - 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) { } + System.Diagnostics.Process proc = null; + f.SetAppName(Properties.Resources.WinscpAppName); + f.SetAppLink("http://winscp.net/"); + 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(); } + } } } diff --git a/MapUserControl.resx b/MapUserControl.resx index 7f2ee4a..37bc4d0 100644 --- a/MapUserControl.resx +++ b/MapUserControl.resx @@ -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 @@ 156, 17 + + 108, 22 + + + Stats... + 109, 26 @@ -938,12 +944,9 @@ System.Windows.Forms.ContextMenuStrip, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - 108, 22 - - - Stats... - + + 334, 17 + True @@ -951,7 +954,7 @@ 6, 13 - 425, 58 + 421, 54 deviceImageList @@ -965,6 +968,12 @@ System.Windows.Forms.ToolStripMenuItem, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + autoExitTimer + + + System.Windows.Forms.Timer, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + MapUserControl diff --git a/MeshCentralServer.cs b/MeshCentralServer.cs index 271aa83..4bb6822 100644 --- a/MeshCentralServer.cs +++ b/MeshCentralServer.cs @@ -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. diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index d3360c5..d9421e7 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -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")]