Restarting interface on schedule

I use my openwrt router to route all traffic through a airvpn vpn.

I use wireguard. I chose Europe so that it connects to a server in europe where there isn't too much traffic.

However, after a couple of hours, depending on the time of the day, the chosen vpn server might have become busy and slow.

Today my connection dropped from 400mbit to 3mbit. I restarted the vpn interface, thus a new vpn server was chosen I guess, and was back at 400mbit.

My questions:

  1. how can i automate/schedule the reconnect of the vpn with openwrt? Maybe on the condition that the speed dropped.

  2. might there be a different reason for the slow connection and it becoming faster after the restart of the vpn interface in openwrt?

  3. is there a better solution than restarting the interface?

Cheers

Scheduling is simple with "cron" if you just restart it at defined times, regardless of the current speed (& current network sessions/traffic)

But if you want speed measurement, you need to craft a script that also triggers speed measurement (with a suitable tool), and then depending on the parsed result, decides to restart the interface.

  1. hard to detect if speed drops or the connection is legitimately idle.

So I used this script to measure the speed and if necessary, restart the interface:

#!/bin/sh

# Configurations
INTERFACE="wg0"  # WireGuard interface name
PRIMARY_TEST_URL="http://ipv4.download.thinkbroadband.com/100MB.zip"  # Primary Test URL
BACKUP_TEST_URL="http://proof.ovh.net/files/100Mb.dat"  # Backup Test URL
THRESHOLD=150  # Speed threshold in Mbps

# Function to measure download speed
measure_speed() {
    URL=$1
    SPEED=$(curl -o /dev/null --max-time 10 --silent --write-out '%{speed_download}' $URL)
    SPEED_MBPS=$(echo "$SPEED / 1000000 * 8" | bc)
    echo "$SPEED_MBPS"
}

# Measure speed from the primary URL
SPEED_MBPS=$(measure_speed $PRIMARY_TEST_URL)

# If the speed is 0 (connection failure), try the backup URL
if [ "$(echo "$SPEED_MBPS == 0" | bc)" -eq 1 ]; then
    logger "Primary URL failed, trying backup URL"
    SPEED_MBPS=$(measure_speed $BACKUP_TEST_URL)
fi

# Log the measured speed
logger "VPN Speed: ${SPEED_MBPS} Mbps"

# Check if speed is below the threshold
if [ "$(echo "$SPEED_MBPS < $THRESHOLD" | bc)" -eq 1 ]; then
    logger "VPN speed is below threshold ($SPEED_MBPS Mbps), restarting WireGuard"
    ifdown $INTERFACE
    sleep 5
    ifup $INTERFACE
else
    logger "VPN speed is acceptable ($SPEED_MBPS Mbps)"
fi

The problem is: When I run the script on the openwrt router, I always get slow speeds like 30-50mbit. When I connect my Mac via Wifi to the router, and run the script on my Mac, I get much faster speeds (300mbit). Why? What can we change so that the speedtest properly works on the router?

apply this patch already in snapshots ie edit 2 lines in ruleset,uc

CPU usage while running curl is at around 10%. So it's not a CPU usage problem.

It is trivial edit reducing per-packet processing. It will not magically speed up if you cast spells at it.

I think I found a better way then restarting the interface, which leads to problems.

With wireguard there comes this script: wireguard_watchdog:

. /lib/functions.sh

check_peer_activity() {
  local cfg=$1
  local iface=$2
  local disabled
  local public_key
  local endpoint_host
  local endpoint_port
  local persistent_keepalive
  local last_handshake
  local idle_seconds

  config_get_bool disabled "${cfg}" "disabled" 0
  config_get public_key "${cfg}" "public_key"
  config_get endpoint_host "${cfg}" "endpoint_host"
  config_get endpoint_port "${cfg}" "endpoint_port"

  if [ "${disabled}" -eq 1 ]; then
    # skip disabled peers
    return 0
  fi

  persistent_keepalive=$(wg show ${iface} persistent-keepalive | grep ${public_key} | awk '{print $2}')

  # only process peers with endpoints and keepalive set
  [ -z ${endpoint_host} ] && return 0;
  [ -z ${persistent_keepalive} -o ${persistent_keepalive} = "off" ] && return 0;

  # 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")
  [ -n "${IPV4}" -o -n "${IPV6}" ] && return 0;

  # 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=$(ubus -S call network.interface dump | jsonfilter -e '@.interface[@.up=true]' | jsonfilter -a -e '@[@.proto="wireguard"].interface' | tr "\n" " ")

# check every peer in every active wireguard interface
config_load network
for iface in $wg_ifaces; do
  config_foreach check_peer_activity "wireguard_${iface}" "${iface}"
done

But it only re-resolves when the connection is lost entirely. I'm thinking about how I could rewrite it so that it always tries to re-resolve hostname.

So here goes my solution. I will report later if it works. I create a custom script /usr/bin/wireguard_watchdog_custom:

#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
#
# Copyright (C) 2018 Aleksandr V. Piskunov <aleksandr.v.piskunov@gmail.com>.
# Copyright (C) 2015-2018 Jason A. Donenfeld <Jason@zx2c4.com>. 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 15th minute:
# echo '*/15 * * * * /usr/bin/wireguard_watchdog_custom' >> /etc/crontabs/root

. /lib/functions.sh

check_peer_activity() {
  local cfg=$1
  local iface=$2
  local disabled
  local public_key
  local endpoint_host
  local endpoint_port
  local persistent_keepalive
  local last_handshake
  local idle_seconds

  config_get_bool disabled "${cfg}" "disabled" 0
  config_get public_key "${cfg}" "public_key"
  config_get endpoint_host "${cfg}" "endpoint_host"
  config_get endpoint_port "${cfg}" "endpoint_port"

  if [ "${disabled}" -eq 1 ]; then
    # skip disabled peers
    return 0
  fi

  persistent_keepalive=$(wg show ${iface} persistent-keepalive | grep ${public_key} | awk '{print $2}')

  # only process peers with endpoints and keepalive set
  [ -z ${endpoint_host} ] && return 0;
  [ -z ${persistent_keepalive} -o ${persistent_keepalive} = "off" ] && return 0;

  # 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")
  [ -n "${IPV4}" -o -n "${IPV6}" ] && return 0;

  # re-resolve endpoint hostname if not responding for too long
  logger -t "wireguard_monitor" "trying to re-resolve hostname"
  echo "wireguard_monitor trying to re-resolve hostname: wg set ${iface} peer ${public_key} endpoint ${endpoint_host}:${endpoint_port}"
  wg set ${iface} peer ${public_key} endpoint "${endpoint_host}:${endpoint_port}"
}

# query ubus for all active wireguard interfaces
wg_ifaces=$(ubus -S call network.interface dump | jsonfilter -e '@.interface[@.up=true]' | jsonfilter -a -e '@[@.proto="wireguard"].interface' | tr "\n" " ")

# check every peer in every active wireguard interface
config_load network
for iface in $wg_ifaces; do
  config_foreach check_peer_activity "wireguard_${iface}" "${iface}"
done

and I add it as a cronjob to check every 15mins (also add the normal script here, since it makes sense):

* * * * * /usr/bin/wireguard_watchdog
*/15 * * * * /usr/bin/wireguard_watchdog_custom

don't forget to make the script executable: chmod +x wireguard_watchdog_custom

Doesn't work.

After a while DNS stops working. Can't resolve any domain names. Not sure why that could be?