wg-persistent.sh 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. #!/bin/bash
  2. # Purpose: Make the running config from wg-instant.sh persistent
  3. ### References
  4. # https://wiki.archlinux.org/title/WireGuard
  5. ### Variables
  6. server_ip='192.168.130.1/24'
  7. network='192.168.130.0/24'
  8. confdir='/etc/wireguard'
  9. interface='ens3' # $(ip link | grep -o -E "ens[0-9]")
  10. # Safe current configuration and
  11. # - remove the endpoint IP (assumption: endpoint IPs are dynamic)
  12. # - add server IP
  13. wg showconf wg0 | sed \
  14. -e "/^Endpoint/d" \
  15. -e "/^\[Interface]$/a Address = $server_ip" \
  16. > $confdir/wg0.conf
  17. PostUp = iptables -t nat -I POSTROUTING 1 -s $network -o $interface -j MASQUERADE; iptables -I INPUT 1 -i %i -j ACCEPT; iptables -I FORWARD 1 -i $interface -o %i -j ACCEPT; iptables -I FORWARD 1 -i %i -o $interface -j ACCEPT; iptables -I INPUT 1 -i $interface -p udp --dport 51871 -j ACCEPT
  18. PostDown = iptables -t nat -D POSTROUTING -s $network -o $interface -j MASQUERADE; iptables -D INPUT -i %i -j ACCEPT; iptables -D FORWARD -i $interface -o %i -j ACCEPT; iptables -D FORWARD -i %i -o $interface -j ACCEPT; iptables -D INPUT -i $interface -p udp --dport 51871 -j ACCEPT
  19. ## Rules in several lines for better readability
  20. # wg-quick expands %i to the wireguard interface, here wg0
  21. iptables -t nat -I POSTROUTING 1 -s $network -o $interface -j MASQUERADE;
  22. iptables -I INPUT 1 -i %i -j ACCEPT;
  23. iptables -I FORWARD 1 -i $interface -o %i -j ACCEPT;
  24. iptables -I FORWARD 1 -i %i -o $interface -j ACCEPT;
  25. iptables -I INPUT 1 -i $interface -p udp --dport 51871 -j ACCEPT
  26. iptables -t nat -D POSTROUTING -s $network -o $interface -j MASQUERADE;
  27. iptables -D INPUT -i %i -j ACCEPT;
  28. iptables -D FORWARD -i $interface -o %i -j ACCEPT;
  29. iptables -D FORWARD -i %i -o $interface -j ACCEPT;
  30. iptables -D INPUT -i $interface -p udp --dport 51871 -j ACCEPT