From fdd12f3880fe5d4dac1366b9252499881f66c560 Mon Sep 17 00:00:00 2001 From: kzolotarev95 Date: Mon, 27 Jul 2026 01:15:45 +0400 Subject: [PATCH] Restart router tunnel after WAN reconnect --- files/etc/hotplug.d/iface/99-owrt-remote | 57 ++++++++++++++++++++++++ install.sh | 1 + 2 files changed, 58 insertions(+) create mode 100755 files/etc/hotplug.d/iface/99-owrt-remote diff --git a/files/etc/hotplug.d/iface/99-owrt-remote b/files/etc/hotplug.d/iface/99-owrt-remote new file mode 100755 index 0000000..611e03d --- /dev/null +++ b/files/etc/hotplug.d/iface/99-owrt-remote @@ -0,0 +1,57 @@ +#!/bin/sh + +PATH="/usr/sbin:/usr/bin:/sbin:/bin" +TAG="owrt-remote" +STATE_DIR="${TMPDIR:-/tmp}/owrt-remote-hotplug" +STAMP_FILE="$STATE_DIR/wan.stamp" + +[ "$ACTION" = "ifup" ] || [ "$ACTION" = "ifupdate" ] || [ "$ACTION" = "ifrestart" ] || exit 0 + +case "$(uci -q get owrtremote.main.enabled 2>/dev/null || echo 0)" in + 1|yes|true|on|enabled) ;; + *) exit 0 ;; +esac + +log() { + logger -t "$TAG" "$*" 2>/dev/null || true +} + +proto="" +if [ -n "$INTERFACE" ] && command -v ubus >/dev/null 2>&1; then + proto="$( + ubus -S call "network.interface.$INTERFACE" status 2>/dev/null | sed -n 's/.*"proto"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1 + )" +fi + +case "$INTERFACE:$DEVICE:$proto" in + wan:*:*|wan6:*:*|*:pppoe*:*|*:ppp*:*|*:*:pppoe|*:*:ppp) ;; + *) exit 0 ;; +esac + +network_ready() { + if command -v ip >/dev/null 2>&1 && ip route get 1.1.1.1 >/dev/null 2>&1; then + return 0 + fi + route -n 2>/dev/null | awk '$4 ~ /G/ { found = 1 } END { exit found ? 0 : 1 }' +} + +mkdir -p "$STATE_DIR" +stamp="$(date +%s 2>/dev/null || echo 0)-$$-${ACTION:-event}-${INTERFACE:-iface}-${DEVICE:-dev}" +printf '%s\n' "$stamp" >"$STAMP_FILE" + +( + i=0 + while [ "$i" -lt 30 ]; do + [ "$(cat "$STAMP_FILE" 2>/dev/null)" = "$stamp" ] || exit 0 + network_ready && break + i=$((i + 1)) + sleep 1 + done + + [ "$(cat "$STAMP_FILE" 2>/dev/null)" = "$stamp" ] || exit 0 + sleep 3 + log "WAN event $ACTION on ${INTERFACE:-unknown}/${DEVICE:-unknown}, restarting reverse tunnel" + /etc/init.d/owrt-remote restart >/dev/null 2>&1 || log "restart failed" +) & + +exit 0 diff --git a/install.sh b/install.sh index e1addac..a0e5217 100644 --- a/install.sh +++ b/install.sh @@ -145,6 +145,7 @@ install_xray_runtime() { install_file "usr/sbin/owrt-remote" 0755 install_file "etc/init.d/owrt-remote" 0755 +install_file "etc/hotplug.d/iface/99-owrt-remote" 0755 install_config install_file "www/cgi-bin/owrt-remote" 0755 install_file "usr/share/luci/menu.d/luci-app-owrt-remote.json" 0644