hierynomus / hierynomus/sshj

Http Proxy using CustomSocketFactory in SSHJ fails

Open
#831 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.7k
Forks
620
Avg merge
3d 23h
Merged PRs (30d)
11

Description

My Java 7 application needs to connect to a remote SFTP server via HTTPS proxy. As per the depprecated method for connect via proxy in SocketClient, I created a custom javax.net.SocketFactory which creates a Socket(using java.net.Socket#Socket(java.net.Proxy) ) to the remote SFTP server and injected the SocketFactory into the SocketClient.

Code snippet :

SSHClient client = new SSHClient();
client.addHostKeyVerifier(new PromiscuousVerifier());

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort));
Authenticator.setDefault(new Authenticator() {
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(proxyUser, proxyPassword.toCharArray());
}
});
final SSLSocketFactory factory = (SSLSocketFactory) SSLSocketFactory.getDefault();
Socket socket = new Socket(proxy);
SocketAddress socketAddress=new InetSocketAddress(InetAddress.getLoopbackAddress(),0);
// binding the socket with the inetAddress and port number
socket.bind(socketAddress);

// connect() method connects the specified socket to the server
socket.connect(new InetSocketAddress(sftpHost, sftpPort));

factory.createSocket(socket, socket.getInetAddress().getHostName(), socket.getPort(), true);

client.setSocketFactory(factory);

KeyProvider keyProvider = client.loadKeys(sftpPrivateKeyPath, sftpPrivateKeyPassprhase);
client.authPublickey(sftpUser, keyProvider);

I could connect to the remote SFTP server via the proxy but the public key authentication fails with the error that the socket is not connected. Its because the 'socket' instance in SSHClient is not initialised via client.connect(..)

Caused by: java.rmi.RemoteException: ; nested exception is:
java.lang.IllegalStateException: Not connected
at net.schmizz.sshj.SSHClient.checkConnected(SSHClient.java:823)
at net.schmizz.sshj.SSHClient.auth(SSHClient.java:216)
at net.schmizz.sshj.SSHClient.authPublickey(SSHClient.java:342)
at net.schmizz.sshj.SSHClient.authPublickey(SSHClient.java:360)

I tried to call client.connect(sftpHost, sftpPort) but this is trying to create a new socket using the SocketFactory ignoring the proxy configuration.
Could there be some way to inject the 'socket' itself so that the lib uses custom socket with proxy settings?

Many Thanks in advance for a quick feedback!

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.