wireguard-instant.sh 1.7 KB

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