Skip to content

Not Able to connect via proxy #881

@lakshya1210

Description

@lakshya1210

So I am trying to make sftp connection via proxy host and port, but could not able to do it
getConnectionTimeout() = 60 seconds

`

    public Response validateWithMinaEngine(long partnerId, SftpCredentials sftpCredentials) {

    SshClient client = null;
    ClientSession session = null;
    SftpClient sftp = null;

    try {

        ProxyConfig proxyConfig = getProxyConfigMina(true);

        client = createMinaClient(sftpCredentials, proxyConfig);

        if (proxyConfig != null) {
            client.setClientProxyConnector(new HttpProxyConnector(proxyConfig.host, proxyConfig.port, getConnectionTimeout()));
        }

        client.start();

        HostConfigEntry host = new HostConfigEntry(
            "",
            sftpCredentials.getHost(),
            sftpCredentials.getPort(),
            sftpCredentials.getUser()
        );

        session = client.connect(host).verify(getConnectionTimeout(), TimeUnit.MILLISECONDS).getSession();

        if (StringUtils.isNotEmpty(sftpCredentials.getPassword())) {
            session.addPasswordIdentity(sftpCredentials.getPassword());
        }

        session.auth().verify(getConnectionTimeout(), TimeUnit.MILLISECONDS);

        sftp = SftpClientFactory.instance().createSftpClient(session);

        String directory = sftpCredentials.getPathPrefix();
        if (!directory.endsWith("/")) {
            directory += "/";
        }
        sftp.stat(directory);

        return Response.ok(Collections.singletonMap("valid", true)).build();

    } catch (IOException e) {
        logger.error("MINA I/O exception", PIILogMaskingUtils.maskExceptionPII(logger, e));
        throw new ExternalFileException(ExternalFileException.DATABUS_SFTP_FOLDER_ERROR);

    } finally {
        if (sftp != null) try { sftp.close(); } catch (Exception ignore) {}
        if (session != null) try { session.close(); } catch (Exception ignore) {}
        if (client != null) client.stop();
        UserContextProvider.setContext(userContext);
    }
}

private static SshClient createMinaClient(SftpCredentials c, ProxyConfig proxyConfig) {
 
    SshClient client = SshClient.setUpDefaultClient();
    if ("no".equalsIgnoreCase(NO_STRICT_HOST_KEY_CHECK)) {
        client.setServerKeyVerifier(AcceptAllServerKeyVerifier.INSTANCE);
    }

    CoreModuleProperties.AUTH_TIMEOUT.set(client, Duration.ofMillis(getConnectionTimeout()));
    SftpModuleProperties.SFTP_CHANNEL_OPEN_TIMEOUT.set(client, Duration.ofMillis(getConnectionTimeout()));

    return client;
}

public class HttpProxyConnector implements ClientProxyConnector {

        private final String proxyHost;
        private final int proxyPort;
        private final int timeout;

        public HttpProxyConnector(String proxyHost, int proxyPort, int timeout) {
            this.proxyHost = proxyHost;
            this.proxyPort = proxyPort;
            this.timeout = timeout;
        }

        @Override
        public void sendClientProxyMetadata(ClientSession clientSession) throws IOException {
            // For HTTP CONNECT proxy, metadata is sent during connect phase
            // No additional metadata needs to be sent after SSH session is established
            // This method can be empty for HTTP proxies
            logger.error("sendClientProxyMetadata called - no action needed for HTTP proxy");
        }
    }

`

org.apache.sshd.common.SshException: [ssh-connection]: Failed to get operation result within specified timeout: 60000 msec
at org.apache.sshd.common.future.AbstractSshFuture.lambda$verifyResult$1(AbstractSshFuture.java:114)
at org.apache.sshd.common.future.AbstractSshFuture.formatExceptionMessage(AbstractSshFuture.java:206)
at org.apache.sshd.common.future.AbstractSshFuture.verifyResult(AbstractSshFuture.java:114)
at org.apache.sshd.client.future.DefaultAuthFuture.verify(DefaultAuthFuture.java:56)
at org.apache.sshd.client.future.DefaultAuthFuture.verify(DefaultAuthFuture.java:35)
at org.apache.sshd.common.future.VerifiableFuture.verify(VerifiableFuture.java:84)
at org.apache.sshd.common.future.VerifiableFuture.verify(VerifiableFuture.java:69)
at com.spr.rest.http.dataflow.DataFlowRestrictedRestApi.validateWithMinaEngine(DataFlowRestrictedRestApi.java:849)

error stack trace, please help here.
Any alternative method to connect via proxy only with proxy host and proxy port. (there is no proxy Username)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions