我正在寻找一种在我的java应用程序中使用ssh绑定连接到我的远程服务器的方法。我在终端内输入如下命令来连接我的服务器:
ssh -D 1234 username@w.x.y.z
然后我可以配置我的浏览器socks ip&移植到:
socks ip: 127.0.0.1
socks port: 1234
&安培;使用我的服务器浏览互联网
现在请帮我在我的java应用程序中执行此操作。
目前我在我的程序中使用了一个名为JSCH的库,但我无法让我的应用程序工作。 您对此问题有任何想法或示例代码或其他内容吗?
(请注意,java库中应支持SOCKS v4和v5)
答案 0 :(得分:1)
您将获得多个选项
- http://mina.apache.org/sshd-project/documentation.html 受信任的Apache项目,以用户社区,docs& amp;例子 我想这就是你要求的。 https://svn.apache.org/repos/asf/mina/sshd/trunk/sshd-core/src/test/java/org/apache/sshd/PortForwardingTest.java
- http://www.jcraft.com/jsch/ 文档不好但已经存在了很长时间。示例代码非常好。
http://code.google.com/p/ganymed-ssh-2/
另一个只有足够的documnetation的lib。
答案 1 :(得分:0)
有时阅读文档是有意义的。有一个sample on JSch home page可以完全满足您的需求。
答案 2 :(得分:0)
我使用jsch作为示例程序在scala中破解了这样的程序,
https://github.com/ymnk/dpfwds
答案 3 :(得分:0)
对于每个https://www.bytefold.com/java-ssh-tunnel-with-dynamic-port-forwarding/,您可以使用Apache Mina完成此操作:
依赖项
<dependency>
<groupId>org.apache.mina</groupId>
<artifactId>mina-core</artifactId>
<version>3.0.0-M2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.sshd/sshd-core -->
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-core</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-putty</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.sshd</groupId>
<artifactId>sshd-common</artifactId>
<version>2.1.0</version>
</dependency>
代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.InetSocketAddress;
import java.net.MalformedURLException;
import java.net.Proxy;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;
import java.security.GeneralSecurityException;
import java.security.KeyPair;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.sshd.client.SshClient;
import org.apache.sshd.client.auth.hostbased.HostKeyIdentityProvider;
import org.apache.sshd.client.keyverifier.AcceptAllServerKeyVerifier;
import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.config.keys.loader.pem.PEMResourceParserUtils;
import org.apache.sshd.common.config.keys.loader.putty.PuttyKeyUtils;
import org.apache.sshd.common.forward.PortForwardingEventListener;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.util.net.SshdSocketAddress;
import org.apache.sshd.server.channel.PuttyRequestHandler;
import org.apache.sshd.server.forward.AcceptAllForwardingFilter;
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
/**
* This class
*
* @author Ankit Katiyar
*
*/
public class AmazonTest {
private static String BASTION_SERVER_PASSWORD = "P@ssword1";
private static final String BASTION_SERVER_USER = "ec2-user";
private static final String BASTION_SEREVR_HOST = "ec2-18-191-207-91.us-east-2.compute.amazonaws.com";
private static final String URL_TO_ACCESS = "http://www.google.com";
public static void main(String[] args) {
try {
Collection<KeyPair> keys = null;
// OPtional loading keys from a PEM file
//keys=PEMResourceParserUtils.getPEMResourceParserByAlgorithm("RSA").loadKeyPairs(ClassLoader.getSystemResource("local-ps-test.pem").toURI().toURL(), null);
// Optional: Using Putty key for login
keys=PuttyKeyUtils.DEFAULT_INSTANCE.loadKeyPairs(ClassLoader.getSystemResource("local-ps-private-key.ppk").toURI().toURL(), null);
SshClient client = SshClient.setUpDefaultClient();
client.setForwardingFilter(AcceptAllForwardingFilter.INSTANCE);
client.setServerKeyVerifier(AcceptAllServerKeyVerifier.INSTANCE);
client.start();
// using the client for multiple sessions...
try (ClientSession session = client.connect(BASTION_SERVER_USER, BASTION_SEREVR_HOST, 22).verify()
.getSession()) {
// IF you use password to login provide here
// session.addPasswordIdentity(BASTION_SERVER_PASSWORD); // for password-based
// authentication
session.addPublicKeyIdentity(keys.iterator().next());
// authentication
// Note: can add BOTH password AND public key identities - depends on the
// client/server security setup
session.auth().verify(10000);
// start using the session to run commands, do SCP/SFTP, create local/remote
// port forwarding, etc...
session.addPortForwardingEventListener(new PortForwardingEventListener() {
@Override
public void establishedDynamicTunnel(Session session, SshdSocketAddress local,
SshdSocketAddress boundAddress, Throwable reason) throws IOException {
// TODO Auto-generated method stub
PortForwardingEventListener.super.establishedDynamicTunnel(session, local, boundAddress, reason);
System.out.println("Dynamic Forword Tunnel is Ready");
}
});
SshdSocketAddress sshdSocketAddress = session
.startDynamicPortForwarding(new SshdSocketAddress("localhost", 8000));
System.out.println("Host: " + sshdSocketAddress.getHostName());
System.out.println("Port: " + sshdSocketAddress.getPort());
// Create a Proxy object to work with
Proxy proxy = new Proxy(Proxy.Type.SOCKS,
new InetSocketAddress(sshdSocketAddress.getHostName(), sshdSocketAddress.getPort()));
/**
* Now you can use this proxy instance into any URL until this SSH session is active.
*/
// TEST one URL
HttpURLConnection connection = (HttpURLConnection) new URL(URL_TO_ACCESS).openConnection(proxy);
System.out.println("Proxy work:" + connection.getURL());
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
System.out.println("================== Data From URL ==================\n");
String inputLine;
while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
System.out.println("================== Data From URL ==================\n");
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
可以在https://github.com/ankitkatiyar91/java-framework-examples/tree/master/java-tunneling上找到他的项目
依赖关系可能已过期,API可能已更改,但这应该可以帮助您入门。我在30分钟内就使用了最新版本的Apache Mina(2.1.4)。不要被Mina-core的3.0.0-M2版本所吸引。此版本实际上早于2.1.4版本。