public void StartPsftp(ConnectionInfo connectionInfo)
 {
     var fileName = Path.Combine(PathHelper.StartupPath, PsftpLocation);
     var args = ArgumentsBuilder.BuildPsftpArguments(
         connectionInfo,
         PrivateKeyStorage.Create(connectionInfo.PrivateKeyData).Filename);
     Process.Start(fileName, args);
 }
        public PLinkConnection(ConnectionInfo info)
        {
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }

            this.Info = info;
        }
 public static string BuildFileZillaArguments(ConnectionInfo connection)
 {
     var arguments = string.Format(
         @"sftp://{0}:{1}@{2}:{3}",
         connection.UserName,
         connection.Password,
         connection.HostName,
         connection.Port);
     return arguments;
 }
        /// <summary>
        ///     Build something like this:
        ///     "-ssh -load _stm_preset_ username@domainName -P 22 -pw password -D 5000 -L 44333:username.dyndns.org:44333"
        /// </summary>
        public static string BuildPuttyArguments(
            ConnectionInfo connection,
            bool forceShellStart,
            string privateKeyFileName)
        {
            const string sshFlag = "-ssh";
            const string verboseFlag = "-v";
            var target = string.Format("{0}@{1}", connection.UserName, connection.HostName);
            var port = string.Format("-P {0}", connection.Port);

            var profile = connection.SharedSettings != null
                ? string.Format("-load {0}", SharedSettingsManager.GetProfileName(connection.SharedSettings.Name))
                : "";

            var dontStartShellFlag = string.IsNullOrEmpty(connection.RemoteCommand) && !forceShellStart
                ? "-N"
                : "";

            var credentials = "";
            switch (connection.AuthType)
            {
            case AuthenticationType.None:
                break;
            case AuthenticationType.Password:
                credentials = string.Format("-pw {0}", connection.Password);
                break;
            case AuthenticationType.PrivateKey:
                credentials = string.Format("-i \"{0}\"", privateKeyFileName);
                break;
            default:
                throw new ArgumentOutOfRangeException();
            }

            var connectionArguments = string.Join(
                " ",
                new[]
                    {
                        port,
                        credentials,
                        profile,
                        verboseFlag,
                        dontStartShellFlag,
                        sshFlag,
                        target
                    }.Where(x => !string.IsNullOrEmpty(x)));

            var sb = new StringBuilder(connectionArguments);
            foreach (var tunnelArguments in connection.Tunnels.Select(BuildPuttyTunnelArguments))
            {
                sb.Append(tunnelArguments);
            }

            return sb.ToString();
        }
        public static string BuildPsftpArguments(ConnectionInfo connection, string privateKeyFileName)
        {
            var arguments = string.Format("{0}@{1} -P {2} -batch", connection.UserName, connection.Password, connection.Port);
            switch (connection.AuthType)
            {
            case AuthenticationType.Password:
                arguments += string.Format("-pw {0}", connection.Password);
                break;
            case AuthenticationType.PrivateKey:
                arguments += string.Format("-i {0}", privateKeyFileName);
                break;
            default:
                throw new ArgumentOutOfRangeException();
            }

            return arguments;
        }
        public void It_should_open_parent_connection_first()
        {
            this.BuildConnectionMock();
            this.BuildConnectionMock();

            var parentConnectionInfo = new ConnectionInfo
                {
                    Name = "Parent Connection"
                };
            var childConnectionInfo = new ConnectionInfo
                {
                    Name = "Child Connection",
                    Parent = parentConnectionInfo
                };
            var cm = this.kernel.Get<ConnectionManager>();
            cm.Open(childConnectionInfo);

            var parentConnectionMock = this.connectionMocks[0];
            var childConnectionMock = this.connectionMocks[1];
            parentConnectionMock.Verify(c => c.Open(), Times.Once());
            childConnectionMock.Verify(c => c.Open(), Times.Once());
            cm.ActiveConnections.Should().Have.SameSequenceAs(parentConnectionInfo, childConnectionInfo);
        }
 private static ConnectionInfo CreateConnectionInfo(int index, int tunnelsCount)
 {
     index++;
     var connection = new ConnectionInfo
         {
             UserName = "******" + index,
             Password = "******" + index,
             HostName = "localhost" + index,
             Port = 22 + index,
             Name = "connection1" + index
         };
     connection.Tunnels.AddRange(CreateTunnels(tunnelsCount));
     return connection;
 }
        public void Close(ConnectionInfo connectionInfo)
        {
            if (connectionInfo == null)
            {
                throw new ArgumentNullException("connectionInfo");
            }

            lock (this.syncObject)
            {
                var connection = this.FindConnection(connectionInfo);
                if (connection == null)
                {
                    return;
                }

                this.activeConnections.Remove(connection);
                this.pendingConnections.Remove(connection);
                connection.Connection.Close();
            }
        }
 private ConnectionInternal FindConnection(ConnectionInfo connectionInfo)
 {
     return this.activeConnections.FirstOrDefault(c => c.Connection.Info.Equals(connectionInfo)) ??
            this.pendingConnections.FirstOrDefault(c => c.Connection.Info.Equals(connectionInfo));
 }
 private void BroadcastMessage(ConnectionInfo connectionInfo, MessageSeverity severity, string message)
 {
     this.Fire(o => o.HandleMessage(connectionInfo, severity, message));
 }
        public void Open(ConnectionInfo connectionInfo)
        {
            if (this.SyncContext == null)
            {
                this.SyncContext = SynchronizationContext.Current;
            }

            if (connectionInfo.Parent != null)
            {
                this.Open(connectionInfo.Parent);
            }

            lock (this.syncObject)
            {
                if (connectionInfo.SharedSettings != null)
                {
                    this.sharedSettingsManager.Save(connectionInfo.SharedSettings);
                }

                var connection = this.connectionFactory.CreateConnection();
                connection.Info = connectionInfo;
                connection.Observer = this;
                this.pendingConnections.Add(new ConnectionInternal(connection));
                connection.Open();
            }
        }
 public ConnectionState GetState(ConnectionInfo info)
 {
     lock (this.syncObject)
     {
         var connection = this.FindConnection(info);
         return connection == null
             ? ConnectionState.Closed
             : connection.State;
     }
 }
        public bool IsChildOf(ConnectionInfo connection)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            for (var parent = this.Parent; parent != null; parent = parent.Parent)
            {
                if (Equals(parent, connection))
                {
                    return true;
                }
            }

            return false;
        }
 public void StartFileZilla(ConnectionInfo connectionInfo)
 {
     var fileName = Path.Combine(PathHelper.StartupPath, FileZillaLocation);
     var args = ArgumentsBuilder.BuildFileZillaArguments(connectionInfo);
     Process.Start(fileName, args);
 }
 private static ConnectionInfo CreateValidConnectionInfo()
 {
     var connection = new ConnectionInfo
         {
             Name = "SDF.org",
             HostName = "sdf.org",
             Port = 22,
             UserName = "******",
             Password = "******"
         };
     return connection;
 }
        public void Render([NotNull] IEnumerable<ConnectionInfo> connectionsList,
            [NotNull] IEnumerable<SharedConnectionSettings> proxyList, ConnectionInfo connection)
        {
            if (connectionsList == null)
            {
                throw new ArgumentNullException("connectionsList");
            }
            if (proxyList == null)
            {
                throw new ArgumentNullException("proxyList");
            }

            this.SuspendLayout();

            var isNew = connection == null;
            if (isNew)
            {
                this.Controller.Connection = connection = new ConnectionInfo();
            }

            this.connectionNameTextBox.Text = connection.Name;
            this.hostNameTextBox.Text = connection.HostName;
            this.portTextBox.Text = connection.Port.ToString(CultureInfo.InvariantCulture);
            this.userNameTextBox.Text = connection.UserName;
            switch (connection.AuthType)
            {
            case AuthenticationType.Password:
                this.usePasswordRadioButton.Checked = true;
                this.passwordTextBox.Text = connection.Password;
                this.passphraseTextBox.Text = "";
                this.privateKeyFileNameLabel.Text = "<Not Loaded>";
                break;
            case AuthenticationType.PrivateKey:
                this.usePrivateKeyRadioButton.Checked = true;
                this.passphraseTextBox.Text = connection.Password;
                this.passwordTextBox.Text = "";
                this.privateKeyFileNameLabel.Text = "<Previously Loaded>";
                break;
            default:
                throw new ArgumentOutOfRangeException();
            }

            this.remoteCommandTextBox.Text = connection.RemoteCommand;

            this.parentConnectionComboBox.Items.Clear();
            // ReSharper disable once PossibleUnintendedReferenceComparison
            var allButThis = connectionsList.Where(c => c != connection).ToArray();
            var possibleParents = allButThis.Where(c => !c.IsChildOf(connection)).OrderBy(c => c.Name);
            this.parentConnectionComboBox.Items.AddRange(new[] { "None" }.Concat<object>(possibleParents).ToArray());
            if (connection.Parent == null)
            {
                this.parentConnectionComboBox.SelectedIndex = 0;
            }
            else
            {
                this.parentConnectionComboBox.SelectedItem = connection.Parent;
            }

            this.proxyComboBox.Items.Clear();
            var pl = proxyList.ToArray();
            var defaultProxy = new[] { pl.First(p => p.Name == "default") };
            this.proxyComboBox.Items.AddRange(defaultProxy.Concat<object>(pl.Except(defaultProxy).OrderBy(p => p.Name)).ToArray());
            if (connection.SharedSettings == null)
            {
                this.proxyComboBox.SelectedIndex = 0;
            }
            else
            {
                this.proxyComboBox.SelectedItem = connection.SharedSettings;
            }

            this.tunnelsGridView.Rows.Clear();
            foreach (var tunnel in connection.Tunnels)
            {
                this.AddTunnelToList(tunnel);
            }

            this.removeTunnelButton.Enabled = this.tunnelsGridView.SelectedRows.Count > 0;

            this.connectionValidator.SetValidationRule(this.connectionNameTextBox, new ConnectionNameValidationRule(allButThis));
            this.connectionValidator.ResetErrors();
            this.ResetAddTunnelGroup();

            this.AcceptButton = isNew
                ? this.createButton
                : this.okButton;
            this.okButton.Visible = !isNew;
            this.applyButton.Visible = !isNew;
            this.createButton.Visible = isNew;
            this.Modified = false;

            this.ResumeLayout(true);
        }
        public void Collect(ConnectionInfo connection)
        {
            connection.Name = this.connectionNameTextBox.Text.Trim();
            connection.HostName = this.hostNameTextBox.Text.Trim();
            connection.Port = int.Parse(this.portTextBox.Text.Trim());
            connection.UserName = this.userNameTextBox.Text.Trim();
            connection.RemoteCommand = this.remoteCommandTextBox.Text.Trim();

            if (this.usePasswordRadioButton.Checked)
            {
                connection.AuthType = AuthenticationType.Password;
                connection.Password = this.passwordTextBox.Text.Trim();
            }
            else
            {
                connection.AuthType = AuthenticationType.PrivateKey;
                var passphrase = this.passphraseTextBox.Text.Trim();
                connection.Password = !string.IsNullOrEmpty(passphrase)
                    ? passphrase
                    : null;
            }

            connection.Parent = this.parentConnectionComboBox.SelectedItem as ConnectionInfo;
            connection.SharedSettings = this.proxyComboBox.SelectedItem as SharedConnectionSettings;
            connection.Tunnels.Clear();
            connection.Tunnels.AddRange(this.CollectTunnelInfoList());
        }