org.apache.sshd.client.SshClient How to use proxy ? (SFTP)

I want to connect a client sshd/sftp to use proxy

I use the method 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) {
        }
    }
}

I can't code the Use proxy part. There is no error.

In this example, I call the setClientProxyConnector method to send additional metadata to the proxy. I can't find the code that makes sshd/sftp connect to the server via a proxy.

Upvotes: 0

Views: 185

Answers (0)

Related Questions