I want my OpenVPN server to listen on multiple ports, e.g. port 3000 to 4000, for some reason. I searched for a solution and the answer I got was basically "just run 1001 instances".
It's not practical obviously. But from my understanding of Internet Protocol, it should be doable with IP firewalls such as iptables. After consulting someone know well about server maintenance, I was able to address the problem with a single command:
# iptables -t nat -A PREROUTING -d <server-ip> -i <wan-interface> -p udp -m udp --dport 30000:31000 -j DNAT --to-destination <server-ip>:<the-port-openvpn-is-listening-on>
The command assumes your OpenVPN server accepts UDP packets.
server-ip is your server's public IP address, which OpenVPN server instance should be listening on.
wan-interface is the interface which has
30000:31000 are additional ports you want your OpenVPN server to listen on.
the-port-openvpn-is-listening-on is the port which your OpenVPN server instance is actually listening on.
How does it work?
A UDP/IP packet conveys the following information:
- Source Address
- Source Port
- Destination Address
- Destination Port
Let's say, your server's IP address is 22.214.171.124 and OpenVPN is listening on port 1194. With the iptables rule above -- which is a Destination Address Translation rule -- applied, all UDP/IP packets with Destination Address of 126.96.36.199 and Destination Port 30000 to 31000 will be modified so that it has the Destination Address 188.8.131.52 but the Destination Port 1194. Then OpenVPN server process can receive the packets.
What about the returning packets? Let's assume your client -- no matter how it's situated behind NAT gateways -- eventually have the public IP address 184.108.40.206 and use port 56677 to send UDP packets, whose parameters are:
- Source Address: 220.127.116.11
- Source Port: 56677
- Destination Address: 18.104.22.168
- Destination Port: 30234
The UDP packets are then forwarded to 22.214.171.124:1194 by server's Linux kernel according to the iptables rule. When OpenVPN replies UDP/IP packets, their parameters are:
- Source Address: 126.96.36.199
- Source Port: 1194
- Destination Address: 188.8.131.52
- Destination Port: 56677
If returning packets are unchanged, they would be discarded by the client or cause errors, because they are expected to have the source port of 30234, the same as the destination port of incoming packets. However, the server's Linux kernel will bear in mind that 184.108.40.206:56677 was sending UDP packets -- whose destination is changed by the kernel -- to server's port 30234. So the returning packets' source port will be changed to 30234 to "revert" changes made to incoming packets.
Why the rule is in PREROUTING chain? Well, it's beyond my knowledge at the time of writing. But I believe the explanation above is enough.