Sample code for 30+ languages & platforms
PureBasic

SSH Tunnel Through an Outbound HTTP Proxy

See more SSH Tunnel Examples

Demonstrates reaching the SSH server through an outbound HTTP proxy using the HttpProxyHostname, HttpProxyPort, HttpProxyUsername, HttpProxyPassword, HttpProxyAuthMethod, and HttpProxyDomain properties.

Background: Many corporate networks force outbound traffic through an HTTP proxy, which reaches non-HTTP services such as SSH via the CONNECT method — so the proxy must be configured to allow CONNECT to the SSH port. HttpProxyAuthMethod is Basic or NTLM, with HttpProxyDomain supplying the Windows domain for NTLM. Note that if SocksVersion is set, the SOCKS proxy takes precedence and these HTTP settings are ignored.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkSshTunnel.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates reaching the SSH server through an outbound HTTP proxy using the SshTunnel
    ;  properties HttpProxyHostname, HttpProxyPort, HttpProxyUsername, HttpProxyPassword,
    ;  HttpProxyAuthMethod, and HttpProxyDomain.

    tunnel.i = CkSshTunnel::ckCreate()
    If tunnel.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    ;  The HTTP proxy through which the outbound connection to the SSH server is made.  Common HTTP
    ;  proxy ports include 8080 and 3128.  (If SocksVersion is 4 or 5, the SOCKS proxy takes
    ;  precedence and these settings are ignored.)
    CkSshTunnel::setCkHttpProxyHostname(tunnel, "proxy.example.com")
    CkSshTunnel::setCkHttpProxyPort(tunnel, 8080)

    ;  Proxy authentication, if the proxy requires it.  Valid HttpProxyAuthMethod values are "Basic"
    ;  and "NTLM".  HttpProxyDomain is the optional Windows domain used for NTLM.
    CkSshTunnel::setCkHttpProxyUsername(tunnel, "myProxyLogin")
    CkSshTunnel::setCkHttpProxyPassword(tunnel, "myProxyPassword")
    CkSshTunnel::setCkHttpProxyAuthMethod(tunnel, "NTLM")
    CkSshTunnel::setCkHttpProxyDomain(tunnel, "CORP")

    CkSshTunnel::setCkDestHostname(tunnel, "db.internal.example.com")
    CkSshTunnel::setCkDestPort(tunnel, 5432)

    sshPort.i = 22
    success = CkSshTunnel::ckConnect(tunnel,"ssh.example.com",sshPort)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

    ;  Normally you would not hard-code the password in source.  You should instead obtain it
    ;  from an interactive prompt, environment variable, or a secrets vault.
    password.s = "mySshPassword"

    success = CkSshTunnel::ckAuthenticatePw(tunnel,"mySshLogin",password)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

    listenPort.i = 1080
    success = CkSshTunnel::ckBeginAccepting(tunnel,listenPort)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf

    Debug "Tunnel established through the HTTP proxy."

    waitForThreadExit.i = 1
    success = CkSshTunnel::ckCloseTunnel(tunnel,waitForThreadExit)
    If success = 0
        Debug CkSshTunnel::ckLastErrorText(tunnel)
        CkSshTunnel::ckDispose(tunnel)
        ProcedureReturn
    EndIf



    CkSshTunnel::ckDispose(tunnel)


    ProcedureReturn
EndProcedure