Example #1
0
        /// <summary>
        /// Connect to an SSH server. This method is not designed to be called directly but from
        /// an <see cref="Maverick.SSH.SSHConnector"/> instance.
        /// </summary>
        /// <param name="io">The underlying transport mechanism</param>
        /// <param name="context">The configuration context for this connection</param>
        /// <param name="connector">The connector creating this client</param>
        /// <param name="username">The username for the connection</param>
        /// <param name="localIdentification">The identification string sent to the server</param>
        /// <param name="remoteIdentification">The identification string received from the server</param>
        /// <param name="threaded">Should this connection use an additional thread to buffer messages</param>
        public void Connect(SSHTransport io,
                            SSHContext context,
                            SSHConnector connector,
                            System.String username,
                            System.String localIdentification,
                            System.String remoteIdentification,
                            bool threaded)
        {
            FireEvent(SSHState.CONNECTING);

            this.context              = (SSH2Context)context;
            this.con                  = connector;
            this.io                   = io;
            this.threaded             = threaded;
            this.username             = username;
            this.localIdentification  = localIdentification;
            this.remoteIdentification = remoteIdentification;
            this.transport            = new TransportProtocol(io,
                                                              this);

            this.transport.StateChange += new TransportProtocolStateChangeEvent(this.OnTransportEvent);

            this.transport.StartProtocol();

            FireEvent(SSHState.CONNECTED);

            this.connection = new ConnectionProtocol(transport, threaded);
            this.connection.AddChannelFactory(requestFactory);
            this.authentication = new AuthenticationProtocol(transport, connection);

            RequestAuthenticationMethods();
        }
Example #2
0
        private void Button1_Click(object sender, EventArgs e)
        {
            server   = server_text.Text;
            port     = port_text.Text;
            username = user_text.Text;
            password = password_text.Text;

            SSHConnector conn = new SSHConnector(server, username, password, port);

            conn.Open();
        }
Example #3
0
        public static SocketWithTimeout StartNewConnection(ISocketWithTimeoutClient client, ChannelProfile prof, string password, HostKeyCheckCallback keycheck)
        {
            SocketWithTimeout swt;
            swt = new SSHConnector(prof, password, keycheck);

            if(Env.Options.UseSocks)
                swt.AsyncConnect(client, CreateSocksParam(prof.SSHHost, prof.SSHPort));
            else
                swt.AsyncConnect(client, prof.SSHHost, prof.SSHPort);
            return swt;
        }
Example #4
0
        //获取一个可用的SFTPClient
        private SFTPClient GetAvailableSFTP()
        {
            //关闭之前的
            Close();

            SSHConnector con = SSHConnector.Create();

            ssh = con.Connect(new TcpClientTransport(ipAddress, 22), loginUser, true);

            PasswordAuthentication pwd = new PasswordAuthentication();

            pwd.Password = loginPass;

            // Authenticate the user
            if (ssh.Authenticate(pwd) == AuthenticationResult.COMPLETE)
            {
                // Open up a session and do something..
                sftp = new SFTPClient(ssh);
            }
            return(sftp);
        }
Example #5
0
        public void Connect()
        {
            // 先判断连接状态
            if (IsConnected || _isConnecting)
            {
                return;
            }

            _isConnecting = true;

            if (statusText == null)
            {
                statusText           = new Label();
                statusText.Dock      = DockStyle.Fill;
                statusText.BackColor = Color.White;
                statusText.ForeColor = Color.Black;
                statusText.Font      = new Font("Microsoft YaHei", 10);
                statusText.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            }

            statusText.Text    = "正在连接";
            statusText.Visible = true;
            this.Invalidate();

            this.Controls.Add(statusText);

            if (GApp._frame == null)
            {
                GApp.Run(new string[0]);
                GApp.Options.BGColor              = this.BackColor;
                GApp.Options.TextColor            = this.ForeColor;
                GApp.Options.RightButtonAction    = RightButtonAction.Paste;
                GApp.Options.AutoCopyByLeftButton = true;
                //GApp.Options.WarningOption = WarningOption.Ignore;
                GApp.Options.Font = this.Font;
                GApp._frame._multiPaneControl.InitUI(null, GApp.Options);
                GEnv.InterThreadUIService.MainFrameHandle = GApp._frame.Handle;
            }

            try
            {
                //------------------------------------------------------------------------
                SSHTerminalParam sshp = new SSHTerminalParam(Poderosa.ConnectionParam.ConnectionMethod.SSH2, this.Host, this.UserName, this.Password);
                sshp.AuthType      = AuthType.Password;
                sshp.IdentityFile  = string.Empty;
                sshp.Encoding      = EncodingType.UTF8;
                sshp.Port          = this._port;
                sshp.RenderProfile = new RenderProfile();
                sshp.TerminalType  = TerminalType.XTerm;

                CommunicationUtil.SilentClient s = new CommunicationUtil.SilentClient();
                Size sz = this.Size;
                SocketWithTimeout swt = new SSHConnector(sshp, sz, sshp.Passphrase, null);
                swt.AsyncConnect(s, sshp.Host, sshp.Port, (ct, errorMessage) =>
                {
                    if (ct == null)
                    {
                        statusText.Invoke(new Action(() =>
                        {
                            statusText.Text = "连接异常:" + errorMessage;
                        }));

                        return;
                    }

                    if (this._terminalPane == null)
                    {
                        this.Invoke(new Action(() =>
                        {
                            this._terminalPane = new TerminalPane();
                            InitTerminalPane(ct);
                        }));
                    }
                    else
                    {
                        this._terminalPane.Invoke(new Action(() =>
                        {
                            this._terminalPane.Detach();
                            InitTerminalPane(ct);
                        }));
                    }
                });
            }
            catch (Exception ex)
            {
                statusText.Text = "连接异常:" + ex.Message;
                return;
            }

            _isConnecting = false;
        }
Example #6
0
        public void Connect()
        {
            #region old stuff

            /*
             * Poderosa.ConnectionParam.LogType logType = Poderosa.ConnectionParam.LogType.Default;
             * string file = null;
             * if (this.TerminalPane.Connection != null)
             * {
             *  logType = this.TerminalPane.Connection.LogType;
             *  file = this.TerminalPane.Connection.LogPath;
             *  //GApp.GetConnectionCommandTarget().Close();
             *  this.TerminalPane.Connection.Close();
             *  this.TerminalPane.Detach();
             * }
             *
             *
             * SSHTerminalParam p = new SSHTerminalParam((Poderosa.ConnectionParam.ConnectionMethod)this.Method, this.Host, this.UserName, this.Password);
             *
             * GApp.GlobalCommandTarget.SilentNewConnection(p);
             *
             *
             * if (file != null)
             *  this.SetLog((LogType) logType, file, true);
             */
            #endregion

            // Save old log info in case this is a reconnect
            Poderosa.ConnectionParam.LogType logType = Poderosa.ConnectionParam.LogType.Default;
            string file = null;
            if (this.TerminalPane.Connection != null)
            {
                logType = this.TerminalPane.Connection.LogType;
                file    = this.TerminalPane.Connection.LogPath;
                //GApp.GetConnectionCommandTarget().Close();
                this.TerminalPane.Connection.Close();
                this.TerminalPane.Detach();
            }

            try
            {
                //------------------------------------------------------------------------
                SSHTerminalParam sshp = new SSHTerminalParam((Poderosa.ConnectionParam.ConnectionMethod) this.Method, this.Host, this.UserName, this.Password);
                sshp.AuthType      = this.AuthType;
                sshp.IdentityFile  = this.IdentifyFile;
                sshp.Encoding      = EncodingType.ISO8859_1;
                sshp.Port          = 22;
                sshp.RenderProfile = new RenderProfile();
                sshp.TerminalType  = TerminalType.XTerm;

                CommunicationUtil.SilentClient s = new CommunicationUtil.SilentClient();
                Size sz = this.Size;

                SocketWithTimeout swt;
                swt = new SSHConnector((Poderosa.ConnectionParam.SSHTerminalParam)sshp, sz, sshp.Passphrase, (HostKeyCheckCallback)null);
                swt.AsyncConnect(s, sshp.Host, sshp.Port);
                ConnectionTag ct = s.Wait(swt);

                this.TerminalPane.FakeVisible = true;

                this.TerminalPane.Attach(ct);
                ct.Receiver.Listen();
                //-------------------------------------------------------------
                if (file != null)
                {
                    this.SetLog((LogType)logType, file, true);
                }
                this.TerminalPane.ConnectionTag.RenderProfile = new RenderProfile();
                this.SetPaneColors(Color.LightBlue, Color.Black);
            }
            catch
            {
                //MessageBox.Show(e.Message, "Connection Error");
                return;
            }
        }