private static SSHConnection ConnectMain(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, ProtocolNegotiationHandler pnh, AbstractSocket s) { pnh.Wait(); if (pnh.State != ReceiverState.Ready) { throw new SSHException(pnh.ErrorMessage); } string sv = pnh.ServerVersion; SSHConnection con = null; if (param.Protocol == SSHProtocol.SSH1) { con = new SSH1Connection(param, receiver, sv, SSHUtil.ClientVersionString(param.Protocol)); } else { con = new SSH2Connection(param, receiver, sv, SSHUtil.ClientVersionString(param.Protocol)); } s.SetHandler(con.PacketBuilder); SendMyVersion(s, param); if (con.Connect(s) != AuthenticationResult.Failure) { return(con); } else { s.Close(); return(null); } }
private static SSHConnection ConnectMain(SSHConnectionParameter param, ISSHConnectionEventReceiver receiver, VersionExchangeHandler pnh, AbstractGranadosSocket s) { DataFragment data = pnh.WaitResponse(); string sv = pnh.ServerVersion; SSHConnection con = null; if (param.Protocol == SSHProtocol.SSH1) { con = new SSH1Connection(param, s, receiver, sv, SSHUtil.ClientVersionString(param.Protocol)); } else { con = new SSH2Connection(param, s, receiver, sv, SSHUtil.ClientVersionString(param.Protocol)); } con.TraceReceptionEvent("server version-string", sv.Trim()); pnh.Close(); s.SetHandler(con.PacketBuilder); con.SendMyVersion(param); if (con.Connect() != AuthenticationResult.Failure) { return(con); } else { s.Close(); return(null); } }
public bool Connect(string host, string username, string password) { if (IsConnected) { return(false); } try { m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); // send TCP keep-alive packets after 10 seconds of inactivity, then every 5 seconds SocketUtil.SetKeepAliveValues(m_socket, true, 10 * 1000, 5 * 1000); m_socket.Connect(host, 22); SSHConnectionParameter f = new SSHConnectionParameter(); f.UserName = username; f.Password = password; f.Protocol = SSHProtocol.SSH2; f.AuthenticationType = AuthenticationType.Password; f.WindowSize = 0x1000; NullReader r = new NullReader(); if (m_timer != null) { m_timer.Stop(); m_timer.Dispose(); } m_conn = (SSH2Connection)SSHConnection.Connect(f, r, m_socket); m_timer = new Timer(); // check connection every 10 milliseconds m_timer.Interval = 10; m_timer.Tick += new EventHandler(OnTimerTick); } catch (Exception e) { if (e.Message == "User authentication failed") { MessageBox.Show("This username and/or password were not accepted. Check them and try again", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { MessageBox.Show("SSH connection exception: " + e.Message); } return(false); } return(true); }
public void OpenSubsystem(string subsystem) { SSH2Connection ssh2 = _connection as SSH2Connection; if (ssh2 == null) { throw new SSHException("OpenSubsystem() can be applied to only SSH2 connection"); } _channel = ssh2.OpenSubsystem(this, subsystem); }
public CommandResult InternalExecute(ICommandTarget target, params IAdaptable[] args) { SSH2Connection ssh2Connection = GetSSHConnection(target) as SSH2Connection; if (ssh2Connection == null) { return(CommandResult.Ignored); } string connectionName = GetTerminalName(target); if (connectionName == null) { connectionName = SFTPPlugin.Instance.StringResource.GetString("Common.UnknownPeer"); } Form ownerForm = GetForm(target); if (!ConfirmToUse(ownerForm, "SFTP")) { return(CommandResult.Cancelled); } SFTPClient sftp = null; try { sftp = SFTPClient.OpenSFTPChannel(ssh2Connection); SFTPForm form = new SFTPForm(ownerForm, sftp, connectionName); form.Show(); // Note: don't specify owner to avoid fixed z-order. return(CommandResult.Succeeded); } catch (Exception e) { if (sftp != null) { try { sftp.Close(); } catch (Exception) { } sftp = null; } RuntimeUtil.ReportException(e); return(CommandResult.Failed); } }
public void Disconnect() { if (m_conn == null) { return; } try { m_conn.Disconnect(""); } // ignore these catch (SocketException) { } catch (ObjectDisposedException) { } m_conn = null; Sftp = null; m_shells.Clear(); }
/// <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; } }
internal CallbackSSH2PacketHandler(SSH2Connection con) { _connection = con; }