This tutorial explains how to set up the following types of forwarding using J2SSH:
Local
Forwarding
Remote Forwarding
Introduction
to Port Forwarding
Port forwarding allows you to transparently secure another applications data
stream by intercepting service requests on one side of the SSH connection, and
forwarding them to the recipient at the other side. This is useful in circumstances
where you wish to secure the communications of an inherently insecure network
application, for example Telnet or SMTP. Once the specifics of the port forward
are established through J2SSH, the secured application may commence communication
as normal, completely unaware of the underlying forwarding mechanism.
Any TCP/IP traffic occuring on the forwarded port is redirected through the
SSH session - this is particularly advantageous in circumstances where certain
protocols are required to pass through a firewall whose rules restrict their
direct usage
In J2SSH, the following class allows the configuration of port forwarding:
import com.sshtools.j2ssh.forwarding.ForwardingClient;
Local
Forwarding
Local forwarding is one of the two variations of forwarding used by the SSH
protocol. By setting up local forwarding, you are specifying that requests initiated
from the local machine are to be redirected over the SSH
communications channel and delivered to the corresponding port at the other
side of the connection. To initiate a
local forward in J2SSH you must do the following after obtaining an authenticated
SSH session:
ForwardingClient forwarding = ssh.getForwardingClient();
// Configure forwarding on local port 10009 to remote port 10007 on mars.sshtools.org forwarding.addLocalForwarding("Test Local", "0.0.0.0", 10009, "mars.sshtools.org", 10007)
// Starts the specified port forward forwarding.startLocalForwarding("Test Local");
Back
to top
Remote Forwarding
Remote forwarding is similar to local forwarding except that the forwarded connection
is initiated from the remote
side. This method should be used when the application client requiring secured
communications is residing at the remote location (the SSH server side), and the
application server is located at the SSH client side. Initiating a remote forward
can be done in a similar manner:
ForwardingClient forwarding = ssh.getForwardingClient();
// Forward remote port 8081 on mars.sshtools.org to local port 8080 forwarding.addRemoteForwarding("Test Remote", "0.0.0.0", 8081, "mars.sshtools.org", 8080)
forwarding.startRemoteForwarding("Test Remote");
Once set up, the
example local and remote forwardings may be removed by specifying:
forwarding.removeLocalForwarding("Test Local"); forwarding.removeRemoteForwarding("Test Remote");