如 12#所说,改过一版,基本只替换了 luci 读配置那些东西。
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (C) 2018 Aleksandr V. Piskunov <
[email protected]>.
# Copyright (C) 2015-2018 Jason A. Donenfeld <
[email protected]>. All Rights Reserved.
#
# This watchdog script tries to re-resolve hostnames for inactive WireGuard peers.
# Use it for peers with a frequently changing dynamic IP.
# persistent_keepalive must be set, recommended value is 25 seconds.
#
# Run this script from cron every minute:
# echo '* * * * * /usr/bin/wireguard_watchdog' >> /etc/crontabs/root
check_peer_activity() {
local iface=$1
local public_key=$2
local endpoint_host=$3
local endpoint_port=$4
local persistent_keepalive
local last_handshake
local idle_seconds
persistent_keepalive=$(wg show ${iface} persistent-keepalive | grep ${public_key} | awk '{print $2}')
echo "checking $1 $2 $3 $4..."
# only process peers with endpoints and keepalive set
echo 1
[ -z ${endpoint_host} ] && return 0;
echo 2
[ -z ${persistent_keepalive} -o ${persistent_keepalive} = "off" ] && return 0;
echo 3
# skip IP addresses
# check taken from packages/net/ddns-scripts/files/
dynamic_dns_functions.sh local IPV4_REGEX="[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}"
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,\}\)"
local IPV4=$(echo ${endpoint_host} | grep -m 1 -o "$IPV4_REGEX$") # do not detect ip in
0.0.0.0.example.com local IPV6=$(echo ${endpoint_host} | grep -m 1 -o "$IPV6_REGEX")
echo 4
[ -n "${IPV4}" -o -n "${IPV6}" ] && return 0;
echo 5
# re-resolve endpoint hostname if not responding for too long
last_handshake=$(wg show ${iface} latest-handshakes | grep ${public_key} | awk '{print $2}')
[ -z ${last_handshake} ] && return 0;
idle_seconds=$(($(date +%s)-${last_handshake}))
[ ${idle_seconds} -lt 150 ] && return 0;
logger -t "wireguard_monitor" "${iface} endpoint ${endpoint_host}:${endpoint_port} is not responding for ${idle_seconds} seconds, trying to re-resolve hostname"
wg set ${iface} peer ${public_key} endpoint "${endpoint_host}:${endpoint_port}"
}
# query ubus for all active wireguard interfaces
wg_ifaces=$(wg show interfaces | awk '{print $1}' | tr '\n' ' ')
# check every peer in every active wireguard interface
for iface in $wg_ifaces; do
config_file="/etc/wireguard/${iface}.conf"
# parsing wireguard config file
eval `cat $config_file | tr -d ' ' |awk -F '=' '
BEGIN {peer_index = 0;status = 0;}
# close previous peer section
status==1 && $1 ~ /^\[/ {status = 0;}
# open new peer section
status==0 && $1 == "[Peer]" {status = 1;peer_index++;}
# parse PublicKey in Peer section
status==1 && $1 == "PublicKey" {public_key = \$0;sub(/PublicKey=/, "", public_key);printf("public_key%d=%s\n", peer_index, public_key);}
# parse Endpoint in Peer section
status==1 && $1 == "Endpoint" {printf("endpoint%d=%s\n", peer_index, $2);}
END {printf("peer_count=%d\n",peer_index)}'`
for ((i = 1; i <= $peer_count; i++)); do
public_key_var="public_key$i"
endpoint_var="endpoint$i"
public_key="${!public_key_var}"
endpoint="${!endpoint_var}"
endpoint_host=`echo "${endpoint}"|awk -F '[:]' '{print $1}'`
endpoint_port=`echo "${endpoint}"|awk -F '[:]' '{print $2}'`
check_peer_activity "${iface}" "${public_key}" "${endpoint_host}" "${endpoint_port}"
done
done