wireguard-instant.sh 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/bin/bash
  2. ### References
  3. # https://wiki.archlinux.org/title/WireGuard
  4. ### Installation
  5. # Install wireguard tools and QR-Code generator
  6. apt-get install --yes wireguard qrencode
  7. ### Variables
  8. network='192.168.130.1/24'
  9. confdir='/etc/wireguard'
  10. ### Create key material
  11. cd $confdir
  12. wgkeypair() { wg genkey | (umask 0077 && tee $1.key) | wg pubkey > $1.pub; }
  13. wgpsk() { wg genpsk | (umask 0077 && cat > $1.psk) }
  14. wgkeypair host
  15. peers="dspx4 zino2"
  16. for peer in $peers; do wgkeypair $peer && wgpsk $peer; done
  17. ### Configuration
  18. # Setup wireguard network interface
  19. ip link add dev wg0 type wireguard
  20. ip addr add $network dev wg0
  21. wg set wg0 listen-port 51871 private-key $confdir/host.key
  22. wgsetpeer() {
  23. peer=$1
  24. ip=$2
  25. wg set wg0 \
  26. peer `cat $confdir/$peer.pub` \
  27. preshared-key $confdir/$peer.psk \
  28. allowed-ips $ip
  29. ip link set wg0 up
  30. cat >$confdir/$peer.conf <<EOL
  31. # Client config for $peer
  32. [Interface]
  33. PrivateKey = `cat $confdir/$peer.key`
  34. Address = $ip
  35. DNS = 8.8.8.8
  36. [Peer]
  37. PublicKey = `cat $confdir/host.pub`
  38. AllowedIPs = 0.0.0.0/0
  39. Endpoint = blue.s-up.net:51871
  40. PersistentKeepalive = 15
  41. PresharedKey = `cat $confdir/$peer.psk`
  42. EOL
  43. qrencode -t ANSIUTF8 -r $confdir/$peer.conf
  44. }
  45. wgsetpeer dspx4 192.168.130.2
  46. # https://www.cyberciti.biz/faq/how-to-set-up-wireguard-firewall-rules-in-linux/
  47. iptables -t nat -I POSTROUTING 1 -s 192.168.130.0/24 -o ens3 -j MASQUERADE
  48. iptables -I INPUT 1 -i wg0 -j ACCEPT
  49. iptables -I FORWARD 1 -i ens3 -o wg0 -j ACCEPT
  50. iptables -I FORWARD 1 -i wg0 -o ens3 -j ACCEPT
  51. iptables -I INPUT 1 -i eth0 -p udp --dport 51871 -j ACCEPT
  52. sysctl -w net.ipv4.ip_forward=1