Quantcast
Channel: Chilkat Forum - latest questions
Viewing all articles
Browse latest Browse all 1061

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

$
0
0

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?


Viewing all articles
Browse latest Browse all 1061

Trending Articles