Port Forwarding

One of the major performance improvements in the Maverick Synergy API has been in the performance of tunnels over the SSH connection. This has benefitted from the move to NIO from standard sockets and threaded/blocking I/O.

By default, the API will not allow port forwarding. You have to enable this on your SshClient before you use the forwarding API.


This will allow forwarding only from the localhost interface. If you want to allow external hosts to access sockets listening for forwarding connections, you also need to enable gateway forwarding.


Local Forwarding

You can start a local forwarding with the startLocalForwarding method. For example, the following code creates a local listener on port 8080 and forwards this to port 80 on the remote host.

ssh.startLocalForwarding("", 8080, "", 80);

If you want to start the local listener on a random port, pass zero as the local port number and consult the integer value returned for the random port.

int randomPort = ssh.startLocalForwarding("", 0, "", 80);

To stop the local forwarding at any time during the lifetime of the connection use:

ssh.stopLocalForwarding("", 8080);

When starting with a random port, pass the randomly chosen port back to the stop method.

You can also stop all local forwarding with:


Remote Forwarding

Remote forwarding allows connections from the remote network back to your local machine or other services in the local network.

You can start a remote forwarding on the remote machine using:

ssh.startRemoteForwarding("", 8080, "", 80);

Similarly to local forwarding, you can request the remote side start the forwarding on a random port. This will be returned to you from the startRemoteForwarding method.

int randomPort = ssh.startRemoteForwarding("", 0, "", 80);

Remote forwarding can be cancelled with:

ssh.stopRemoteForwarding("", 8080);

And all remote forwarding with: