Exemplo n.º 1
0
        /// <summary>
        /// Establish a SSH connection
        /// </summary>
        /// <param name="socket">TCP socket which is already connected to the server.</param>
        /// <param name="param">SSH connection parameter</param>
        /// <param name="connectionEventHandlerCreator">a factory function to create a connection event handler (can be null)</param>
        /// <param name="protocolEventLoggerCreator">a factory function to create a protocol log event handler (can be null)</param>
        /// <returns>new connection object</returns>
        public static ISSHConnection Connect(
            Socket socket,
            SSHConnectionParameter param,
            Func <ISSHConnection, ISSHConnectionEventHandler> connectionEventHandlerCreator = null,
            Func <ISSHConnection, ISSHProtocolEventLogger> protocolEventLoggerCreator       = null)
        {
            if (socket == null)
            {
                throw new ArgumentNullException("socket");
            }
            if (param == null)
            {
                throw new ArgumentNullException("param");
            }
            if (!socket.Connected)
            {
                throw new ArgumentException("socket is not connected to the remote host", "socket");
            }
            if (param.UserName == null)
            {
                throw new ArgumentException("UserName property is not set", "param");
            }
            if (param.AuthenticationType != AuthenticationType.KeyboardInteractive && param.Password == null)
            {
                throw new ArgumentException("Password property is not set", "param");
            }

            string clientVersion = SSHUtil.ClientVersionString(param.Protocol);

            PlainSocket psocket = new PlainSocket(socket, null);

            try {
                // receive protocol version string
                SSHProtocolVersionReceiver protoVerReceiver = new SSHProtocolVersionReceiver();
                protoVerReceiver.Receive(psocket, 5000);
                // verify the version string
                protoVerReceiver.Verify(param.Protocol);

                ISSHConnection sshConnection;

                if (param.Protocol == SSHProtocol.SSH1)
                {
                    // create a connection object
                    var con = new SSH1Connection(
                        psocket,
                        param,
                        protoVerReceiver.ServerVersion,
                        clientVersion,
                        connectionEventHandlerCreator,
                        protocolEventLoggerCreator);
                    // start receiving loop
                    psocket.RepeatAsyncRead();
                    // send client version
                    con.SendMyVersion();
                    // establish a SSH connection
                    con.Connect();
                    sshConnection = con;
                }
                else
                {
                    // create a connection object
                    var con = new SSH2Connection(
                        psocket,
                        param,
                        protoVerReceiver.ServerVersion,
                        clientVersion,
                        connectionEventHandlerCreator,
                        protocolEventLoggerCreator);
                    // start receiving loop
                    psocket.RepeatAsyncRead();
                    // send client version
                    con.SendMyVersion();
                    // establish a SSH connection
                    con.Connect();
                    sshConnection = con;
                }

                return(sshConnection);
            }
            catch (Exception) {
                psocket.Close();
                throw;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Establish a SSH connection
        /// </summary>
        /// <param name="socket">TCP socket which is already connected to the server.</param>
        /// <param name="param">SSH connection parameter</param>
        /// <param name="connectionEventHandlerCreator">a factory function to create a connection event handler (can be null)</param>
        /// <param name="protocolEventLoggerCreator">a factory function to create a protocol log event handler (can be null)</param>
        /// <returns>new connection object</returns>
        public static ISSHConnection Connect(
                    Socket socket,
                    SSHConnectionParameter param,
                    Func<ISSHConnection, ISSHConnectionEventHandler> connectionEventHandlerCreator = null,
                    Func<ISSHConnection, ISSHProtocolEventLogger> protocolEventLoggerCreator = null)
        {
            if (socket == null) {
                throw new ArgumentNullException("socket");
            }
            if (param == null) {
                throw new ArgumentNullException("param");
            }
            if (!socket.Connected) {
                throw new ArgumentException("socket is not connected to the remote host", "socket");
            }
            if (param.UserName == null) {
                throw new ArgumentException("UserName property is not set", "param");
            }
            if (param.AuthenticationType != AuthenticationType.KeyboardInteractive && param.Password == null) {
                throw new ArgumentException("Password property is not set", "param");
            }

            string clientVersion = SSHUtil.ClientVersionString(param.Protocol);

            PlainSocket psocket = new PlainSocket(socket, null);
            try {
                // receive protocol version string
                SSHProtocolVersionReceiver protoVerReceiver = new SSHProtocolVersionReceiver();
                protoVerReceiver.Receive(psocket, 5000);
                // verify the version string
                protoVerReceiver.Verify(param.Protocol);

                ISSHConnection sshConnection;

                if (param.Protocol == SSHProtocol.SSH1) {
                    // create a connection object
                    var con = new SSH1Connection(
                                psocket,
                                param,
                                protoVerReceiver.ServerVersion,
                                clientVersion,
                                connectionEventHandlerCreator,
                                protocolEventLoggerCreator);
                    // start receiving loop
                    psocket.RepeatAsyncRead();
                    // send client version
                    con.SendMyVersion();
                    // establish a SSH connection
                    con.Connect();
                    sshConnection = con;
                }
                else {
                    // create a connection object
                    var con = new SSH2Connection(
                                psocket,
                                param,
                                protoVerReceiver.ServerVersion,
                                clientVersion,
                                connectionEventHandlerCreator,
                                protocolEventLoggerCreator);
                    // start receiving loop
                    psocket.RepeatAsyncRead();
                    // send client version
                    con.SendMyVersion();
                    // establish a SSH connection
                    con.Connect();
                    sshConnection = con;
                }

                return sshConnection;
            }
            catch (Exception) {
                psocket.Close();
                throw;
            }
        }