You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
2.1 KiB

  1. #!/bin/bash
  2. # TODO : Render this script POSIX compliant
  3. # This script can be used directly from bash after defining the IP address for the node with :
  4. # IP=x bash <(curl -s https://gitlab.altinea.fr/altinea/install-scripts/raw/branch/master/wireguard/deploy-wg-clients.sh)
  5. if ! [[ $IP =~ ^[0-9]{1,3}$ ]] ; then
  6. echo "error: Please set IP variable (with IP=1-254. See https://ipam.as41405.net" >&2; exit 1
  7. fi
  8. if ! modprobe -q wireguard ; then
  9. echo "Wireguard support missing. You should probably play with kernel headers and wireguard-dkms package"
  10. exit 1
  11. fi
  12. # Create keys subdir, generate private and derive public key
  13. mkdir -p /etc/wireguard/keys
  14. umask 077 && wg genkey > /etc/wireguard/keys/private.key && wg pubkey < /etc/wireguard/keys/private.key > /etc/wireguard/keys/public.key && umask 0022
  15. PRESHAREDKEY=`wg genpsk`
  16. # Calculate IPv4 and IPv6 address
  17. IP4="10.17.25.$IP"
  18. IP6="fd42:42:42:25"`printf '%.2x\n' $IP`"::"
  19. # Create config file for wireguard interface
  20. echo "[Interface]
  21. Address = $IP4/32
  22. Address = $IP6/64
  23. SaveConfig = false
  24. PostUp = wg set %i private-key /etc/wireguard/keys/private.key
  25. PostUp = ping -c1 10.17.25.1
  26. [Peer]
  27. PublicKey = iu3I09FtiVDIOuiU83JvpfJkg4yiCxolqcFsXbz5Ixc=
  28. PresharedKey = $PRESHAREDKEY
  29. AllowedIPs = 10.17.24.0/22, fd42:42:42::/48 # All Wireguard address space
  30. AllowedIPs = 172.16.5.0/24, fc00:db8:f00:bebe::/64 # OpenVPN Admin tunnel
  31. Endpoint = vpn.altinea.fr:58212
  32. PersistentKeepalive = 25" > /etc/wireguard/vpnaltinea.conf
  33. # Display the public key to add it on the wireguard concentrator
  34. echo "Now you should read https://wiki.altinea.fr/doku.php/wireguard#cote_concentrateur_wireguard"
  35. echo ""
  36. echo "[Peer]"
  37. echo "# "`hostname -f`
  38. echo -n "PublicKey = "
  39. cat /etc/wireguard/keys/public.key
  40. echo "PresharedKey = $PRESHAREDKEY"
  41. echo "AllowedIPs = $IP4/32, $IP6/64"
  42. read -n1 -r -p "Press space only AFTER configuration is done ..."
  43. # Enable and start interface (systemctl needed)
  44. systemctl enable wg-quick@vpnaltinea.service && systemctl daemon-reload && systemctl start wg-quick@vpnaltinea
  45. # Run a ping to make the interface usable
  46. ping -c1 10.17.25.1
  47. exit 0;