Provides functionality for remote port forwarding
Inheritance: ForwardedPort, IDisposable
        /// <summary>
        /// Starts the ssh connection, and bridge the streams.
        /// </summary>
        private void StartTunnel()
        {
            try
            {
                _client = new SshClient(_config.host, _config.user, new PrivateKeyFile(new MemoryStream(Encoding.Default.GetBytes(PrivateKey))));
                _client.Connect();
                _client.KeepAliveInterval = new TimeSpan(0, 0, 5);

                if (!_client.IsConnected)
                {
                    throw new ServiceException("Can't start tunnel, try again.");
                }

                string connectHost = string.IsNullOrEmpty(this.LocalHost) ? "127.0.0.1" : this.LocalHost;

                _connectionPort = _client.AddForwardedPort<ForwardedPortRemote>((uint)_config.through_port,  connectHost, (uint)LocalPort);
                _connectionPort.Exception += new EventHandler<ExceptionEventArgs>(fw_Exception);
                _connectionPort.RequestReceived += new EventHandler<PortForwardEventArgs>(port_RequestReceived);
                _connectionPort.Start();
            }
            catch (Exception e)
            {
                throw new ServiceException(e.Message);
            }
        }
Example #2
0
        private void Start()
        {
            iProxyServer.Start(this);
            XElement body = new XElement("getaddress");
            body.Add(new XElement("uidnode", iDeviceUdn));
            XElement tree = CallWebService("getaddress", body.ToString());
            if (tree == null)
                return;
            XElement error = tree.Element("error");
            if (error != null)
            {
                Logger.ErrorFormat("Remote access method {0} failed with error {1}.", "getaddress", error.Value);
                return;
            }

            XElement successElement = tree.Element("success");
            XElement sshServerElement = successElement.Element("sshserver");
            iSshServerHost = sshServerElement.Element("address").Value;
            iSshServerPort = Convert.ToInt32(sshServerElement.Element("port").Value);
            XElement portForwardElement = successElement.Element("portforward");
            iPortForwardAddress = portForwardElement.Element("address").Value;
            iPortForwardPort = (uint)Convert.ToInt32(portForwardElement.Element("port").Value);

            if (Environment.OSVersion.Platform.ToString() == "Unix")
            {
                iSshClientNative = new Process();
                ProcessStartInfo startInfo = new ProcessStartInfo
                                                 {
                                                     WindowStyle = ProcessWindowStyle.Hidden,
                                                     FileName = "ssh",
                                                     Arguments = String.Format(
                                                             "-i {0} -p {1} -R {2}:{3}:{4}:{5} -N {6}@{7} -o StrictHostKeyChecking=no",
                                                             FileFullName(kFilePrivateKey), iSshServerPort, iPortForwardAddress,
                                                             iPortForwardPort, iNetworkAdapter, iProxyServer.Port, kSshServerUserName,
                                                             iSshServerHost)
                                                 };
                iSshClientNative.StartInfo = startInfo;
                iSshClientNative.Start();
            }
            else
            {
                PrivateKeyFile pkf = new PrivateKeyFile(FileFullName(kFilePrivateKey));
                iSshClient = new SshClient(iSshServerHost, iSshServerPort, kSshServerUserName, pkf);
                iSshClient.Connect();
                Logger.InfoFormat("Connected to ssh server at {0}:{1}", iSshServerHost, iSshServerPort);
                iForwardedPortRemote = new ForwardedPortRemote(iPortForwardAddress, iPortForwardPort, iNetworkAdapter, iProxyServer.Port);
                iSshClient.AddForwardedPort(iForwardedPortRemote);
                iForwardedPortRemote.Start();
            }

            Logger.InfoFormat("Forwarded remote port {0}:{1} to {2}:{3}", iPortForwardAddress, iPortForwardPort, iNetworkAdapter, iProxyServer.Port);
            iConnectionCheckTimer.Enabled = true;
        }
Example #3
0
 private void Stop()
 {
     iConnectionCheckTimer.Enabled = false;
     iProxyServer.Stop();
     if (iSshClient != null)
     {
         if (iForwardedPortRemote != null)
         {
             iForwardedPortRemote.Stop();
             iForwardedPortRemote.Dispose();
             iForwardedPortRemote = null;
         }
         iSshClient.Dispose();
         iSshClient = null;
     }
     if (iSshClientNative != null)
     {
         iSshClientNative.Kill();
         iSshClientNative.Dispose();
         iSshClientNative = null;
     }
 }
Example #4
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;
        }