Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void Connect()
- {
- ConnectEnabled = true;
- Debug.Console(1, this, "attempting connect");
- // Cancel reconnect if running.
- if (ReconnectTimer != null)
- {
- ReconnectTimer.Stop();
- ReconnectTimer = null;
- }
- // Don't try to connect if already
- if (IsConnected)
- return;
- // Don't go unless everything is here
- if (string.IsNullOrEmpty(Hostname) || Port < 1 || Port > 65535
- || Username == null || Password == null)
- {
- Debug.Console(1, this, "Connect failed. Check hostname, port, username and password are set or not null");
- return;
- }
- // This handles both password and keyboard-interactive (like on OS-X, 'nixes)
- KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(Username);
- kauth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(kauth_AuthenticationPrompt);
- PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(Username, Password);
- // Make a new client if we need it or things have changed
- //if (Client == null || PropertiesHaveChanged())
- //{
- if (Client != null)
- {
- Debug.Console(1, this, "Cleaning up disconnected client");
- Client.ErrorOccurred -= Client_ErrorOccurred;
- KillStream();
- //if (TheStream != null)
- //{
- // TheStream.DataReceived -= Stream_DataReceived;
- // TheStream.ErrorOccurred -= TheStream_ErrorOccurred;
- //}
- //TheStream = null;
- }
- Debug.Console(1, this, "Creating new SshClient");
- ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth);
- Client = new SshClient(connectionInfo);
- Client.ErrorOccurred += Client_ErrorOccurred;
- //}
- //PreviousHostname = Hostname;
- //PreviousPassword = Password;
- //PreviousPort = Port;
- //PreviousUsername = Username;
- //You can do it!
- ClientStatus = SocketStatus.SOCKET_STATUS_WAITING;
- try
- {
- Client.Connect();
- // Have to assume client is connected cause Client.IsConnected is busted in some cases
- // All other conditions *should* error out...
- //if (Client.IsConnected)
- //{
- //Client.KeepAliveInterval = TimeSpan.FromSeconds(2);
- //Client.SendKeepAlive();
- TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534);
- TheStream.DataReceived += Stream_DataReceived;
- //TheStream.ErrorOccurred += TheStream_ErrorOccurred;
- Debug.Console(1, this, "Connected");
- ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED;
- //PreviousHostname = Hostname;
- //PreviousPassword = Password;
- //PreviousPort = Port;
- //PreviousUsername = Username;
- //}
- return; // Success will not pass here
- }
- catch (SshConnectionException e)
- {
- var ie = e.InnerException; // The details are inside!!
- if (ie is SocketException)
- Debug.Console(1, this, "'{0}' CONNECTION failure: Cannot reach host, ({1})", Key, ie.GetType());
- else if (ie is System.Net.Sockets.SocketException)
- Debug.Console(1, this, "'{0}' Connection failure: Cannot reach host '{1}' on port {2}, ({3})",
- Key, Hostname, Port, ie.GetType());
- else if (ie is SshAuthenticationException)
- {
- Debug.Console(1, this, "Authentication failure for username '{0}', ({1})",
- Username, ie.GetType());
- }
- else
- Debug.Console(1, this, "Error on connect:\r({0})", e);
- }
- catch (Exception e)
- {
- Debug.Console(1, this, "Unhandled exception on connect:\r({0})", e);
- }
- // Sucess will not make it this far
- ClientStatus = SocketStatus.SOCKET_STATUS_CONNECT_FAILED;
- HandleConnectionFailure();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement