Forwarding TCP Requests over VPN: A Simple Guide for Server, Laptop, and Raspberry Pi
In this article, we will discuss how to forward TCP requests over a VPN (Virtual Private Network) using three devices: a server, a laptop connected to Network 1, and a Raspberry Pi connected to Network 2. We will also cover setting up an open VPN server on Network 1 and configuring port forwarding.
Prerequisites
- A server with Ubuntu or Debian installed
- A laptop with any Linux distribution installed
- A Raspberry Pi with Raspberry Pi OS installed
- Two separate networks, Network 1 and Network 2
- An open VPN server installed on Network 1
Setting up an Open VPN Server on Network 1
To set up an open VPN server on Network 1, follow these steps:
- Install the open VPN package on the server using the following command:
sudo apt-get install openvpn- Generate a key and certificate using the following command:
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -days 3650 -nodes- Create a directory for the open VPN server and move the key and certificate to that directory:
sudo mkdir /etc/openvpn/serversudo mv key.pem cert.pem /etc/openvpn/server- Create a configuration file for the open VPN server using the following command:
sudo nano /etc/openvpn/server/server.confAdd the following configuration to the file:
proto udpport 1194dev tunca ca.crtcert server.crtkey server.keydh dh.pemserver 10.8.0.0 255.255.255.0ifconfig-pool-persist ipp.txtpush "redirect-gateway def1 bypass-dhcp"push "dhcp-option DNS 8.8.8.8"push "dhcp-option DNS 8.8.4.4"keepalive 10 120cipher AES-256-CBCcomp-lzouser nobodygroup nogrouppersist-keypersist-tunstatus openvpn-status.logverb 3- Generate a Diffie-Hellman key using the following command:
openssl dhparam -out dh.pem 4096- Start the open VPN server using the following command:
sudo systemctl start openvpn@server- Enable the open VPN server to start on boot using the following command:
sudo systemctl enable openvpn@serverConfiguring Port Forwarding on Network 1
To configure port forwarding on Network 1, follow these steps:
- Log in to the router for Network 1.
- Find the port forwarding section and add a new rule.
- Set the protocol to TCP.
- Set the external port to 8080.
- Set the internal IP address to the IP address of the server.
- Set the internal port to 1194.
- Save the changes.
Connecting the Laptop to Network 1
To connect the laptop to Network 1, follow these steps:
- Connect to Network 1 using Wi-Fi or Ethernet.
- Download the open VPN client configuration file from the server.
- Import the configuration file into the open VPN client.
- Connect to the VPN using the open VPN client.
Connecting the Raspberry Pi to Network 2
To connect the Raspberry Pi to Network 2, follow these steps:
- Connect to Network 2 using Wi-Fi or Ethernet.
- Download the open VPN client configuration file from the server.
- Import the configuration file into the open VPN client.
- Connect to the VPN using the open VPN client.
Forwarding TCP Requests over the VPN
To forward TCP requests over the VPN, follow these steps:
- Install the iptables package on the server using the following command:
sudo apt-get install iptables- Allow traffic to pass through the VPN using the following command:
sudo iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE- Forward traffic from the laptop to the Raspberry Pi using the following command:
sudo iptables -t nat -A PREROUTING -i wlan0 -p tcp --dport 80 -j DNAT --to-destination 10.8.0.2:80In this article, we discussed how to forward TCP requests over a VPN using three devices: a server, a laptop connected to Network 1, and a Raspberry Pi connected to Network 2. We also covered setting up an open VPN server on Network 1 and configuring port forwarding. By following these steps, you can securely forward traffic between two networks using a VPN.