'Connecting to an SFTP channel via proxy returns com.jcraft.jsch.JSchException: ProxySOCKS5: server returns 2

I'm trying to connect to a SFTP channel throughout a Proxy, per requirements of the customer.
The connection should happen with autentication to the final end but not with the proxy.
So far I've tried the following

private ChannelSftp setupJschProxy(String host, String user, String pwd, int ftpPort, int proxyPort, String proxyServer) throws  JSchException {
    log.info("Stabilisco la connessione sftp con Selecta");
    JSch jsch = new JSch();
    java.util.Properties config = new java.util.Properties();
    Session session = jsch.getSession(user, host, ftpPort);
    session.setPassword(pwd);
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    log.info("session.setConfig(config) OK");
    session.setProxy(new ProxySOCKS5(proxyServer, proxyPort));
    log.info("new ProxySOCKS5(proxyServer, proxyPort) OK");
    session.connect();
    ChannelSftp channel = (ChannelSftp) session.openChannel("sftp");
    if (channel.isConnected()) {
        log.info("Connessione stabilita e autenticazione riuscita");
    } else {
        log.warn("Connessione non stabilita, autenticazione non riuscita!");
    }
    return channel;
}

But I had no luck at all in trying to cnnect, because I always get this error

proxyPort)) OK
04-May-2022 16:17:42.687 [main] ERROR i.r.m.service.SendFileService - ProxySOCKS5: com.jcraft.jsch.JSchException: ProxySOCKS5: server returns 2
com.jcraft.jsch.JSchException: ProxySOCKS5: com.jcraft.jsch.JSchException: ProxySOCKS5: server returns 2
at com.jcraft.jsch.ProxySOCKS5.connect(ProxySOCKS5.java:317)
at com.jcraft.jsch.Session.connect(Session.java:231)
at com.jcraft.jsch.Session.connect(Session.java:183)
at it.repmonit.monitoraggiofdr.utility.SftpUploaderProxy.setupJschProxy(SftpUploaderProxy.java:29)
at it.repmonit.monitoraggiofdr.utility.SftpUploaderProxy.<init>(SftpUploaderProxy.java:55)

I've tried to change type of proxy but this isn't working. Is this a kind of issue related to the end server or to something that I'm missing?

EDIT

Added the connection via CLI as requested in the comments

[f701834@bfbfbatch01 batchMultiUso]$ curl -vvv -x thelink.to.theproxy:1080 -T prova.txt sftp://<user>:<pass>@<url for transfer>/<path>/
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* About to connect() to proxy thelink.to.theproxy port 1080 (#0)
* Trying 10.68.96.45...
* Connected to thelink.to.theproxy (10.68.96.45) port 1080 (#0)
* Server auth using Basic with user '<user>'
> PUT sftp://<user>:<pass>@<url for transfer>/<path>/prova.txt HTTP/1.1
> Authorization: Basic <some hashcoded stuff>
> User-Agent: curl/7.29.0
> Host: transfer.selecta.it:22
> Accept: */*
> Proxy-Connection: Keep-Alive
> Content-Length: 0
> Expect: 100-continue
>
* Empty reply from server
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
* Connection #0 to host thelink.to.theproxy left intact
curl: (52) Empty reply from server

Also tried the following

[f701834@bfbfbatch01 batchMultiUso]$ curl -vvv -x -socks5h://thelink.to.theproxy:1080 -T prova.txt sftp://<user>:<pass>@<url for transfer>/<path>/
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* About to connect() to proxy thelink.to.theproxy port 1080 (#0)
* Trying 10.68.96.45...
* Connected to thelink.to.theproxy (10.68.96.45) port 1080 (#0)
* Server auth using Basic with user '<user>'
> PUT sftp://<user>:<pass>@<url for transfer>/<path>/prova.txt HTTP/1.1
> Authorization: Basic <some hashcoded stuff>
> User-Agent: curl/7.29.0
> Host: transfer.selecta.it:22
> Accept: */*
> Proxy-Connection: Keep-Alive
> Content-Length: 0
> Expect: 100-continue
>
* Empty reply from server
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0
* Connection #0 to host thelink.to.theproxy left intact
curl: (52) Empty reply from server

And this

    [f701834@bfbfbatch01 batchMultiUso]$ curl -vvv -x socks5h://thelink.to.theproxy:1080 -T prova.txt sftp://<user>:<pass>@<url for transfer>/<path>/
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0* About to connect() to proxy thelink.to.theproxy port 1080 (#0)
*   Trying 10.68.96.45...
* Can't complete SOCKS5 connection to 0.0.0.0:0. (2)
* Closing connection 0
curl: (7) Can't complete SOCKS5 connection to 0.0.0.0:0. (2)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source