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.

51 lines
1.2 KiB

  1. #!/bin/sh
  2. next_port () {
  3. local port_start=$1
  4. local port_end=$2
  5. ports=$(wg show all listen-port | awk '{print $2}')
  6. for i in $(seq "$port_start" "$port_end"); do
  7. if ! echo "$ports" | grep -q "$i"; then
  8. echo "$i"
  9. return
  10. fi
  11. done
  12. }
  13. cleanup_wginterfaces() {
  14. check_wg_neighbors
  15. }
  16. delete_wg_interface() {
  17. ip link del dev "$1"
  18. [ -f "/tmp/run/wgserver/$1.key" ] && rm "/tmp/run/wgserver/$1.key"
  19. [ -f "/tmp/run/wgserver/$1.pub" ] && rm "/tmp/run/wgserver/$1.pub"
  20. }
  21. check_wg_neighbors() {
  22. wg_interfaces=$(ip link | grep wg | awk '{print $2}' | sed 's/://')
  23. for phy in $wg_interfaces; do
  24. linklocal=$(ip -6 addr list dev "$phy" | grep "scope link" | awk '{print $2}' | sed 's/\/64//') 2>/dev/null
  25. ips=$(ping ff02::1%"$phy" -w5 -W5 -c10 | awk '/from/{print($4)}' | sed 's/.$//') 2>/dev/null
  26. delete=1
  27. for ip in $ips; do
  28. if [ "$ip" != "$linklocal" ] && [ "$(owipcalc $ip linklocal)" -eq 1 ]; then
  29. delete=0
  30. break
  31. fi
  32. done
  33. if [ $delete -eq 1 ]; then
  34. delete_wg_interface "$phy"
  35. fi
  36. done
  37. }
  38. case $1 in
  39. next_port|\
  40. cleanup_wginterfaces)
  41. "$@"
  42. exit
  43. ;;
  44. esac