我搜索连接客户端 sshd/sftp 以使用代理
我的代码是JAVA / MAVEN
我使用方法setClientProxyConnector ...
`<sshd-sftp.version>2.12.1</sshd-sftp.version>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>${sshd-sftp.version}</version>
</dependency>
package com.reservit.sftp;
import org.apache.logging.*;
import org.apache.sshd.*;
public class SshSftpBO {
public void uploadFile(String tmpFile, String fileName) throws SendingSystemException {
try {
String host = this.sftpVO.getHost();
int port = this.sftpVO.getPort();
String username = this.sftpVO.getLogin();
String password = this.sftpVO.getSecret();
String remoteDirectory = this.sftpVO.getDirectory();
byte[] bytes = Files.readAllBytes(Paths.get(tmpFile));
try (SshClient client = SshClient.setUpDefaultClient()) {
client.start();
client.setClientProxyConnector(new ClientProxyConnector() {
@Override
public void sendClientProxyMetadata(ClientSession clientSession) throws Exception {
// ??
}
protected SshdSocketAddress getProxyAddress() {
// Code not executed
return new SshdSocketAddress("proxy.dev", 3128);
}
});
try (ClientSession session = client.connect(username, host, port).verify().getSession()) {
session.addPasswordIdentity(password);
session.auth().verify();
try (SftpClient sftpClient = SftpClientFactory.instance().createSftpClient(session)) {
try (OutputStream outputStream = sftpClient.write(remoteDirectory + "/" + fileName)) {
outputStream.write(bytes);
}
}
}
}
} catch (Exception e) {
}
}
}`
我无法编写“使用代理”部分的代码。没有错误。
在此示例中,我调用 setClientProxyConnector 方法将其他元数据发送到代理。我找不到使 sshd/sftp 通过代理连接到服务器的代码
// Set the proxy details here
SshdSocketAddress proxyAddress = new SshdSocketAddress("proxyAddress", 3128); // Proxy address and port
client.setClientProxyConnector(new Socks5ProxyConnector(proxyAddress));