Inheritance: Granados.IO.AbstractGranadosSocket
Ejemplo n.º 1
0
        /**
         * open a new SSH connection via the .NET socket
         */
        public static SSHConnection Connect(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, Socket underlying_socket)
        {
            if (param.UserName == null)
                throw new InvalidOperationException("UserName property is not set");
            if (param.AuthenticationType != AuthenticationType.KeyboardInteractive && param.Password == null)
                throw new InvalidOperationException("Password property is not set");

            PlainSocket s = new PlainSocket(underlying_socket, null);
            VersionExchangeHandler pnh = new VersionExchangeHandler(param, s);
            s.SetHandler(pnh);
            s.RepeatAsyncRead();
            return ConnectMain(param, receiver, pnh, s);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Receive version string.
 /// </summary>
 /// <param name="sock">socket object</param>
 /// <param name="timeout">timeout in msec</param>
 /// <returns>true if version string was received.</returns>
 public bool Receive(PlainSocket sock, long timeout)
 {
     byte[] buf = new byte[1];
     DateTime tm = DateTime.UtcNow.AddMilliseconds(timeout);
     using (MemoryStream mem = new MemoryStream()) {
         while (DateTime.UtcNow < tm && sock.SocketStatus == SocketStatus.Ready) {
             int n = sock.ReadIfAvailable(buf);
             if (n != 1) {
                 Thread.Sleep(10);
                 continue;
             }
             byte b = buf[0];
             mem.WriteByte(b);
             if (b == 0xa) { // LF
                 byte[] bytestr = mem.ToArray();
                 mem.SetLength(0);
                 string line = Encoding.UTF8.GetString(bytestr).TrimEnd('\xd', '\xa');
                 _lines.Add(line);
                 if (line.StartsWith("SSH-")) {
                     _serverVersion = line;
                     return true;
                 }
             }
         }
     }
     return false;
 }
Ejemplo n.º 3
0
        /**
         * open a new SSH connection via the .NET socket
         */
        public static SSHConnection Connect(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, Socket underlying_socket)
        {
            if (param.UserName == null)
                throw new InvalidOperationException("UserName property is not set");
            if (param.AuthenticationType != AuthenticationType.KeyboardInteractive && param.Password == null)
                throw new InvalidOperationException("Password property is not set");

            PlainSocket s = new PlainSocket(underlying_socket, null);
            try {
                SSHProtocolVersionReceiver protoVerReceiver = new SSHProtocolVersionReceiver();
                protoVerReceiver.Receive(s, 5000);
                protoVerReceiver.Verify(param.Protocol);

                SSHConnection con;
                if (param.Protocol == SSHProtocol.SSH1)
                    con = new SSH1Connection(param, s, receiver, protoVerReceiver.ServerVersion, SSHUtil.ClientVersionString(param.Protocol));
                else
                    con = new SSH2Connection(param, s, receiver, protoVerReceiver.ServerVersion, SSHUtil.ClientVersionString(param.Protocol));

                s.SetHandler(con.Packetizer);
                s.RepeatAsyncRead();
                con.SendMyVersion(param);

                if (con.Connect() == AuthenticationResult.Failure) {
                    s.Close();
                    return null;
                }

                return con;
            }
            catch (Exception) {
                s.Close();
                throw;
            }
        }
Ejemplo n.º 4
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;
            }
        }