我编写了一个Java程序,尝试通过代理服务器连接到SFTP服务器。代理服务器是我已安装在本地网络中另一台计算机上的Shadowsocks服务器。我正在使用Java 8和j2ssh-maverick 1.5
我已经按照示例创建了j2ssh-maverick随附的SFTP代理连接。当我尝试与代理服务器建立第一个连接时,在尝试获取从未获得的远程标识的同时,执行在SshConnect类中挂起。
private void connect() throws ProcessorException {
try {
boolean useProxy = StringUtils.isNotBlank(System.getProperty("wwProxyHost"));
if (useProxy) {
String proxyHost = System.getProperty("wwProxyHost");
int proxyPort = Integer.parseInt(System.getProperty("wwProxyPort", "1080"));
logger.info("Connecting to SFTP server through proxy "+proxyHost+":"+proxyPort);
String proxyUser = System.getProperty("wwProxyUser", null);
SocketTransport socketTransport = new SocketTransport(proxyHost, proxyPort);
// Execution doesn't go beyong the line below
SshClient proxySshClient = sshConnector.connect(socketTransport, proxyUser);
if (StringUtils.isNotBlank(proxyUser)) {
logger.debug("Trying to authenticate proxy server with user "+proxyUser);
PasswordAuthentication proxyAuthentication = new PasswordAuthentication();
proxyAuthentication.setPassword(System.getProperty("wwProxyPassword", ""));
int proxyAuthResult = proxySshClient.authenticate(proxyAuthentication);
if (proxyAuthResult != SshAuthentication.COMPLETE) {
throw new ProcessorException("Cannot authenticate proxy server "+proxyHost+":"+proxyPort+" with user "+proxyUser);
}
logger.debug("Proxy authentication completed");
}
SshTunnel tunnel = proxySshClient.openForwardingChannel(host, port, "127.0.0.1", 22, "127.0.0.1", 22, null, null);
sshClient = sshConnector.connect(tunnel, user);
} else {
SocketTransport socketTransport = new SocketTransport(host, port);
socketTransport.setTcpNoDelay(true);
sshClient = sshConnector.connect(socketTransport, user);
}
PublicKeyAuthentication authentication = new PublicKeyAuthentication();
SshPrivateKeyFile sshPrivateKeyFile = SshPrivateKeyFileFactory.parse(new FileInputStream(identity));
int authResult = -1;
if (sshPrivateKeyFile.isPassphraseProtected()) {
throw new ProcessorException("Using passphrase protected key files not implemented");
} else {
SshKeyPair keyPair = sshPrivateKeyFile.toKeyPair(null);
authentication.setPrivateKey(keyPair.getPrivateKey());
authentication.setPublicKey(keyPair.getPublicKey());
authResult = sshClient.authenticate(authentication);
}
if (authResult == SshAuthentication.COMPLETE && sshClient.isConnected()) {
sftpClient = new SftpClient(sshClient);
sftpClient.setTransferMode(SftpClient.MODE_BINARY);
} else {
throw new ProcessorException("Authentication didn't complete or SSH clients is not connected; (authResult=" + authResult + ", conected=" + sshClient.isConnected() + ")");
}
} catch (IOException | SshException | InvalidPassphraseException | SftpStatusException | ChannelOpenException e) {
throw new ProcessorException("Cannot connect to " + host+":"+port, e);
}
}
一段时间后,从输入流中读取-1,并引发SshException,声称“无法读取远程标识”。
这是来自堆栈跟踪:
Caused by: com.sshtools.ssh.SshException: Failed to read remote identification [Unknown cause]
at com.sshtools.ssh.SshConnector.getRemoteIdentification(SshConnector.java:355) ~[j2ssh-maverick-1.5.5.jar:?]
at com.sshtools.ssh.SshConnector.connect(SshConnector.java:296) ~[j2ssh-maverick-1.5.5.jar:?]
at com.sshtools.ssh.SshConnector.connect(SshConnector.java:169) ~[j2ssh-maverick-1.5.5.jar:?]
任何帮助将不胜感激