# python3 /home/me/postup_predown.py /etc/wireguard/*.conf
sudo bash -c 'python3 /home/me/postup_predown.py /etc/wireguard/*.conf'
-
-
Save vaultah/da4c294df18f1127fece470deecc018d to your computer and use it in GitHub Desktop.
Add PostUp / PreDown rules to Wireguard configs to restrict traffic to the tunnel
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
import configparser, sys | |
for path in sys.argv[1:]: | |
conf = configparser.RawConfigParser() | |
conf.optionxform = str | |
conf.read(path) | |
conf['Interface']['PostUp'] = 'iptables -I OUTPUT ! -o %i -m mark ! --mark $(wg show %i fwmark) -m addrtype ! --dst-type LOCAL -j REJECT && ip6tables -I OUTPUT ! -o %i -m mark ! --mark $(wg show %i fwmark) -m addrtype ! --dst-type LOCAL -j REJECT' | |
conf['Interface']['PreDown']= 'iptables -D OUTPUT ! -o %i -m mark ! --mark $(wg show %i fwmark) -m addrtype ! --dst-type LOCAL -j REJECT && ip6tables -D OUTPUT ! -o %i -m mark ! --mark $(wg show %i fwmark) -m addrtype ! --dst-type LOCAL -j REJECT' | |
with open(path, 'w') as out: | |
conf.write(out) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment