Beispiel #1
0
        public override bool Connect()
        {
            try
            {
                _isPuttyNg = PuttyTypeDetector.GetPuttyType() == PuttyTypeDetector.PuttyType.PuttyNg;

                PuttyProcess = new Process
                {
                    StartInfo =
                    {
                        UseShellExecute = false,
                        FileName        = PuttyPath
                    }
                };

                var arguments = new CommandLineArguments {
                    EscapeForShell = false
                };

                arguments.Add("-load", InterfaceControl.Info.PuttySession);

                if (!(InterfaceControl.Info is PuttySessionInfo))
                {
                    arguments.Add("-" + PuttyProtocol);

                    if (PuttyProtocol == Putty_Protocol.ssh)
                    {
                        var username = "";
                        var password = "";

                        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);
                            }
                        }

                        arguments.Add("-" + (int)PuttySSHVersion);

                        if (!Force.HasFlag(ConnectionInfo.Force.NoCredentials))
                        {
                            if (!string.IsNullOrEmpty(username))
                            {
                                arguments.Add("-l", username);
                            }

                            if (!string.IsNullOrEmpty(password))
                            {
                                arguments.Add("-pw", password);
                            }
                        }
                    }

                    arguments.Add("-P", InterfaceControl.Info.Port.ToString());
                    arguments.Add(InterfaceControl.Info.Hostname);
                }

                if (_isPuttyNg)
                {
                    arguments.Add("-hwndparent", InterfaceControl.Handle.ToString());
                }

                PuttyProcess.StartInfo.Arguments = arguments.ToString();

                PuttyProcess.EnableRaisingEvents = true;
                PuttyProcess.Exited += ProcessExited;

                PuttyProcess.Start();
                PuttyProcess.WaitForInputIdle(Settings.Default.MaxPuttyWaitTime * 1000);

                var startTicks = Environment.TickCount;
                while (PuttyHandle.ToInt32() == 0 &
                       Environment.TickCount < startTicks + Settings.Default.MaxPuttyWaitTime * 1000)
                {
                    if (_isPuttyNg)
                    {
                        PuttyHandle = NativeMethods.FindWindowEx(
                            InterfaceControl.Handle, new IntPtr(0), null, null);
                    }
                    else
                    {
                        PuttyProcess.Refresh();
                        PuttyHandle = PuttyProcess.MainWindowHandle;
                    }

                    if (PuttyHandle.ToInt32() == 0)
                    {
                        Thread.Sleep(0);
                    }
                }

                if (!_isPuttyNg)
                {
                    NativeMethods.SetParent(PuttyHandle, InterfaceControl.Handle);
                }

                Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, Language.strPuttyStuff, true);
                Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg,
                                                    string.Format(Language.strPuttyHandle, PuttyHandle), true);
                Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg,
                                                    string.Format(Language.strPuttyTitle, PuttyProcess.MainWindowTitle),
                                                    true);
                Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg,
                                                    string.Format(Language.strPuttyParentHandle,
                                                                  InterfaceControl.Parent.Handle), true);

                Resize(this, new EventArgs());
                base.Connect();
                return(true);
            }
            catch (Exception ex)
            {
                Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg,
                                                    Language.strPuttyConnectionFailed + Environment.NewLine +
                                                    ex.Message);
                return(false);
            }
        }
Beispiel #2
0
            public override bool Connect()
            {
                try
                {
                    if (!File.Exists(PuttyPath))
                    {
                        throw new Exception("PuTTY not found.");
                    }
                    _isPuttyNg = IsFilePuttyNg(PuttyPath);

                    PuttyProcess = new Process();
                    var _with1 = PuttyProcess.StartInfo;
                    _with1.UseShellExecute = false;
                    _with1.FileName        = PuttyPath;

                    var arguments = new CommandLineArguments {
                        EscapeForShell = false
                    };

                    arguments.Add("-load", InterfaceControl.Info.PuttySession);
                    arguments.Add("-" + _PuttyProtocol);

                    if (_PuttyProtocol == Putty_Protocol.ssh)
                    {
                        string username = "";
                        string password = "";

                        if (!string.IsNullOrEmpty(InterfaceControl.Info.Username))
                        {
                            username = InterfaceControl.Info.Username;
                        }
                        else
                        {
                            if (Settings.Default.EmptyCredentials == "windows")
                            {
                                username = Environment.UserName;
                            }
                            else if (Settings.Default.EmptyCredentials == "custom")
                            {
                                username = Settings.Default.DefaultUsername;
                            }
                        }

                        if (!string.IsNullOrEmpty(InterfaceControl.Info.Password))
                        {
                            password = InterfaceControl.Info.Password;
                        }
                        else
                        {
                            if (Settings.Default.EmptyCredentials == "custom")
                            {
                                password = Security.Crypt.Decrypt(Settings.Default.DefaultPassword, AppInfo.General.EncryptionKey);
                            }
                        }

                        arguments.Add("-" + (int)_PuttySSHVersion);
                        if (!string.IsNullOrEmpty(username))
                        {
                            arguments.Add("-l", username);
                        }
                        if (!string.IsNullOrEmpty(password))
                        {
                            arguments.Add("-pw", password);
                        }
                    }

                    arguments.Add("-P", InterfaceControl.Info.Port.ToString());
                    arguments.Add(InterfaceControl.Info.Hostname);

                    if (_isPuttyNg)
                    {
                        arguments.Add("-hwndparent", InterfaceControl.Handle.ToString());
                    }

                    _with1.Arguments = arguments.ToString();


                    //REMOVE IN RELEASE!
#if DEBUG
                    Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg,
                                                        "PuTTY Arguments: " + PuttyProcess.StartInfo.Arguments, true);
                    Debug.WriteLine("PuTTY Arguments: " + PuttyProcess.StartInfo.Arguments);
#endif

                    PuttyProcess.EnableRaisingEvents = true;
                    PuttyProcess.Exited += ProcessExited;

                    PuttyProcess.Start();
                    PuttyProcess.WaitForInputIdle(Settings.Default.MaxPuttyWaitTime * 1000);

                    int startTicks = Environment.TickCount;
                    while (PuttyHandle.ToInt32() == 0 & Environment.TickCount < startTicks + (Settings.Default.MaxPuttyWaitTime * 1000))
                    {
                        if (_isPuttyNg)
                        {
                            PuttyHandle = Native.FindWindowEx(InterfaceControl.Handle, IntPtr.Zero, Constants.vbNullString, Constants.vbNullString);
                        }
                        else
                        {
                            PuttyProcess.Refresh();
                            PuttyHandle = PuttyProcess.MainWindowHandle;
                        }
                        if (PuttyHandle.ToInt32() == 0)
                        {
                            Thread.Sleep(50);
                        }
                    }

                    if (!_isPuttyNg)
                    {
                        Native.SetParent(PuttyHandle, InterfaceControl.Handle);
                    }

                    Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, Language.strPuttyStuff, true);

                    Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, string.Format(Language.strPuttyHandle, PuttyHandle), true);
                    Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, string.Format(Language.strPuttyTitle, PuttyProcess.MainWindowTitle), true);
                    Runtime.MessageCollector.AddMessage(MessageClass.InformationMsg, string.Format(Language.strPuttyParentHandle, InterfaceControl.Parent.Handle), true);

                    Resize(this, new EventArgs());

                    base.Connect();
                    return(true);
                }
                catch (Exception ex)
                {
                    Runtime.MessageCollector.AddMessage(MessageClass.ErrorMsg, Language.strPuttyConnectionFailed + Constants.vbNewLine + ex.Message);
                    return(false);
                }
            }