Giuseppe The Dreamer
Giuseppe The Dreamer

Reputation: 111

Trying to connect to an SSH server with on-premise proxy type?

I have a new project where I should connect to an SSH server, with a proxy (which is on-premise). The problem is, that if I don't use proxy, I get an error saying "UnknownHost". But when I use proxy, it says "JSchException ProxySOCKS5: com.jcraft.jsch.JSchException: fail in SOCKS5 proxy". I'm pretty new to sockets, proxies and all these kinds of things, so every advice is appreciated.

JSch jsch = new JSch();
jsch.setKnownHosts("known_hosts");
com.jcraft.jsch.Session session = null;
com.jcraft.jsch.ProxySOCKS5 proxy = new ProxySOCKS5("localhost", 20004);
proxy.setUserPasswd(userName, password);

URL url = new URL("http", "<remoteUrl>", 22, filePath, null);

session = jsch.getSession(userName, hostName, 22);
session.setPassword(password);

session.setProxy(proxy );
session.connect(10000);

I did try a different direction, where I don't use jsch, only java.net. That code:

SocketAddress addr = new InetSocketAddress("localhost", 20004);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, addr);

final String encodedSubaccount = new String(Base64.encodeBase64(subaccount.getBytes()));
final String encodedLocationId = new String(Base64.encodeBase64(locationId.getBytes()));

char[] pwdHelp = [];

Authenticator.setDefault(new Authenticator() {
      @Override
        protected java.net.PasswordAuthentication getPasswordAuthentication() {
           return new java.net.PasswordAuthentication("1." + encodedSubaccount + "." + encodedLocationId , pwdHelp);
                }
            });

URL url = new URL("http", "<remoteUrl>", 22, filePath, null);

HttpURLConnection connection = (HttpURLConnection) url.openConnection(proxy);

With this approach, there is no error, but when I try to getResponseMessage() or code, then it returns only -1 or null.

Can somebody help me out? Thanks in advance

Upvotes: 0

Views: 1514

Answers (1)

GuBo
GuBo

Reputation: 229

I'm not a java developer so I can help you only the infrastructure part of the problem.

UnknownHost: you cannot connect directly that's why you have to use proxy. UnknownHost means java/your machine cannot resolve DNS name to IP address, maybe that DNS name is an inside/private one.

As I see In your java code You try to connect HTTP protocol instead of SSH protocol.

What is the exact task?

Somebody was provided You an on-premise SocksProxy IP and port, and you have to connect via to an inside SSH server?

OR

You have to connect with SSH protocol to the on-premise server to create a local SocksProxy, and you have to connect to an inside server via local SocksProxy?

In the 2. case you can test the connection with ssh command and a web browser:

  1. SSH to on-premise: ssh -D 1080 on-premise_remote_hosntame_or_IP
  2. Setup socksproxy in a webbrowser: Socks proxy ip: 127.0.0.1, port: 1080
  3. In the web browser try to connect to an inside webserver

Upvotes: 1

Related Questions