【发布时间】:2023-03-03 02:53:01
【问题描述】:
我有一个新项目,我应该使用代理(本地)连接到 SSH 服务器。 问题是,如果我不使用代理,我会收到一条错误消息“UnknownHost”。 但是当我使用代理时,它会显示“JSchException ProxySOCKS5:com.jcraft.jsch.JSchException:在 SOCKS5 代理中失败”。 我对套接字、代理和所有这些东西都很陌生,所以每一个建议都值得赞赏。
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);
我确实尝试了不同的方向,我不使用 jsch,只使用 java.net。该代码:
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);
使用这种方法,没有错误,但是当我尝试 getResponseMessage() 或代码时,它只返回 -1 或 null。
有人可以帮帮我吗? 提前致谢
【问题讨论】:
-
有不同种类的代理。您是否收到有关如何连接到此代理或如何通过代理进行 ssh 的说明?你能edit你的问题包括说明吗?
标签: java networking groovy proxy jsch