Ejemplo n.º 1
0
        public RemoteControl_ssh(ContentControl contentControl)
            : base(contentControl)
        {
            InitializeComponent();

            try
            {
                _putty               = new Putty();
                _event_error         = Error_Event;
                _event_securityAlert = SecurityAlert_Event;
                _event_connected     = Connected_Event;
                _event_verifyhostkey = VerifyHostKey_Event;
                _event_storehostkey  = StoreHostKey_Event;
                _event_keydown       = KeyDown_Event;
                _event_mousemove     = MouseMove_Event;
                _event_debug         = Debug_Event;

                _putty.SetCallback(_event_error, _event_connected, _event_securityAlert, _event_verifyhostkey,
                                   _event_storehostkey, _event_keydown, _event_mousemove, _event_debug);
                if (!_putty.Init())
                {
                    string errorText = Marshal.PtrToStringAnsi(_putty.GetError());
                    throw new Exception(errorText);
                }
            }
            catch (Exception e)
            {
                throw new Exception($"加载putty失败,{e.Message}。");
            }
        }
        public RemoteControl_telnet(ContentControl contentControl)
            : base(contentControl)
        {
            InitializeComponent();
            try
            {
                _putty           = new Putty();
                _event_error     = Error_Event;
                _event_connected = Connected_Event;
                _event_keydown   = KeyDown_Event;
                _event_mousemove = MouseMove_Event;
                _event_debug     = OnDebug;

                _putty.SetCallback(_event_error, _event_connected, null, null, null, _event_keydown, _event_mousemove, _event_debug);
                if (!_putty.Init())
                {
                    string errorText = Marshal.PtrToStringAnsi(_putty.GetError());
                    throw new Exception(errorText);
                }
            }
            catch (Exception e)
            {
                throw new Exception($"加载putty失败,{e.Message}。");
            }
        }
        public override void Connect(DbItemRemoteLink linkSettings, FinalItemSetting finalItemSetting)
        {
            string[] values = linkSettings.Server.Split(':');
            if (values.Length > 2)
            {
                throw new Exception($"服务器地址不正确。");                   //地址不正确
            }
            string server = values[0];
            string port   = values.Length == 2 ? $"-P {values[1]}" : "-P 23";

            FinalItemSetting_telnet finalSetting = (FinalItemSetting_telnet)finalItemSetting;

            IntPtr parentHwnd = FormsControl.Handle;
            Window windows    = Window.GetWindow(this);
            int    width      = (int)windows.Width - 4;
            int    height     = (int)windows.Height - 34;

            Task.Run(() =>
            {
                Putty.Settings settings = new Putty.Settings
                {
                    fontname          = finalSetting.FontName,
                    fontsize          = finalSetting.FontSize,
                    curtype           = finalSetting.Cursor - 1,
                    linecodepage      = Encoding.GetEncoding(finalSetting.Character).BodyName,
                    backspaceisdelete = finalSetting.Fallbackkeys - 1,
                    mouseisxterm      = finalSetting.MouseAction - 1,
                    puttycolor        = new Putty.PuttyColor(finalSetting.GetPuttyColor()),
                    rxvthomeend       = finalSetting.HomeAndEnd - 1,
                    functionkeys      = finalSetting.FnAndKeypad - 1,
                    cjkambigwide      = finalSetting.CJKAmbigWide.Value,
                    capslockcyr       = finalSetting.CapsLockCyr.Value,
                    crimplieslf       = finalSetting.CRImpliesLF.Value,
                    lfimpliescr       = finalSetting.LFImpliesCR.Value,
                };

                string user = string.IsNullOrEmpty(linkSettings.UserName) ? "" : $"-l {linkSettings.UserName}";
                string pw   = string.IsNullOrEmpty(linkSettings.Password) ? "" : $"-pw {linkSettings.Password}";

                if (!_putty.Create(parentHwnd, $"-telnet {port} {user} {pw} {server}", 0, 0, width, height, settings))
                {
                    string errorText = Marshal.PtrToStringAnsi(_putty.GetError());
                    Dispatcher.Invoke(() => { OnFatalError?.Invoke("错误", errorText); });
                }

                _putty.Dispose();
                _putty = null;
                GC.Collect();
                GC.SuppressFinalize(this);
            });
        }
Ejemplo n.º 4
0
        public override void Connect(DbItemRemoteLink linkSettings, DbItemSetting lastSetting)
        {
            DbItemSettingSsh lastSettingSsh = lastSetting as DbItemSettingSsh;

            if (lastSettingSsh == null)
            {
                return;
            }

            string[] values = linkSettings.Server.Split(':');
            if (values.Length > 2)
            {
                throw new Exception($"服务器地址不正确。");                   //地址不正确
            }
            string server = values[0];
            string port   = values.Length == 2 ? $"-P {values[1]}" : "-P 22";

            IntPtr parentHwnd = FormsControl.Handle;
            Window windows    = Window.GetWindow(this);
            int    width      = (int)windows.Width - 4;
            int    height     = (int)windows.Height - 34;
            Thread thread     = new Thread(() =>
            {
                Putty.Settings settings = new Putty.Settings
                {
                    fontname          = lastSettingSsh.FontName,
                    fontsize          = lastSettingSsh.FontSize,
                    curtype           = lastSettingSsh.Cursor - 1,
                    linecodepage      = Encoding.GetEncoding(lastSettingSsh.Character).BodyName,
                    backspaceisdelete = lastSettingSsh.Fallbackkeys - 1,
                    mouseisxterm      = lastSettingSsh.MouseAction - 1,
                    puttycolor        = new Putty.PuttyColor(lastSettingSsh.GetPuttyColor()),
                    rxvthomeend       = lastSettingSsh.HomeAndEnd - 1,
                    functionkeys      = lastSettingSsh.FnAndKeypad - 1,
                    cjkambigwide      = lastSettingSsh.CJKAmbigWide.Value,
                    capslockcyr       = lastSettingSsh.CapsLockCyr.Value,
                    crimplieslf       = lastSettingSsh.CRImpliesLF.Value,
                    lfimpliescr       = lastSettingSsh.LFImpliesCR.Value,
                };

                string user = string.IsNullOrEmpty(linkSettings.UserName) ? "" : $"-l {linkSettings.UserName}";
                string pw   = null;
                if (string.IsNullOrWhiteSpace(linkSettings.PrivateKey))
                {
                    pw = string.IsNullOrEmpty(linkSettings.Password) ? "" : $"-pw {linkSettings.Password}";
                }
                else
                {
                    _privateKey = Path.GetTempPath() + linkSettings.Id;
                    using (StreamWriter streamWriter = new StreamWriter(_privateKey, false, Encoding.ASCII))
                    {
                        streamWriter.Write(linkSettings.PrivateKey);
                        streamWriter.Flush();
                    }
                    pw = $"-i \"{_privateKey}\"";
                }

                if (!_putty.Create(parentHwnd, $"-ssh {port} {user} {pw} {server}", 0, 0, width, height, settings))
                {
                    string errorText = Marshal.PtrToStringAnsi(_putty.GetError());
                    Dispatcher.Invoke(() => { OnFatalError?.Invoke("错误", errorText); });
                }

                _putty.Dispose();
                _putty = null;
                if (_privateKey != null)
                {
                    File.Delete(_privateKey);
                }
                GC.Collect();
                GC.SuppressFinalize(this);
            });

            thread.Start();
        }