Using Dynamic Port Forwarding

Lee Painter

The Maverick Legacy Client supports dynamic port forwarding using a SOCKS proxy server. Dynamic forwarding allows you to intercept outgoing socket requests and forward them over an SSH client simply by configuring your system or application to use a SOCKS proxy.

Import the following classes

import socks.ProxyServer;
import socks.server.ServerAuthenticatorNone;
import socks.server.UserPasswordAuthenticator;
import socks.server.UserValidation;

 

Create your SSH connection using the API as normal, the rest of this article assumes you have an SshClient instance that is connected and authenticated to a remote server.

Creating the SOCKS proxy is straight forward. The following code creates and starts the proxy on port 10000. Simply configure your system or clients with localhost:10000 and once the code is running any traffic will be dynamically forwarded over the SshClient instance you provide as the second argument to the ProxyServer constructor.

ProxyServer socks = new ProxyServer(new ServerAuthenticatorNone(), ssh);
socks.start(10000);

 

The start method is blocking so you may need to consider to thread this if your also using the connection to perform other operations.

You can optionally add authentication to the proxy by passing a different type of authenticator.

ProxyServer socks = new ProxyServer(new UserPasswordAuthenticator(new UserValidation() {
public boolean isUserValid(String username, String password, Socket connection) {
return username.equals("user") && password.equals("pass");
}
}), ssh);