public void TryParse(String puttyOptionsString, PuttyOptions expectedPuttyOptions)
        {
            PuttyOptions puttyOptions;

            Assert.IsTrue(PuttyOptions.TryParse(puttyOptionsString, out puttyOptions));
            Assert.AreEqual(expectedPuttyOptions, puttyOptions);
        }
        public String Format(IHostPwEntry hostPwEntry)
        {
            PuttyOptions options = null;
            bool         success = PuttyOptions.TryParse(hostPwEntry.AdditionalOptions, out options);

            StringBuilder sb = new StringBuilder(String.Format("\"{0}\" scp://{1}", ExecutablePath, hostPwEntry.GetUsername()));

            if (!success || (success && !options.HasKeyFile()))
            {
                sb.AppendFormat(":\"{0}\"", hostPwEntry.GetPassword());
            }

            sb.AppendFormat("@{0}", hostPwEntry.IPAddress);

            if (success && options.Port.HasValue)
            {
                sb.AppendFormat(":{0}", options.Port);
            }

            // Starting with version 5.6 a passphrase for the private key file can be provided.
            // See: https://winscp.net/eng/docs/faq_passphrase
            if (success && options.HasKeyFile())
            {
                sb.AppendFormat(" -privatekey=\"{0}\" -passphrase=\"{1}\"", options.KeyFilePath, hostPwEntry.GetPassword());
            }

            return(sb.ToString());
        }
        public void TryParse(String optionsString, String expectedSessionName, int?expectedPort)
        {
            PuttyOptions options;

            Assert.IsTrue(PuttyOptions.TryParse(optionsString, out options));
            Assert.AreEqual(expectedSessionName, options.SessionName);
            Assert.AreEqual(expectedPort, options.Port);
        }
Esempio n. 4
0
        public override void Conn()
        {
            Debug.Assert(ParentWindow != null);
            Debug.Assert(_protocolPuttyBase.ProtocolServerBase.Id > 0);


            // set putty bg color
            var options = SystemConfig.Instance.Theme.SelectedPuttyTheme;

            GridBg.Background = new SolidColorBrush(new Color()
            {
                A = 255,
                R = 0,
                G = 0,
                B = 0,
            });
            var bgOption = options?.First(x => x.Key == PuttyRegOptionKey.Colour2.ToString());

            if (bgOption != null &&
                bgOption.Value.ToString().Split(',').Length == 3)
            {
                var color = bgOption.Value.ToString().Split(',');
                if (byte.TryParse(color[0], out var r) &&
                    byte.TryParse(color[1], out var g) &&
                    byte.TryParse(color[2], out var b))
                {
                    GridBg.Background = new SolidColorBrush(new Color()
                    {
                        A = 255,
                        R = r,
                        G = g,
                        B = b,
                    });
                }
            }


            _puttyOption = new PuttyOptions(_protocolPuttyBase.GetSessionName());
            RegPuttySessionInRegTable();

            _puttyHandle = IntPtr.Zero;
            //FormBorderStyle = FormBorderStyle.None;
            //WindowState = FormWindowState.Maximized;
            var tsk = new Task(InitPutty);

            tsk.Start();


            _puttyMasterPanel = new System.Windows.Forms.Panel
            {
                BackColor   = System.Drawing.Color.Transparent,
                Dock        = System.Windows.Forms.DockStyle.Fill,
                BorderStyle = BorderStyle.None
            };
            _puttyMasterPanel.SizeChanged += PuttyMasterPanelOnSizeChanged;
            FormsHost.Child = _puttyMasterPanel;
        }
Esempio n. 5
0
        public void ChangePassword(IHostPwEntry hostPwEntry, string newPassword)
        {
            PuttyOptions options = null;
            bool         success = PuttyOptionsParser.TryParse(hostPwEntry.AdditionalOptions, out options);

            var passwordChanger = success && options.Port.HasValue ? this.passwordChangerFactory.Create(options.Port.Value) :
                                  this.passwordChangerFactory.Create();

            passwordChanger.ChangePassword(hostPwEntry.IPAddress, hostPwEntry.GetUsername(), hostPwEntry.GetPassword(), newPassword);
        }
        public void TryParseReturnsFalse(String optionsString)
        {
            PuttyOptions options;

            Assert.IsFalse(PuttyOptions.TryParse(optionsString, out options));
        }
Esempio n. 7
0
        public String Format(IHostPwEntry hostPwEntry)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("\"{0}\"", this.ExecutablePath);

            string ipAddress = null;
            string port      = null;

            if (hostPwEntry.IPAddress.Contains(":"))
            {
                ipAddress = hostPwEntry.IPAddress.Substring(0, hostPwEntry.IPAddress.IndexOf(':'));
                port      = hostPwEntry.IPAddress.Substring(hostPwEntry.IPAddress.IndexOf(':') + 1);
            }
            else
            {
                ipAddress = hostPwEntry.IPAddress;
            }

            PuttyOptions options = null;
            bool         success = PuttyOptions.TryParse(hostPwEntry.AdditionalOptions, out options);

            if (success && options.HasKeyFile())
            {
                sb.AppendFormat(" -i \"{0}\"", options.KeyFilePath);
            }

            if (success && !String.IsNullOrEmpty(options.SessionName))
            {
                ICollection <String> sessionNames = this.PuttySessionFinder.Find(options.SessionName);

                if (sessionNames.Count > 0)
                {
                    sb.AppendFormat(" -load \"{0}\"", new List <String>(sessionNames)[0]);
                }
            }

            if (!success || (success && !options.CommandContainsPort))
            {
                if (port != null)
                {
                    sb.AppendFormat(" -P {0}", port);
                }

                if (port == null && success && options.Port.HasValue)
                {
                    sb.AppendFormat(" -P {0}", options.Port);
                }
            }

            if (hostPwEntry.ConnectionMethods.Contains(ConnectionMethodType.PuttySSH))
            {
                sb.Append(" -ssh");
            }
            else if (hostPwEntry.ConnectionMethods.Contains(ConnectionMethodType.PuttyTelnet))
            {
                sb.Append(" -telnet");
            }

            sb.AppendFormat(" {0}@{1}", hostPwEntry.GetUsername(), ipAddress);

            // Specifying the password via -pw switch only works with SSH protocol.
            // See: http://the.earth.li/~sgtatham/putty/0.65/htmldoc/Chapter3.html.
            if (this.AppendPassword && hostPwEntry.ConnectionMethods.Contains(ConnectionMethodType.PuttySSH))
            {
                // Allow passwords with white-spaces.
                if (!success || (success && !options.CommandContains("-pw ")))
                {
                    var password = hostPwEntry.GetPassword();

                    if (password.Contains(@""""))
                    {
                        sb.AppendFormat(" -pw \"{0}\"", password.Replace(@"""", @"\"""));
                    }
                    else
                    {
                        sb.AppendFormat(" -pw \"{0}\"", password);
                    }
                }
            }

            if (success && options.HasCommand())
            {
                sb.AppendFormat(" {0}", options.Command);
            }

            return(sb.ToString());
        }
Esempio n. 8
0
 private void DelPuttySessionInRegTable()
 {
     _puttyOption?.Del();
     _puttyOption = null;
 }