1
0

wireguard_watchdog 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/bin/sh
  2. # SPDX-License-Identifier: GPL-2.0
  3. #
  4. # Copyright (C) 2018 Aleksandr V. Piskunov <aleksandr.v.piskunov@gmail.com>.
  5. # Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
  6. #
  7. # This watchdog script tries to re-resolve hostnames for inactive WireGuard peers.
  8. # Use it for peers with a frequently changing dynamic IP.
  9. # persistent_keepalive must be set, recommended value is 25 seconds.
  10. #
  11. # Run this script from cron every minute:
  12. # echo '* * * * * /usr/bin/wireguard_watchdog' >> /etc/crontabs/root
  13. . /lib/functions.sh
  14. check_peer_activity() {
  15. local cfg=$1
  16. local iface=$2
  17. local disabled
  18. local public_key
  19. local endpoint_host
  20. local endpoint_port
  21. local persistent_keepalive
  22. local last_handshake
  23. local idle_seconds
  24. config_get_bool disabled "${cfg}" "disabled" 0
  25. config_get public_key "${cfg}" "public_key"
  26. config_get endpoint_host "${cfg}" "endpoint_host"
  27. config_get endpoint_port "${cfg}" "endpoint_port"
  28. if [ "${disabled}" -eq 1 ]; then
  29. # skip disabled peers
  30. return 0
  31. fi
  32. persistent_keepalive=$(wg show ${iface} persistent-keepalive | grep ${public_key} | awk '{print $2}')
  33. # only process peers with endpoints and keepalive set
  34. [ -z ${endpoint_host} ] && return 0;
  35. [ -z ${persistent_keepalive} -o ${persistent_keepalive} = "off" ] && return 0;
  36. # skip IP addresses
  37. # check taken from packages/net/ddns-scripts/files/dynamic_dns_functions.sh
  38. local IPV4_REGEX="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
  39. local IPV6_REGEX="\(\([0-9A-Fa-f]\{1,4\}:\)\{1,\}\)\(\([0-9A-Fa-f]\{1,4\}\)\{0,1\}\)\(\(:[0-9A-Fa-f]\{1,4\}\)\{1,\}\)"
  40. local IPV4=$(echo ${endpoint_host} | grep -m 1 -o "$IPV4_REGEX$") # do not detect ip in 0.0.0.0.example.com
  41. local IPV6=$(echo ${endpoint_host} | grep -m 1 -o "$IPV6_REGEX")
  42. [ -n "${IPV4}" -o -n "${IPV6}" ] && return 0;
  43. # re-resolve endpoint hostname if not responding for too long
  44. last_handshake=$(wg show ${iface} latest-handshakes | grep ${public_key} | awk '{print $2}')
  45. [ -z ${last_handshake} ] && return 0;
  46. idle_seconds=$(($(date +%s)-${last_handshake}))
  47. [ ${idle_seconds} -lt 150 ] && return 0;
  48. logger -t "wireguard_monitor" "${iface} endpoint ${endpoint_host}:${endpoint_port} is not responding for ${idle_seconds} seconds, trying to re-resolve hostname"
  49. wg set ${iface} peer ${public_key} endpoint "${endpoint_host}:${endpoint_port}"
  50. }
  51. # query ubus for all active wireguard interfaces
  52. wg_ifaces=$(ubus -S call network.interface dump | jsonfilter -e '@.interface[@.up=true]' | jsonfilter -a -e '@[@.proto="wireguard"].interface' | tr "\n" " ")
  53. # check every peer in every active wireguard interface
  54. config_load network
  55. for iface in $wg_ifaces; do
  56. config_foreach check_peer_activity "wireguard_${iface}" "${iface}"
  57. done