Exemple #1
0
        /// <exception cref="SocketException">Socket connection to the SSH server or proxy server could not be established, or an error occurred while resolving the hostname.</exception>
        /// <exception cref="SshConnectionException">SSH session could not be established.</exception>
        /// <exception cref="SshAuthenticationException">Authentication of SSH session failed.</exception>
        /// <exception cref="ProxyException">Failed to establish proxy connection.</exception>
        public override bool Connect(Uri destination, NetworkCredentials credentials)
        {
            Destination = destination;

            if (credentials is UsernamePasswordCredentials)
            {
                var upCredentials = credentials as UsernamePasswordCredentials;
                AuthenticationMethod = new PasswordAuthenticationMethod(upCredentials.Username, upCredentials.Password);
            }
            else
            {
                throw new UnhandledCredentialTypeException("Unhandled credential type " + credentials.GetType().ToString());
            }

            int port = Destination.IsDefaultPort ? 22 : Destination.Port;

            ConnectionInfo = new ConnectionInfo(destination.Host, port, credentials.Username, AuthenticationMethod);
            Client         = new SshClient(ConnectionInfo);

            try
            {
                Client.Connect();

                ClientStream = Client.CreateShellStream("xterm", (uint)Columns, (uint)Rows, 800, 600, 16384);
                if (ClientStream == null)
                {
                    throw new VtConnectException("Failed to create client stream");
                }

                ClientStream.DataReceived  += ClientStream_DataReceived;
                ClientStream.ErrorOccurred += ClientStream_ErrorOccurred;
                Client.ErrorOccurred       += ClientErrorOccurred;

                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("IsConnected"));
            }
            catch (Exception e)
            {
                Client.Disconnect();
                Client.Dispose();
                Client = null;

                ConnectionInfo       = null;
                AuthenticationMethod = null;

                System.Diagnostics.Debug.WriteLine(e.Message);
                return(false);
            }

            return(true);
        }