Sample code for 30+ languages & platforms
PowerBuilder

SSH Tunnel Dynamic Port Forwarding (Local SOCKS Proxy)

See more SSH Tunnel Examples

Demonstrates dynamic port forwarding with the DynamicPortForwarding, InboundSocksUsername, and InboundSocksPassword properties. In dynamic mode the listener acts as a local SOCKS proxy, so each client chooses its own destination and one tunnel can reach many hosts on the SSH server's network.

Background: This is the ssh -D scenario: instead of pinning one local port to one destination, the tunnel speaks SOCKS and lets each client name where it wants to go. Any SOCKS-capable application — a browser, a mail client — can then reach the remote network through the single tunnel. Setting an inbound SOCKS username and password restricts who may use the local proxy, which matters because it would otherwise accept unauthenticated connections from any local process.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Tunnel
integer li_SshPort
string ls_Password
integer li_ListenPort
integer li_WaitForThreadExit

li_Success = 0

//  Demonstrates dynamic port forwarding with the SshTunnel properties DynamicPortForwarding,
//  InboundSocksUsername, and InboundSocksPassword.
//  
//  In dynamic mode the listener behaves as a local SOCKS proxy: each client chooses its own
//  destination, so one tunnel can reach many hosts on the SSH server's network.

loo_Tunnel = create oleobject
li_rc = loo_Tunnel.ConnectToNewObject("Chilkat.SshTunnel")
if li_rc < 0 then
    destroy loo_Tunnel
    MessageBox("Error","Connecting to COM object failed")
    return
end if

//  Enable dynamic (SOCKS) forwarding instead of a single fixed destination.  No DestHostname or
//  DestPort is needed.
loo_Tunnel.DynamicPortForwarding = 1

//  Optionally require inbound SOCKS5 clients to authenticate with the local proxy.  If left unset,
//  the local proxy accepts unauthenticated SOCKS4 and SOCKS5 connections.
loo_Tunnel.InboundSocksUsername = "myLocalSocksLogin"
loo_Tunnel.InboundSocksPassword = "myLocalSocksPassword"

li_SshPort = 22
li_Success = loo_Tunnel.Connect("ssh.example.com",li_SshPort)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    return
end if

//  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.
ls_Password = "mySshPassword"

li_Success = loo_Tunnel.AuthenticatePw("mySshLogin",ls_Password)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    return
end if

//  Start the local SOCKS proxy on port 1080.  SOCKS-capable clients point at 127.0.0.1:1080.
li_ListenPort = 1080
li_Success = loo_Tunnel.BeginAccepting(li_ListenPort)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    return
end if

Write-Debug "Local SOCKS proxy running on port " + string(li_ListenPort)

li_WaitForThreadExit = 1
li_Success = loo_Tunnel.CloseTunnel(li_WaitForThreadExit)
if li_Success = 0 then
    Write-Debug loo_Tunnel.LastErrorText
    destroy loo_Tunnel
    return
end if



destroy loo_Tunnel