Created
March 21, 2022 15:04
-
-
Save termermc/7652e41ad7bf31f30295c32ba0349a5f to your computer and use it in GitHub Desktop.
Port forwarding through Wireguard (script to be run on the server)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Usage: ./wireguard_port_forward.sh <client IP> <server IP> <port> | |
# Example: ./wireguard_port_forward.sh 10.0.0.2 10.0.0.1 8080 | |
# Script must be run as root | |
CLIENT=$1 | |
SERVER=$2 | |
PORT=$3 | |
iptables -A FORWARD -i eth0 -o wg0 -p tcp --syn --dport $PORT -m conntrack --ctstate NEW -j ACCEPT | |
iptables -A FORWARD -i eth0 -o wg0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT | |
iptables -A FORWARD -i wg0 -o eth0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT | |
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport "$PORT" -j DNAT --to-destination "$CLIENT" | |
iptables -t nat -A POSTROUTING -o wg0 -p tcp --dport "$PORT" -d "$CLIENT" -j SNAT --to-source "$SERVER" | |
netfilter-persistent save |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment