DEV Community

Marius Teufelweich
Marius Teufelweich

Posted on • Originally published at gist.github.com

Sharing the WWW with a remote host that has no internet connection

Recently I had the following issue with my Turtlebots: They are only connected to a local network without a route to an uplink with internet connection. This is okay as I just want them to communicate with other ROS2 devices inside the local network. But sometimes I want to update or install a new package with apt or pip. Usually the Turtlebot/Pi would need a internet connection for this. But inside this local network only my notebook has internet access through a different network interface. I could use the notebook as a router but I don't want to get into the hassle of doing this, as network stuff with ROS2 is already complex enough.

But there is another way to achieve this communication from the Turtlebot to the WWW via my notebook: the SOCKS5 proxy.

SSH SOCKS5 proxy

When I ssh onto the Turtlebot (remotehost) I can use the -R option to open a SOCKS5 proxy port on the remotehost which forwards the communication to my notebook.

With ssh -R 1080 user@remotehost I open the connection on the default SOCKS5 port.

We can check if the proxy works with curl --socks5-hostname localhost http://captive.apple.com which should return:

<HTML><HEAD><TITLE>Success</TITLE></HEAD><BODY>Success</BODY></HTML>
Enter fullscreen mode Exit fullscreen mode

The --socks5-hostname is especially important here, as this forwards the DNS resolving on to the notebook, where as --socks5 would try to resolve it on the remotehost (which can't do it because it has no internet connection).

APT

Now we have to tell apt to use this proxy. For this we have to add a config to /etc/apt/apt.conf.d/ and write Acquire::http::proxy "socks5h://localhost:1080"; into it.
This one liner does it:

echo "Acquire::http::proxy "socks5h://localhost:1080";"  | sudo tee -a /etc/apt/apt.conf.d/12proxy
Enter fullscreen mode Exit fullscreen mode

(You can remove the port, as 1080 is the default one, but I like it more explicit as it doesn't hurt inside such config files)
As with curl it is important to use Acquire::http::proxy and not Acquire::socks::proxy with socks5h and not socks5 as only the combination used above forwards the DNS resolving to the notebook.

Now you can happily run sudo apt update and you should see something like:
0% [Connecting to SOCKS5h proxy (socks5h://localhost:1080)]

pip

Out of the box, pip can't use SOCKS sockets. We first have to install pysocks. We could do this by downloading it with curl or scp the file from the notebook, but the more convenient way would be to install it with apt (this isn't ideal, but bootstrapping never is):
sudo apt install python3-socks

Now we can install any python package with pip:
pip install --proxy socks5h://localhost:1080 <package>

tsocks for everything else

If I would have other software that I want to connect via the SOCKS5 socket, but doesn't bring support for it, I would try using tsocks.

Top comments (0)