Stabilize WAN reconnect tunnel recovery

This commit is contained in:
kzolotarev95 committed 2026-07-27 01:37:43 +04:00
1 parent fdd12f3880
commit c98322364d
2 files changed
+37 -6

No files matched your search

+29 -2
View File
@@ -4,6 +4,9 @@ PATH="/usr/sbin:/usr/bin:/sbin:/bin"
TAG="owrt-remote"
STATE_DIR="${TMPDIR:-/tmp}/owrt-remote-hotplug"
STAMP_FILE="$STATE_DIR/wan.stamp"
LAST_RESTART_FILE="$STATE_DIR/last-restart"
QUIET_SECONDS=18
RESTART_COOLDOWN=45
[ "$ACTION" = "ifup" ] || [ "$ACTION" = "ifupdate" ] || [ "$ACTION" = "ifrestart" ] || exit 0
@@ -35,12 +38,22 @@ network_ready() {
route -n 2>/dev/null | awk '$4 ~ /G/ { found = 1 } END { exit found ? 0 : 1 }'
}
safe_num() {
case "$1" in
''|*[!0-9]*) printf '%s' "$2" ;;
*) printf '%s' "$1" ;;
esac
}
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
sleep "$QUIET_SECONDS"
[ "$(cat "$STAMP_FILE" 2>/dev/null)" = "$stamp" ] || exit 0
while [ "$i" -lt 30 ]; do
[ "$(cat "$STAMP_FILE" 2>/dev/null)" = "$stamp" ] || exit 0
network_ready && break
@@ -49,9 +62,23 @@ printf '%s\n' "$stamp" >"$STAMP_FILE"
done
[ "$(cat "$STAMP_FILE" 2>/dev/null)" = "$stamp" ] || exit 0
sleep 3
sleep 2
[ "$(cat "$STAMP_FILE" 2>/dev/null)" = "$stamp" ] || exit 0
now="$(safe_num "$(date +%s 2>/dev/null || echo 0)" 0)"
last_restart="$(safe_num "$(cat "$LAST_RESTART_FILE" 2>/dev/null)" 0)"
if [ "$now" -gt 0 ] && [ $((now - last_restart)) -lt "$RESTART_COOLDOWN" ]; then
log "WAN event $ACTION on ${INTERFACE:-unknown}/${DEVICE:-unknown}, restart skipped by cooldown"
exit 0
fi
printf '%s\n' "$now" >"$LAST_RESTART_FILE"
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"
if /etc/init.d/owrt-remote restart >/dev/null 2>&1; then
/usr/sbin/owrt-remote heartbeat >/dev/null 2>&1 || true
else
log "restart failed"
fi
) &
exit 0
+8 -4
View File
@@ -404,7 +404,7 @@ service_enabled() {
}
render_client_config() {
local out stdout router_id vps_host vps_port uuid ssh_uuid encryption flow reverse_tag ssh_reverse_tag admin_host admin_port ssh_host ssh_port xray_config
local out stdout router_id vps_host vps_port uuid ssh_uuid encryption flow reverse_tag ssh_reverse_tag admin_host admin_port ssh_host ssh_port xray_config xray_config_dir
stdout="0"
if [ "${1:-}" = "--stdout" ]; then
stdout="1"
@@ -429,7 +429,13 @@ render_client_config() {
[ -n "$uuid" ] || die "vless_uuid пустой"
[ -n "$ssh_uuid" ] || ssh_uuid="$uuid"
if [ "$stdout" = "1" ]; then
out="${TMPDIR:-/tmp}/owrt-remote-client.$$"
else
xray_config_dir="$(dirname "$xray_config")"
mkdir -p "$xray_config_dir"
out="$xray_config_dir/.owrt-remote-client.$$"
fi
cat >"$out" <<EOF
{
"log": {
@@ -516,10 +522,8 @@ EOF
return
fi
mkdir -p "$(dirname "$xray_config")"
cp "$out" "$xray_config"
mv -f "$out" "$xray_config"
chmod 0600 "$xray_config"
rm -f "$out"
printf '%s\n' "$xray_config"
}