wireguard.sh 1.6 KB

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