Warning
This project is no longer maintained.
SSH Proxy is built on the original JSch, which is no longer maintained
and lacks support for modern algorithms such as rsa-sha2-256/512 or ssh-ed25519.
Apache MINA SSHD now covers the use case of this library out of the box:
it reads ~/.ssh/config (including multi-hop ProxyJump) and supports local port forwarding.
We recommend migrating to Apache MINA SSHD. See Migration to Apache MINA SSHD.
A pure Java implementation for SSH port tunneling that is able to understand
OpenSSH configurations which involve multiple hops to reach a target host.
This library essentially combines JSch with the ability to understand
ProxyJump or ProxyCommand configurations in your local ~/.ssh/config
file.
See our blog post "Tunneling Basics – Part II: OpenSSH Configuration Files" for some context where and how this library can be used.
Add the following Maven dependency to your project:
<dependency>
<groupId>de.cronn</groupId>
<artifactId>ssh-proxy</artifactId>
<version>1.6</version>
</dependency># cat ~/.ssh/config
Host jumpHost1
User my-user
HostName jumphost1.my.domain
Host jumpHost2
User other-user
ProxyJump jumpHost1
Host targetHost
ProxyCommand ssh -q -W %h:%p jumpHost2
try (SshProxy sshProxy = new SshProxy()) {
int targetPort = 1234;
int randomLocalPort = sshProxy.connect("jumpHost2", "targetHost", targetPort);
try (Socket s = new Socket(SshProxy.LOCALHOST, randomLocalPort)) {
OutputStream out = s.getOutputStream();
InputStream in = s.getInputStream();
// ...
}
}Add the dependency org.apache.sshd:sshd-core. The equivalent of the example above:
import java.util.concurrent.TimeUnit;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.config.keys.ClientIdentity;
import org.apache.sshd.client.keyverifier.DefaultKnownHostsServerKeyVerifier;
import org.apache.sshd.client.keyverifier.RejectAllServerKeyVerifier;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.config.keys.FilePasswordProvider;
import org.apache.sshd.common.util.net.SshdSocketAddress;
try (SshClient client = SshClient.setUpDefaultClient()) {
// The defaults accept any server key and load no keys from ~/.ssh
client.setServerKeyVerifier(new DefaultKnownHostsServerKeyVerifier(RejectAllServerKeyVerifier.INSTANCE));
client.setKeyIdentityProvider(ClientIdentity.loadDefaultKeyPairProvider(true, true, FilePasswordProvider.EMPTY));
client.start();
// ProxyJump hops from ~/.ssh/config are resolved automatically
try (ClientSession session = client.connect("jumpHost2").verify(10, TimeUnit.SECONDS).getClientSession()) {
session.auth().verify(10, TimeUnit.SECONDS);
int targetPort = 1234;
SshdSocketAddress local = session.startLocalPortForwarding(
new SshdSocketAddress("localhost", 0),
new SshdSocketAddress("targetHost", targetPort));
try (Socket s = new Socket("localhost", local.getPort())) {
// ...
}
}
}Differences to be aware of:
ProxyCommandis not executed. ReplaceProxyCommand ssh -q -W %h:%p jumpHostwithProxyJump jumpHost.- Like OpenSSH, entries in
~/.ssh/known_hostsfor non-default ports must have the form[host]:port. - In strict mode (first argument of
loadDefaultKeyPairProvider),~/.sshand the key files must not be accessible by other users.
- Java 17+
- JSch (with JZlib)