manual merge from Filippo125:add_winbox - save only

This commit is contained in:
Dimitrij
2022-01-18 11:31:23 +00:00
parent 859d12b450
commit 05c8da3ee4
8 changed files with 283 additions and 4 deletions

View File

@@ -1,4 +1,4 @@
using System;
using System;
using System.Drawing;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
@@ -11,7 +11,7 @@ using mRemoteNG.Messages;
using mRemoteNG.Tools;
using mRemoteNG.UI.Controls;
using mRemoteNG.UI.Forms;
using mRemoteNG.Connection.Protocol.Winbox;
namespace mRemoteNG.Config.Settings
{
@@ -69,6 +69,7 @@ namespace mRemoteNG.Config.Settings
SetKioskMode();
SetPuttyPath();
SetWinboxPath();
SetShowSystemTrayIcon();
SetAutoSave();
LoadExternalAppsFromXml();
@@ -169,6 +170,15 @@ namespace mRemoteNG.Config.Settings
: GeneralAppInfo.PuttyPath;
}
private static void SetWinboxPath()
{
ProtocolWinbox.WinboxPath = "C:\\Program Files (x86)\\winbox.exe";
// mRemoteNG.Settings.Default.UseCustomPuttyPath
// ? mRemoteNG.Settings.Default.CustomPuttyPath
// : GeneralAppInfo.PuttyPath;
}
private void EnsureSettingsAreSavedInNewestVersion()
{
if (Properties.Settings.Default.DoUpgrade)

View File

@@ -18,6 +18,7 @@ using mRemoteNG.Properties;
using mRemoteNG.Tree;
using mRemoteNG.Resources.Language;
using mRemoteNG.Tree.Root;
using mRemoteNG.Connection.Protocol.Winbox;
namespace mRemoteNG.Connection
@@ -274,6 +275,8 @@ namespace mRemoteNG.Connection
return (int)ProtocolPowerShell.Defaults.Port;
case ProtocolType.IntApp:
return (int)IntegratedProgram.Defaults.Port;
case ProtocolType.Winbox:
return (int)ProtocolWinbox.Defaults.Port;
}
return 0;

View File

@@ -9,6 +9,7 @@ using System;
using mRemoteNG.Connection.Protocol.PowerShell;
using mRemoteNG.Properties;
using mRemoteNG.Resources.Language;
using mRemoteNG.Connection.Protocol.Winbox;
namespace mRemoteNG.Connection.Protocol
{
@@ -49,6 +50,8 @@ namespace mRemoteNG.Connection.Protocol
throw (new Exception(Language.NoExtAppDefined));
}
return new IntegratedProgram();
case ProtocolType.Winbox:
return new ProtocolWinbox();
}
return default(ProtocolBase);

View File

@@ -36,6 +36,9 @@ namespace mRemoteNG.Connection.Protocol
PowerShell = 10,
[LocalizedAttributes.LocalizedDescription(nameof(Language.ExternalTool))]
IntApp = 20
IntApp = 20,
[LocalizedAttributes.LocalizedDescription(nameof(Language.Winbox))]
Winbox = 11
}
}

View File

@@ -0,0 +1,246 @@
using System;
using System.Threading;
using mRemoteNG.App;
using mRemoteNG.Messages;
using mRemoteNG.Security.SymmetricEncryption;
using mRemoteNG.Tools.Cmdline;
using mRemoteNG.UI;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using mRemoteNG.Resources.Language;
using mRemoteNG.Properties;
namespace mRemoteNG.Connection.Protocol.Winbox
{
public class ProtocolWinbox : ProtocolBase
{
public ProtocolWinbox() { }
public enum Defaults
{
Port = 8291
}
private const int IDM_RECONF = 0x50; // PuTTY Settings Menu ID
private readonly DisplayProperties _display = new DisplayProperties();
#region Public Properties
public IntPtr WinboxHandle { get; set; }
private Process WinboxProcess { get; set; }
public static string WinboxPath { get; set; }
public bool Focused
{
get { return NativeMethods.GetForegroundWindow() == WinboxHandle; }
}
#endregion
#region Private Events & Handlers
private void ProcessExited(object sender, EventArgs e)
{
Event_Closed(this);
}
#endregion
#region Public Methods
public override bool Connect()
{
try
{
WinboxProcess = new Process
{
StartInfo =
{
UseShellExecute = true,
FileName = WinboxPath
}
};
var arguments = new CommandLineArguments { EscapeForShell = false };
var username = "";
var password = "";
var host = InterfaceControl.Info.Hostname;
if (!string.IsNullOrEmpty(InterfaceControl.Info?.Username))
{
username = InterfaceControl.Info.Username;
}
else
{
// ReSharper disable once SwitchStatementMissingSomeCases
switch (Settings.Default.EmptyCredentials)
{
case "windows":
username = Environment.UserName;
break;
case "custom":
username = Settings.Default.DefaultUsername;
break;
}
}
if (!string.IsNullOrEmpty(InterfaceControl.Info?.Password))
{
password = InterfaceControl.Info.Password;
}
else
{
if (Settings.Default.EmptyCredentials == "custom")
{
var cryptographyProvider = new LegacyRijndaelCryptographyProvider();
password = cryptographyProvider.Decrypt(Settings.Default.DefaultPassword,
Runtime.EncryptionKey);
}
}
if (!InterfaceControl.Info.Port.Equals(Defaults.Port))
{
host += ":" + InterfaceControl.Info.Port.ToString();
}
arguments.Add(host, username, password);
WinboxProcess.StartInfo.Arguments = arguments.ToString();
WinboxProcess.EnableRaisingEvents = true;
WinboxProcess.Exited += ProcessExited;
WinboxProcess.Start();
WinboxProcess.WaitForInputIdle(Settings.Default.MaxPuttyWaitTime * 1000);
var startTicks = Environment.TickCount;
while (WinboxHandle.ToInt32() == 0 &
Environment.TickCount < startTicks + Settings.Default.MaxPuttyWaitTime * 1000)
{
//WinboxHandle = NativeMethods.FindWindowEx(InterfaceControl.Handle, new IntPtr(0), null, null);
WinboxProcess.Refresh();
WinboxHandle = WinboxProcess.MainWindowHandle;
if (WinboxHandle.ToInt32() == 0)
{
Thread.Sleep(0);
}
}
NativeMethods.SetParent(WinboxHandle, InterfaceControl.Handle);
Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, Language.PuttyStuff, true);
Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, string.Format(Language.PuttyHandle, WinboxHandle), true);
Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, string.Format(Language.PuttyTitle, WinboxProcess.MainWindowTitle), true);
//::TODO not found putty parent handle
//Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, string.Format(Language.PuttyParentHandle, InterfaceControl.Parent.Handle), true);
Resize(this, new EventArgs());
base.Connect();
return true;
}
catch (Exception ex)
{
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg,
Language.ConnectionFailed + Environment.NewLine +
ex.Message);
return false;
}
}
public override void Focus()
{
try
{
NativeMethods.SetForegroundWindow(WinboxHandle);
}
catch (Exception ex)
{
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.PuttyFocusFailed + Environment.NewLine + ex.Message, true);
}
}
public override void Resize(object sender, EventArgs e)
{
try
{
if (InterfaceControl.Size == Size.Empty)
return;
//NativeMethods.MoveWindow(WinboxHandle, 0, 0, InterfaceControl.Width, InterfaceControl.Height, true);
var scaledFrameBorderHeight = _display.ScaleHeight(SystemInformation.FrameBorderSize.Height);
var scaledFrameBorderWidth = _display.ScaleWidth(SystemInformation.FrameBorderSize.Width);
NativeMethods.MoveWindow(WinboxHandle, -scaledFrameBorderWidth,
-(SystemInformation.CaptionHeight + scaledFrameBorderHeight),
InterfaceControl.Width + scaledFrameBorderWidth * 2,
InterfaceControl.Height + SystemInformation.CaptionHeight +
scaledFrameBorderHeight * 2,
true);
}
catch (Exception ex)
{
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.PuttyResizeFailed + Environment.NewLine + ex.Message, true);
}
}
public override void Close()
{
try
{
if (WinboxProcess.HasExited == false)
{
WinboxProcess.Kill();
}
}
catch (Exception ex)
{
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.PuttyKillFailed + Environment.NewLine + ex.Message, true);
}
try
{
WinboxProcess.Dispose();
}
catch (Exception ex)
{
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.PuttyDisposeFailed + Environment.NewLine + ex.Message, true);
}
base.Close();
}
public void ShowSettingsDialog()
{
try
{
NativeMethods.PostMessage(WinboxHandle, NativeMethods.WM_SYSCOMMAND, (IntPtr)IDM_RECONF, (IntPtr)0);
NativeMethods.SetForegroundWindow(WinboxHandle);
}
catch (Exception ex)
{
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.PuttyShowSettingsDialogFailed + Environment.NewLine + ex.Message, true);
}
}
#endregion
#region Enums
protected enum Winbox_Protocol
{
winbox = 0,
}
#endregion
}
}

View File

@@ -6353,7 +6353,18 @@ namespace mRemoteNG.Resources.Language {
return ResourceManager.GetString("Windows", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Windows.
/// </summary>
internal static string Winbox
{
get
{
return ResourceManager.GetString("Winbox", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Working directory.
/// </summary>

View File

@@ -2223,4 +2223,7 @@ Nightly Channel includes Alphas, Betas &amp; Release Candidates.</value>
<data name="PropertyDescriptionEC2Region" xml:space="preserve">
<value>fetch aws instance info from this region</value>
</data>
<data name="Winbox" xml:space="preserve">
<value>Winbox</value>
</data>
</root>

BIN
mRemoteNG/winbox.exe Normal file

Binary file not shown.