Using N2N VPN to Enable NAS Remote Accessing

n2n-virtual-network

Background

I have multiple devices including a NAS, a Raspberry Pi, and two MacBooks in different networks, my home and office. All I need is that I want to let all these devices can be accessed in any of those networks. After searching on the internet, I found out n2n should be the one I looked for. The architecture and setup are very simple 👍.

Steps to Setup N2N Virtual Network

1. Setup a super node

The supernode should be run on a device that can be accessed from the public network. I'm using a VPS with public IP to run this, so all edge nodes can connect to it directly. The VPS I have is based on Debian, and the following commands only testyed on Debian in this post. Let's say the public IP of the VPS is 200.200.200.200.

supernode -l 9053
# output

-> supernode -l 9053
07/Aug/2019 06:45:47 [supernode.c: 476] Supernode ready: listening on port 9053 [TCP/UDP]

2. Run edge nodes

We can have multiple edge nodes that connect to one super node. Don't forget to change the virtual IP of the following command.

edge -d n2n0 -a 192.168.100.1 -c YOU_NETWORK_NAME -k YOUR_PASS -l 200.200.200.200:9053

now, we have a device with private network IP 192.168.100.1. If you have run this command on other devices, you can ping it from each other.

Run n2n in Docker Container

To run N2N VPN on macOS is not a good choice, I run into problems. It's better to use docker or vagrant, it depends on you.

# Dockerfile

FROM debian:stable-slim

RUN apt-get update && apt-get -y install iputils-ping n2n socat
# Build the Dockerfile
docker build -f Dockerfile -t n2n .

# Run a edge node
docker run -d -p 9091:9091/tcp --name=n2n --privileged --rm -it n2n edge -d n2n0 -a 192.168.100.1 -c YOU_NETWORK_NAME -k YOU_PASS -l 200.200.200.200:9053

Enable Port Forwarding

The port of 9091 is used by Transmission. At first, I tried to use iptables to forward port, so I can visit a remote Transmission by typing http://localhost:9091 in my browser on docker host device. But, it turns out not working, the socat is a good one for port forwarding. I also encountered a known bug of Linux, here the link if you are interested in it.

Run the command below in your edge node container.

# Forward a port

ifconfig n2n0 mtu 500
nohup socat TCP4-LISTEN:9091,fork TCP4:192.168.100.1:9091 &

Then, we can visit the Transimission web UI on my Chrome browser.

REPOSITORY TAG IMAGE ID CREATED SIZE
n2n latest f0628fbbb0e6 3 hours ago 93.4MB
CONTAINER ID NAME CPU % MEM USAGE / LIMIT MEM % NET I/O
b6a1d36c60b2 n2n 0.00% 1.77MiB / 1.952GiB 0.09% 40.2kB / 39.7kB

From the two tables above, the memory and CPU usage is very small, don't have to worry about resource usage.