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.

ssh.getForwardingPolicy().allowForwarding();

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.

ssh.getForwardingPolicy().allowGatewayForwarding();

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("127.0.0.1", 8080, "127.0.0.1", 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("127.0.0.1", 0, "127.0.0.1", 80);

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

ssh.stopLocalForwarding("127.0.0.1", 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:

ssh.stopLocalForwarding();

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("127.0.0.1", 8080, "127.0.0.1", 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("127.0.0.1", 0, "127.0.0.1", 80);

Remote forwarding can be cancelled with:

ssh.stopRemoteForwarding("127.0.0.1", 8080);

And all remote forwarding with:

ssh.stopRemoteForwarding();