mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-17 22:11:48 +08:00
347 lines
9.5 KiB
C#
347 lines
9.5 KiB
C#
using System;
|
|
using System.Threading;
|
|
using System.Timers;
|
|
using System.Windows.Forms;
|
|
using AxWFICALib;
|
|
using mRemoteNG.App;
|
|
using mRemoteNG.Connection.Protocol.RDP;
|
|
using mRemoteNG.Messages;
|
|
using mRemoteNG.Security.SymmetricEncryption;
|
|
using mRemoteNG.Tools;
|
|
using mRemoteNG.UI.Forms;
|
|
|
|
|
|
namespace mRemoteNG.Connection.Protocol.ICA
|
|
{
|
|
public class IcaProtocol : ProtocolBase
|
|
{
|
|
private AxICAClient _icaClient;
|
|
private readonly FrmMain _frmMain = FrmMain.Default;
|
|
|
|
#region Public Methods
|
|
public IcaProtocol(ConnectionInfo connectionInfo)
|
|
: base(connectionInfo)
|
|
{
|
|
try
|
|
{
|
|
Control = new AxICAClient();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.strIcaControlFailed + Environment.NewLine + ex.Message, true);
|
|
}
|
|
}
|
|
|
|
public override bool Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
try
|
|
{
|
|
_icaClient = (AxICAClient)Control;
|
|
_icaClient.CreateControl();
|
|
|
|
while (!_icaClient.Created)
|
|
{
|
|
Thread.Sleep(10);
|
|
Application.DoEvents();
|
|
}
|
|
|
|
_icaClient.Address = Info.Hostname;
|
|
SetCredentials();
|
|
SetResolution();
|
|
SetColors();
|
|
SetSecurity();
|
|
|
|
//Disable hotkeys for international users
|
|
_icaClient.Hotkey1Shift = null;
|
|
_icaClient.Hotkey1Char = null;
|
|
_icaClient.Hotkey2Shift = null;
|
|
_icaClient.Hotkey2Char = null;
|
|
_icaClient.Hotkey3Shift = null;
|
|
_icaClient.Hotkey3Char = null;
|
|
_icaClient.Hotkey4Shift = null;
|
|
_icaClient.Hotkey4Char = null;
|
|
_icaClient.Hotkey5Shift = null;
|
|
_icaClient.Hotkey5Char = null;
|
|
_icaClient.Hotkey6Shift = null;
|
|
_icaClient.Hotkey6Char = null;
|
|
_icaClient.Hotkey7Shift = null;
|
|
_icaClient.Hotkey7Char = null;
|
|
_icaClient.Hotkey8Shift = null;
|
|
_icaClient.Hotkey8Char = null;
|
|
_icaClient.Hotkey9Shift = null;
|
|
_icaClient.Hotkey9Char = null;
|
|
_icaClient.Hotkey10Shift = null;
|
|
_icaClient.Hotkey10Char = null;
|
|
_icaClient.Hotkey11Shift = null;
|
|
_icaClient.Hotkey11Char = null;
|
|
|
|
_icaClient.PersistentCacheEnabled = Info.CacheBitmaps;
|
|
_icaClient.Title = Info.Name;
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.strIcaSetPropsFailed + Environment.NewLine + ex.Message, true);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public override bool Connect()
|
|
{
|
|
SetEventHandlers();
|
|
|
|
try
|
|
{
|
|
_icaClient.Connect();
|
|
base.Connect();
|
|
return true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.strIcaConnectionFailed + Environment.NewLine + ex.Message);
|
|
return false;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Private Methods
|
|
private void SetCredentials()
|
|
{
|
|
try
|
|
{
|
|
if (((int)Force & (int)ConnectionInfo.Force.NoCredentials) == (int)ConnectionInfo.Force.NoCredentials)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var user = Info?.Username ?? "";
|
|
var pass = Info?.Password ?? "";
|
|
var dom = Info?.Domain ?? "";
|
|
|
|
if (string.IsNullOrEmpty(user))
|
|
{
|
|
if (Settings.Default.EmptyCredentials == "windows")
|
|
{
|
|
_icaClient.Username = Environment.UserName;
|
|
}
|
|
else if (Settings.Default.EmptyCredentials == "custom")
|
|
{
|
|
_icaClient.Username = Settings.Default.DefaultUsername;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_icaClient.Username = user;
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(pass))
|
|
{
|
|
if (Settings.Default.EmptyCredentials == "custom")
|
|
{
|
|
if (Settings.Default.DefaultPassword != "")
|
|
{
|
|
var cryptographyProvider = new LegacyRijndaelCryptographyProvider();
|
|
_icaClient.SetProp("ClearPassword", cryptographyProvider.Decrypt(Settings.Default.DefaultPassword, Runtime.EncryptionKey));
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_icaClient.SetProp("ClearPassword", pass);
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(dom))
|
|
{
|
|
if (Settings.Default.EmptyCredentials == "windows")
|
|
{
|
|
_icaClient.Domain = Environment.UserDomainName;
|
|
}
|
|
else if (Settings.Default.EmptyCredentials == "custom")
|
|
{
|
|
_icaClient.Domain = Settings.Default.DefaultDomain;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_icaClient.Domain = dom;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.strIcaSetCredentialsFailed + Environment.NewLine + ex.Message, true);
|
|
}
|
|
}
|
|
|
|
private void SetResolution()
|
|
{
|
|
try
|
|
{
|
|
if ((Force & ConnectionInfo.Force.Fullscreen) == ConnectionInfo.Force.Fullscreen)
|
|
{
|
|
_icaClient.SetWindowSize(WFICALib.ICAWindowType.WindowTypeClient, Screen.FromControl(_frmMain).Bounds.Width, Screen.FromControl(_frmMain).Bounds.Height, 0);
|
|
_icaClient.FullScreenWindow();
|
|
|
|
return;
|
|
}
|
|
|
|
if (Info.Resolution == RdpResolutions.FitToWindow)
|
|
{
|
|
_icaClient.SetWindowSize(WFICALib.ICAWindowType.WindowTypeClient, InterfaceControl.Size.Width, InterfaceControl.Size.Height, 0);
|
|
}
|
|
else if (Info.Resolution == RdpResolutions.SmartSize)
|
|
{
|
|
_icaClient.SetWindowSize(WFICALib.ICAWindowType.WindowTypeClient, InterfaceControl.Size.Width, InterfaceControl.Size.Height, 0);
|
|
}
|
|
else if (Info.Resolution == RdpResolutions.Fullscreen)
|
|
{
|
|
_icaClient.SetWindowSize(WFICALib.ICAWindowType.WindowTypeClient, Screen.FromControl(_frmMain).Bounds.Width, Screen.FromControl(_frmMain).Bounds.Height, 0);
|
|
_icaClient.FullScreenWindow();
|
|
}
|
|
else
|
|
{
|
|
var resolution = Info.Resolution.GetResolutionRectangle();
|
|
_icaClient.SetWindowSize(WFICALib.ICAWindowType.WindowTypeClient, resolution.Width, resolution.Height, 0);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.strIcaSetResolutionFailed + Environment.NewLine + ex.Message, true);
|
|
}
|
|
}
|
|
|
|
private void SetColors()
|
|
{
|
|
// ReSharper disable once SwitchStatementMissingSomeCases
|
|
switch (Info.Colors)
|
|
{
|
|
case RdpColors.Colors256:
|
|
_icaClient.SetProp("DesiredColor", "2");
|
|
break;
|
|
case RdpColors.Colors15Bit:
|
|
_icaClient.SetProp("DesiredColor", "4");
|
|
break;
|
|
case RdpColors.Colors16Bit:
|
|
_icaClient.SetProp("DesiredColor", "4");
|
|
break;
|
|
default:
|
|
_icaClient.SetProp("DesiredColor", "8");
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void SetSecurity()
|
|
{
|
|
// ReSharper disable once SwitchStatementMissingSomeCases
|
|
switch (Info.ICAEncryptionStrength)
|
|
{
|
|
case EncryptionStrength.Encr128BitLogonOnly:
|
|
_icaClient.Encrypt = true;
|
|
_icaClient.EncryptionLevelSession = "EncRC5-0";
|
|
break;
|
|
case EncryptionStrength.Encr40Bit:
|
|
_icaClient.Encrypt = true;
|
|
_icaClient.EncryptionLevelSession = "EncRC5-40";
|
|
break;
|
|
case EncryptionStrength.Encr56Bit:
|
|
_icaClient.Encrypt = true;
|
|
_icaClient.EncryptionLevelSession = "EncRC5-56";
|
|
break;
|
|
case EncryptionStrength.Encr128Bit:
|
|
_icaClient.Encrypt = true;
|
|
_icaClient.EncryptionLevelSession = "EncRC5-128";
|
|
break;
|
|
}
|
|
}
|
|
|
|
private void SetEventHandlers()
|
|
{
|
|
try
|
|
{
|
|
_icaClient.OnConnecting += ICAEvent_OnConnecting;
|
|
_icaClient.OnConnect += ICAEvent_OnConnected;
|
|
_icaClient.OnConnectFailed += ICAEvent_OnConnectFailed;
|
|
_icaClient.OnDisconnect += ICAEvent_OnDisconnect;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.strIcaSetEventHandlersFailed + Environment.NewLine + ex.Message, true);
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Private Events & Handlers
|
|
private void ICAEvent_OnConnecting(object sender, EventArgs e)
|
|
{
|
|
RaiseConnectionConnectingEvent(this);
|
|
}
|
|
|
|
private void ICAEvent_OnConnected(object sender, EventArgs e)
|
|
{
|
|
RaiseConnectionConnectedEvent(this);
|
|
}
|
|
|
|
private void ICAEvent_OnConnectFailed(object sender, EventArgs e)
|
|
{
|
|
RaiseErrorOccuredEvent(this, e.ToString());
|
|
}
|
|
|
|
private void ICAEvent_OnDisconnect(object sender, EventArgs e)
|
|
{
|
|
RaiseConnectionDisconnectedEvent(this, e.ToString());
|
|
|
|
if (Settings.Default.ReconnectOnDisconnect)
|
|
{
|
|
ReconnectGroup = new ReconnectGroup();
|
|
//this.Load += ReconnectGroup_Load;
|
|
ReconnectGroup.Left = (int) (((double) Control.Width / 2) - ((double) ReconnectGroup.Width / 2));
|
|
ReconnectGroup.Top = (int) (((double) Control.Height / 2) - ((double) ReconnectGroup.Height / 2));
|
|
ReconnectGroup.Parent = Control;
|
|
ReconnectGroup.Show();
|
|
tmrReconnect.Enabled = true;
|
|
}
|
|
else
|
|
{
|
|
Close();
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region Reconnect Stuff
|
|
public void tmrReconnect_Elapsed(object sender, ElapsedEventArgs e)
|
|
{
|
|
var srvReady = PortScanner.IsPortOpen(Info.Hostname, Convert.ToString(Info.Port));
|
|
|
|
ReconnectGroup.ServerReady = srvReady;
|
|
|
|
if (!ReconnectGroup.ReconnectWhenReady || !srvReady) return;
|
|
tmrReconnect.Enabled = false;
|
|
ReconnectGroup.DisposeReconnectGroup();
|
|
_icaClient.Connect();
|
|
}
|
|
#endregion
|
|
|
|
#region Enums
|
|
public enum Defaults
|
|
{
|
|
Port = 1494,
|
|
EncryptionStrength = 0
|
|
}
|
|
|
|
public enum EncryptionStrength
|
|
{
|
|
[LocalizedAttributes.LocalizedDescription("strEncBasic")]
|
|
EncrBasic = 1,
|
|
[LocalizedAttributes.LocalizedDescription("strEnc128BitLogonOnly")]
|
|
Encr128BitLogonOnly = 127,
|
|
[LocalizedAttributes.LocalizedDescription("strEnc40Bit")]
|
|
Encr40Bit = 40,
|
|
[LocalizedAttributes.LocalizedDescription("strEnc56Bit")]
|
|
Encr56Bit = 56,
|
|
[LocalizedAttributes.LocalizedDescription("strEnc128Bit")]
|
|
Encr128Bit = 128
|
|
}
|
|
#endregion
|
|
}
|
|
} |