コード例 #1
2
ファイル: Connection.cs プロジェクト: npcook/terminal
        public void Connect(ConnectionSettings settings, int terminalCols, int terminalRows)
        {
            Settings = settings;
            if (dataThread != null)
                throw new InvalidOperationException("Already connecting to a server.");
            dataThread = new Thread(() =>
            {
            #if USE_LIBSSHNET
                var connection = new LibSshNetConnection(serverAddress, username, (authentications.First() as PasswordAuthentication).Password);
                stream = connection.GetStream();
            #else
                try
                {
                    var authentications = new List<AuthenticationMethod>();
                    if (!string.IsNullOrEmpty(settings.KeyFilePath))
                    {
                        var privateKeyFile = new PrivateKeyFile(settings.KeyFilePath, settings.KeyFilePassphrase);
                        authentications.Add(new PrivateKeyAuthenticationMethod(settings.Username, privateKeyFile));
                    }
                    authentications.Add(new PasswordAuthenticationMethod(settings.Username, settings.Password));
                    ConnectionInfo connectionInfo = new ConnectionInfo(settings.ServerAddress, settings.ServerPort, settings.Username, authentications.ToArray());

                    Client = new SshClient(connectionInfo);
                    Client.Connect();

                    Client.KeepAliveInterval = TimeSpan.FromSeconds(20);

                    Stream = Client.CreateShellStream("xterm-256color", (uint) terminalCols, (uint) terminalRows, 0, 0, 1000);

                    if (Connected != null)
                        Connected(this, EventArgs.Empty);
                }
                catch (Exception ex)
                {
                    ConnectionFailedEventArgs args = null;
                    if (ex is Renci.SshNet.Common.SshPassPhraseNullOrEmptyException ||
                        ex is InvalidOperationException)
                        args = new ConnectionFailedEventArgs(ConnectionError.PassphraseIncorrect, ex.Message);
                    else if (ex is SocketException)
                        args = new ConnectionFailedEventArgs(ConnectionError.NetError, ex.Message);
                    else if (ex is Renci.SshNet.Common.SshAuthenticationException)
                        args = new ConnectionFailedEventArgs(ConnectionError.AuthenticationError, ex.Message);
                    else
                        throw;

                    if (Failed != null)
                        Failed(this, args);
                }
            #endif
            });
            dataThread.Name = "Data Thread";
            dataThread.IsBackground = true;
            dataThread.Start();
        }
コード例 #2
0
        public void Connect(ConnectionSettings settings, int terminalCols, int terminalRows)
        {
            Settings = settings;
            if (dataThread != null)
            {
                throw new InvalidOperationException("Already connecting to a server.");
            }
            dataThread = new Thread(() =>
            {
#if USE_LIBSSHNET
                var connection = new LibSshNetConnection(serverAddress, username, (authentications.First() as PasswordAuthentication).Password);
                stream         = connection.GetStream();
#else
                try
                {
                    var authentications = new List <AuthenticationMethod>();
                    if (!string.IsNullOrEmpty(settings.KeyFilePath))
                    {
                        var privateKeyFile = new PrivateKeyFile(settings.KeyFilePath, settings.KeyFilePassphrase);
                        authentications.Add(new PrivateKeyAuthenticationMethod(settings.Username, privateKeyFile));
                    }
                    authentications.Add(new PasswordAuthenticationMethod(settings.Username, settings.Password));
                    ConnectionInfo connectionInfo = new ConnectionInfo(settings.ServerAddress, settings.ServerPort, settings.Username, authentications.ToArray());

                    Client = new SshClient(connectionInfo);
                    Client.Connect();

                    Client.KeepAliveInterval = TimeSpan.FromSeconds(20);

                    Stream = Client.CreateShellStream("xterm-256color", (uint)terminalCols, (uint)terminalRows, 0, 0, 1000);

                    if (Connected != null)
                    {
                        Connected(this, EventArgs.Empty);
                    }
                }
                catch (Exception ex)
                {
                    ConnectionFailedEventArgs args = null;
                    if (ex is Renci.SshNet.Common.SshPassPhraseNullOrEmptyException ||
                        ex is InvalidOperationException)
                    {
                        args = new ConnectionFailedEventArgs(ConnectionError.PassphraseIncorrect, ex.Message);
                    }
                    else if (ex is SocketException)
                    {
                        args = new ConnectionFailedEventArgs(ConnectionError.NetError, ex.Message);
                    }
                    else if (ex is Renci.SshNet.Common.SshAuthenticationException)
                    {
                        args = new ConnectionFailedEventArgs(ConnectionError.AuthenticationError, ex.Message);
                    }
                    else
                    {
                        throw;
                    }

                    if (Failed != null)
                    {
                        Failed(this, args);
                    }
                }
#endif
            });
            dataThread.Name         = "Data Thread";
            dataThread.IsBackground = true;
            dataThread.Start();
        }