SSHをサポートするファイアウォールの背後にあるサービスを使用するためのトンネルを作成しようとしています。Javaで完全なソリューションが欲しかったのですが、うまく機能しないようです。私はこのgithubスニップを見つけ、それに基づいて、トンネルを提供するバックグラウンドスレッドを維持するために次のコードを作成しました。
// property on surrounding class
// static final SSHClient sshclient = new SSHClient();
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
String host = "10.0.3.96";
sshclient.useCompression();
sshclient.addHostKeyVerifier("30:68:2a:20:21:9f:c8:e8:ac:b4:a7:fc:2d:a7:d0:26");
sshclient.connect(host);
sshclient.authPassword("messy", "messy");
if (!sshclient.isAuthenticated()) {
throw new RuntimeException(String.format("Unable to authenticate against '%s'", host));
}
Forward forward = new Forward(8111);
InetSocketAddress addr = new InetSocketAddress("google.com", 80);
SocketForwardingConnectListener listener = new SocketForwardingConnectListener(addr);
sshclient.getRemotePortForwarder().bind(forward, listener);
sshclient.getTransport().setHeartbeatInterval(30);
HttpURLConnection con = (HttpURLConnection) new URL("http://localhost:8111").openConnection();
con.setRequestMethod("GET");
BufferedReader reader = new BufferedReader(new InputStreamReader(con.getInputStream()));
String line = null;
while( (line = reader.readLine()) != null) {
System.out.println(line);
}
sshclient.getTransport().join();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (sshclient != null && sshclient.isConnected()) {
sshclient.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
});
thread.setDaemon(true);
thread.start();
問題は、「10.0.3.96」のようなリモートSSHサーバーに接続すると機能しないことです。ローカルホストでローカルSSHサーバーを使用すると、機能します。運が良ければさまざまな構成を試し、デバッグを試みましたが、SSHjパッケージ内で何が起こっているのか理解できません。
これでSSHjソリューションである必要はありませんが、コードの他の部分が完全に実装され、SSHjを使用しており、1つのプロジェクトに2つのSSHパッケージを混在させたくないためです。
助けてくれてありがとう。