问题
I want to use ssh dynamic port forwarding in my android code.
I used the library jcraft, but unfortunately this library doesn't support dynamic port forwarding and just support local port forwarding and remote port forwarding.
In the other word, I want to ssh like this, in my android code:
ssh -D 10000 user@host
Can anybody help me?
回答1:
JSCH is a Java implementation of the SSH protocol. It's not a complete reimplementation of the OpenSSH ssh
command-line utility. In the case of the dynamic proxy feature, JSCH provides the ability to tunnel TCP connections through an SSH session, but it doesn't provide the SOCKS proxy server needed for the client to receive the connections which are to be forwarded.
To make this work, you would embed a SOCKS proxy into your application. When a client connects to the application's SOCKS port (the equivalent of port 10000 in your example), it will use the SOCKS protocol to tell your application what host and port it wants to be connected to.
Then your application--which has presumably used JSCH to create an SSH session to some remote server--would use JSCH to open a "direct-tcpip" channel to the host and port requested by the client. Then your application would relay data between the direct-tcpip channel and the client which requested the proxy.
来源:https://stackoverflow.com/questions/50333231/ssh-dynamic-port-forwarding