Remote Reverse Tethering With Gnirehtet
Recently I had some automation work again with Android but this time with a catch, it had to work with remote physical devices that are not reachable over TCP. More than that, I wanted to connect my devices to a VLAN that I assemble and dismantle on demand. I’ve done something similar to it before with Gnirehtet1 for reverse tethering into the VLAN but this was the first time using a remote ADB server.
The first problem I noticed while trying to connect a device using Gnirehtet was
that I could not specify the ADB server host with -H
as I would do with the
ADB client itself. Luckily Gnirehtet’s start command is mostly a small wrapper
around ADB to start the Android application and connect it to the relay, so I
decided to run them manually with the host flag. Sadly, while it seemed to start
the application properly, no packet passed to my computer over the reverse
forwarding set-up by ADB. These were the commands:
gnirehtet relay &
adb -H $ADB_HOST -s $DEVICE_SERIAL reverse localabstract:gnirehtet tcp:31416
adb -H $ADB_HOST -s $DEVICE_SERIAL shell am start -a com.genymobile.gnirehtet.START -n com.genymobile.gnirehtet/.GnirehtetActivity
The operations were exactly what happens when I use Gnirehtet’s CLI so I was baffled why the device refused to connect to the relay server. The problem was with how I split the device management into two machines: the physical devices hub and my client controlling them. The ADB reverse forwarding was created through my client but the packets were forwarded to the ADB host, not my client, which didn’t have a Gnirehtet relay to handle the packets so they were dropped.
My solution was simple although I’m still not totally satisfied with it. I made another reverse forwarding from the devices hub to my client machine using an SSH reverse tunnel. Sadly it requires SSH access in addition to the ADB access and it also require managing ports allocation on the device hub to prevent collisions. This was the additional command to forward the port:
ssh -N -R 127.0.0.1:31416:31416 $USER@$ADB_HOST
I’m always open to hearing about better solutions although it solved my problem.