Arya
Arya

Reputation: 8965

Using JSch to create a SOCKS proxy tunneled through SSH

As far as I heave read, it is possible to create a SSH tunnel using JSch, and then put the settings in Firefox as a SOCKS5 proxy and all the traffic would go through the machine JSch is connected to. I have found the the following code but there is somethings I don't understand about it.

String host = "my ssh server ip";
String user = "root";
String password = "mypass";
int port = 22;

int tunnelLocalPort = 9080;
String tunnelRemoteHost = "YYY.YYY.YYY.YYY";
int tunnelRemotePort = 80;

JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
localUserInfo lui = new localUserInfo();
session.setUserInfo(lui);
session.connect();
session.setPortForwardingL(tunnelLocalPort, tunnelRemoteHost, tunnelRemotePort);
System.out.println("Connected");

tunnelLocalPort would be the port that my java program would be listening on? this is the port I put the Firefox SOCKS5 proxy settings?

I don't understand what tunnelRemoteHost is for, I want this to act like a SOCKS5 proxy, just like PuTTY does when tunneling is setup on it.

Upvotes: 1

Views: 1485

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202088

JSch indeed allows creating an SSH tunnel. But the "dynamic" port forwarding feature of PuTTY (which, I assume, you are referring to) is a lot more than an SSH tunnel. It particularly implements the SOCKS proxy (what you are after). That's something that JSch does not implement.

For a plain SSH tunnel/port forwarding, you have to specify the target address, to connect the other end of the tunnel to (tunnelRemoteHost). That's obviously not necessary in the PuTTY "dynamic" mode, as there a proxy protocol (SOCKS) takes care of that.

Upvotes: 1

Related Questions