auto-apply MTU once on first WG handshake (probe + rewrite + restart)

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
d4nn13andClaude Opus 4.7 committed 2026-04-23 19:30:28 +02:00
1 parent 7406096b79
commit b1085fd23c
3 files changed
+71 -1

No files matched your search

+2 -1
View File
@@ -56,13 +56,14 @@ fi
# --- files ------------------------------------------------------------------ # --- files ------------------------------------------------------------------
install -d -m 0755 /etc/wirecon /etc/wireguard /etc/conf.d \ install -d -m 0755 /etc/wirecon /etc/wireguard /etc/conf.d \
/etc/init.d /usr/local/bin /var/log /etc/init.d /usr/local/bin /var/log /var/lib/wirecon
log "deploying scripts" log "deploying scripts"
install -m 0755 "$SRC/usr/local/bin/wirecon-watchdog.sh" /usr/local/bin/ install -m 0755 "$SRC/usr/local/bin/wirecon-watchdog.sh" /usr/local/bin/
install -m 0755 "$SRC/usr/local/bin/wirecon-watchdog-loop.sh" /usr/local/bin/ install -m 0755 "$SRC/usr/local/bin/wirecon-watchdog-loop.sh" /usr/local/bin/
install -m 0755 "$SRC/usr/local/bin/wirecon-monitor.sh" /usr/local/bin/ install -m 0755 "$SRC/usr/local/bin/wirecon-monitor.sh" /usr/local/bin/
install -m 0755 "$SRC/usr/local/bin/wirecon-mtu-probe.sh" /usr/local/bin/ install -m 0755 "$SRC/usr/local/bin/wirecon-mtu-probe.sh" /usr/local/bin/
install -m 0755 "$SRC/usr/local/bin/wirecon-mtu-apply.sh" /usr/local/bin/
log "deploying OpenRC init scripts" log "deploying OpenRC init scripts"
install -m 0755 "$SRC/etc/init.d/wirecon-watchdog" /etc/init.d/ install -m 0755 "$SRC/etc/init.d/wirecon-watchdog" /etc/init.d/
+62
View File
@@ -0,0 +1,62 @@
#!/bin/bash
# wirecon-mtu-apply.sh — one-shot: run wirecon-mtu-probe.sh against the current
# WG peer endpoint, rewrite `MTU =` in /etc/wireguard/wg0.conf if the probe
# recommends something different, restart wg-quick, drop a marker so future
# watchdog ticks skip us.
#
# Invoked detached from wirecon-watchdog.sh on the first tick where a handshake
# has happened and /var/lib/wirecon/mtu-probed.flag is absent. Marker stays
# absent on probe failure so the next handshake retries.
set -uo pipefail
WG_CONF="${WG_CONF:-/etc/wireguard/wg0.conf}"
IFACE="${WG_INTERFACE:-wg0}"
MARKER="${WIRECON_MTU_MARKER:-/var/lib/wirecon/mtu-probed.flag}"
PROBE="${WIRECON_MTU_PROBE:-/usr/local/bin/wirecon-mtu-probe.sh}"
log() {
local level="$1"; shift
printf 'level=%s ts=%s %s\n' "$level" "$(date -u +%FT%TZ)" "$*"
}
mkdir -p "$(dirname "$MARKER")"
if [[ -e "$MARKER" ]]; then
log info "event=mtu_skip reason=marker_present"
exit 0
fi
endpoint=$(awk -F'= *' '/^Endpoint/ {gsub(/:[0-9]+$/,"",$2); print $2; exit}' "$WG_CONF" 2>/dev/null || true)
if [[ -z "$endpoint" || "$endpoint" == REPLACE_WITH_* ]]; then
log warn "event=mtu_skip reason=no_real_endpoint endpoint=\"${endpoint:-<none>}\""
exit 0
fi
current_mtu=$(awk -F'= *' '/^MTU/ {print $2; exit}' "$WG_CONF" 2>/dev/null | tr -d '[:space:]' || true)
log info "event=mtu_probe_start endpoint=$endpoint current_mtu=${current_mtu:-?}"
probe_out=$("$PROBE" "$endpoint" 2>/dev/null || true)
new_mtu=$(printf '%s\n' "$probe_out" | awk -F': *' '/^recommended WG MTU/ {print $2; exit}' | tr -d '[:space:]')
if [[ -z "$new_mtu" ]]; then
log error "event=mtu_probe_failed endpoint=$endpoint"
exit 1
fi
if [[ "$new_mtu" == "$current_mtu" ]]; then
log info "event=mtu_noop mtu=$new_mtu"
: > "$MARKER"
exit 0
fi
log info "event=mtu_apply old=${current_mtu:-?} new=$new_mtu"
sed -i "s/^MTU = .*/MTU = ${new_mtu}/" "$WG_CONF"
if rc-service "wg-quick.$IFACE" restart >/dev/null 2>&1; then
log info "event=mtu_applied old=${current_mtu:-?} new=$new_mtu"
: > "$MARKER"
else
log error "event=mtu_restart_failed new=$new_mtu"
exit 1
fi
+7
View File
@@ -85,6 +85,13 @@ age=$(handshake_age)
lost=$(in_tunnel_ping) lost=$(in_tunnel_ping)
streak=$(read_streak) streak=$(read_streak)
# One-shot MTU probe on the first-ever successful handshake. The apply
# helper short-circuits on its own marker; we only skip the fork here to
# avoid spawning a subshell every tick once the marker exists.
if [[ "$age" -ge 0 && ! -e /var/lib/wirecon/mtu-probed.flag ]]; then
( /usr/local/bin/wirecon-mtu-apply.sh >>/var/log/wirecon-mtu.log 2>&1 ) &
fi
reason="" reason=""
if [[ "$age" -lt 0 ]]; then if [[ "$age" -lt 0 ]]; then
reason="no handshake ever" reason="no handshake ever"