S04wireguard 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides:
  4. # Short-Description: Wireguard support
  5. # Description: Enable Wireguard support
  6. ### END INIT INFO
  7. . /opt/wz_mini/wz_mini.conf
  8. . /opt/wz_mini/etc/rc.common
  9. case "$1" in
  10. start)
  11. echo "#####$(basename "$0")#####"
  12. if [[ "$ENABLE_WIREGUARD" == "true" ]]; then
  13. if [[ "$WIREGUARD_IPV4" != "" ]]; then
  14. if [ -d /opt/wz_mini/etc/wireguard ]; then
  15. echo "Wireguard dir exists"
  16. else
  17. mkdir -p /opt/wz_mini/etc/wireguard
  18. fi
  19. if [ ! -f /opt/wz_mini/etc/wireguard/privatekey ]; then
  20. (umask 277 && /opt/wz_mini/bin/wg genkey | /opt/wz_mini/bin/busybox tee /opt/wz_mini/etc/wireguard/privatekey | /opt/wz_mini/bin/wg pubkey > /opt/wz_mini/etc/wireguard/publickey)
  21. fi
  22. /opt/wz_mini/bin/busybox ip link add dev wg0 type wireguard
  23. /opt/wz_mini/bin/busybox ip address add dev wg0 "$WIREGUARD_IPV4"
  24. /opt/wz_mini/bin/wg set wg0 private-key /opt/wz_mini/etc/wireguard/privatekey
  25. /opt/wz_mini/bin/busybox ip link set wg0 up
  26. fi
  27. if [[ "$WIREGUARD_PEER_PUBLIC_KEY" != "" ]] && [[ "$WIREGUARD_PEER_ALLOWED_IPS" != "" ]] && [[ "$WIREGUARD_PEER_ENDPOINT" != "" ]] && [[ "$WIREGUARD_PEER_KEEP_ALIVE" != "" ]]; then
  28. /opt/wz_mini/bin/wg set wg0 peer "$WIREGUARD_PEER_PUBLIC_KEY" allowed-ips "$WIREGUARD_PEER_ALLOWED_IPS" endpoint "$WIREGUARD_PEER_ENDPOINT" persistent-keepalive "$WIREGUARD_PEER_KEEP_ALIVE"
  29. /opt/wz_mini/bin/busybox ip route add "$WIREGUARD_PEER_ALLOWED_IPS" dev wg0
  30. fi
  31. else
  32. echo "Wireguard disabled"
  33. fi
  34. ;;
  35. *)
  36. echo "Usage: $0 {start}"
  37. exit 1
  38. ;;
  39. esac