Archived Forum Post

Index of archived forum posts

Question:

Stand alone SSH tunnel (port forwarding) - does it really work?

Aug 09 '16 at 08:48

Hi,

I'm trying to setup an SSH tunnel using the SSH (VB.NET) library but it doesn't seem to work for me. At least not the way I am expecting it.

I started something very simple: took the example from "SSH Tunnel for Database Connection" and split the code in two methods, one to start the listener and one to stop it. I assume that while the listener is up and the connection is established successfully, I can use another (external) application (e.g. browser, telnet, etc.) to utilize the forwarding.

In my example, I tried to access a web server via the tunnel. In fact, from the log I can see that the SSH connection is established and some traffic is forwarded initially: when I try accessing it with my browser, I can see some communication but no where close to the full thing. The full page never shows up and the browser keeps waiting. Doing the forwarding from the same network with an app such as "Bitvise SSH Client", the full page loads instantly.

Here are the two methods of my class:

Public Function StartListening(ByRef sCmdOutput As String) As Boolean

  m_SSH = New Chilkat.SshTunnel

  Dim bSuccess As Boolean
  bSuccess = m_SSH.UnlockComponent("test")
  If (bSuccess <> True) Then
     sCmdOutput = m_SSH.LastErrorText
     Return False
  End If

  '  SSH server
  m_SSH.SshHostname = m_sHost
  m_SSH.SshPort = m_iPort
  m_SSH.SshLogin = m_sLogin
  m_SSH.SshPassword = m_sPassword

  ' Remote host/port
  m_SSH.DestHostname = m_sTunnelRemoteAddr
  m_SSH.DestPort = m_iTunnelRemotePort

  '  Start accepting connections in a background thread.
  '  The SSH tunnels are autonomously run in a background
  '  thread.  There is one background thread for accepting
  '  connections, and another for managing the tunnel pool.
  bSuccess = m_SSH.BeginAccepting(m_iTunnelLocalPort)
  If (bSuccess <> True) Then
     sCmdOutput = m_SSH.LastErrorText
     Return False
  End If

  Return True

End Function

Public Function StopListening(ByRef sCmdOutput As String) As Boolean

  Try
     If Not m_SSH Is Nothing Then
        '  stop the background tunnel threads:
        '  Stop the background thread that accepts new connections:
        Dim bSuccess As Boolean = m_SSH.StopAccepting()
        If (bSuccess <> True) Then
           sCmdOutput = m_SSH.LastErrorText
           Return False
        End If

        '  If any background tunnels are still in existence (and managed
        '  by a single SSH tunnel pool background thread), stop them...
        Dim iMaxWaitMs As Integer = 1000
        bSuccess = m_SSH.StopAllTunnels(iMaxWaitMs)
        If (bSuccess <> True) Then
           sCmdOutput = m_SSH.LastErrorText
           Return False
        End If
     End If

     Return True
  Finally
     'm_SSH.Dispose()
     m_SSH = Nothing
  End Try

End Function

The only thing I do from my GUI app is to have two buttons: one instantiates the object and calls StartListening(), the other invokes the StopListening().

Should this work or am I missing something?


Answer

have you got any success with it? even i'm facing similar issue ?


Answer

Make sure you're using the latest version of Chilkat...


Answer

Yes i'm using the latest version of Chilkat


Answer

The SshTunnel can operate in two different ways. The first way is to set a hard-coded destination host:port, such as what is done in the code above.

 m_SSH.DestHostname = m_sTunnelRemoteAddr
 m_SSH.DestPort = m_iTunnelRemotePort

This is reasonable if all communications are directed to a single location, such as a database server.

However, HTTP traffic would need to be directed to many locations. For that, you use SshTunnel in the second way -- with dynamic port forwarding. You don't set DestHostname or DestPort. Instead, you do this:

// Indicate that the background SSH tunnel thread will behave as a SOCKS proxy server // with dynamic port forwarding: tunnel.DynamicPortForwarding = true;

// We may optionally require that connecting clients authenticate with our SOCKS proxy server. // To do this, set an inbound username/password. Any connecting clients would be required to // use SOCKS5 with the correct username/password. // If no inbound username/password is set, then our SOCKS proxy server will accept both // SOCKS4 and SOCKS5 unauthenticated connections.

tunnel.InboundSocksUsername = "chilkat123"; tunnel.InboundSocksPassword = "password123";

See the example here: https://www.example-code.com/vbnet/sshTunnel_dpf.asp