Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

SSH Tunnel Through an Outbound SOCKS Proxy

See more SSH Tunnel Examples

Demonstrates reaching the SSH server through an outbound SOCKS proxy using the SocksVersion, SocksHostname, SocksPort, SocksUsername, and SocksPassword properties.

Background: This is the reverse direction from dynamic forwarding: here a SOCKS proxy is used to reach the SSH server, for networks where outbound connections must pass through one. SocksVersion is the switch — 0 means no SOCKS, while 4 or 5 selects the protocol. SOCKS5 supports username/password authentication and remote DNS; SOCKS4 supports only a username.

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.SshTunnel;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  tunnel: TSshTunnel;
  sshPort: Integer;
  password: string;
  listenPort: Integer;
  waitForThreadExit: Boolean;

begin
  success := False;

  //  Demonstrates reaching the SSH server through an outbound SOCKS proxy using the SshTunnel
  //  properties SocksVersion, SocksHostname, SocksPort, SocksUsername, and SocksPassword.

  tunnel := TSshTunnel.Create;

  //  Set SocksVersion to 4 or 5 to route the outbound connection to the SSH server through a SOCKS
  //  proxy.  A value of 0 (the default) means no SOCKS proxy.
  tunnel.SocksVersion := 5;
  tunnel.SocksHostname := 'socks.example.com';
  tunnel.SocksPort := 1080;

  //  SOCKS4 supports only a username; SOCKS5 supports username and password.
  tunnel.SocksUsername := 'myProxyLogin';
  tunnel.SocksPassword := 'myProxyPassword';

  tunnel.DestHostname := 'db.internal.example.com';
  tunnel.DestPort := 5432;

  //  Connect to the SSH server through the SOCKS proxy configured above.
  sshPort := 22;
  success := tunnel.Connect('ssh.example.com',sshPort);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;

  //  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 := 'mySshPassword';

  success := tunnel.AuthenticatePw('mySshLogin',password);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;

  listenPort := 1080;
  success := tunnel.BeginAccepting(listenPort);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;
  WriteLn('Tunnel established through the SOCKS proxy.');

  waitForThreadExit := True;
  success := tunnel.CloseTunnel(waitForThreadExit);
  if (success = False) then
    begin
      WriteLn(tunnel.LastErrorText);
      Exit;
    end;


  tunnel.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.