Exemplo n.º 1
0
        /// <summary>
        /// Connect to SSH-Server and open TCP tunnel.
        /// </summary>
        /// <returns>Returns true on success, else false.</returns>
        private bool BuildTunnel()
        {
            try
            {
                client = new SshClient(config.SupportHost,
                    config.SupportPort,
                    textBoxUsername.Text,
                    textBoxPassword.Text);
                client.ErrorOccurred += new EventHandler<Renci.SshNet.Common.ExceptionEventArgs>(client_ErrorOccurred);
                client.KeepAliveInterval = new System.TimeSpan(0, 0, 10);

                client.Connect();
                client.SendKeepAlive();
                var port = new ForwardedPortRemote(IPAddress.Loopback,
                    config.FwdRemotePort, IPAddress.Loopback, config.FwdLocalPort);
                port.Exception += new EventHandler<Renci.SshNet.Common.ExceptionEventArgs>(port_Exception);
                port.RequestReceived += new EventHandler<Renci.SshNet.Common.PortForwardEventArgs>(port_RequestReceived);
                client.AddForwardedPort(port);
                port.Start();
            }
            catch (System.Exception ex)
            {
                Log.WriteLine("BuildTunnel() general exception: {0}", ex.Message);

                if (client.IsConnected)
                {
                    client.Disconnect();
                }

                MessageBox.Show(GetCaption("serverLoginError"), GetCaption("loginFailed"),
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            return sessionActive = true;
        }