Search code examples
javaandroidsshtunnel

Java / or Android IP tunnel


We need to tunnel a port on a cloud server to an Android device. Now, we're using a SSH client on Android (Jsch) to initiate a reverse port forward. This works, but we do not need / want the overhead of encryption / authentication.

Essentially, a simple IP tunnel that can be established from the client side (Android) to a server in the cloud.

Googling on this topic was not promising, so I thought I'd give it a try here and see if I can get any leads. Obviously we'd rather not build this ourselves from scratch.


Solution

  • This is actually rather simple to implement in Java. The tunneler app on the device opens two connections, one to the server and one to the service on the device. Now if you use Input-/OutputStreams (as as ooposed to nio Channels) you need 2 threads that are each reading on one of the InputStreams and just write everything they received to the opposite output stream. On the server you will need a similar process that gets it's two connections from ServerSocket.accept and needs to be able to find out which one is the tunneler and which it a client of the service (i.e. tests for the header of the tunneled protocol on incoming connections).

    This becomes a bit more complicated if you want to be able to tunnel multiple connections at the same time. You would need to implement some sort of protocol for this to be able to dispatch the tunneled packets to multiple connections on the device if you want to avoid to open multiple tunnel connections.