From 8420f38540ca76cb5e7ca3c2ae5b7c6c4645e695 Mon Sep 17 00:00:00 2001 From: kzolotarev95 Date: Mon, 27 Jul 2026 20:58:28 +0400 Subject: [PATCH] Avoid router tunnel restarts during SSH --- files/etc/hotplug.d/iface/99-owrt-remote | 38 +++++++++++++++++++++++- files/usr/sbin/owrt-remote | 23 ++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/files/etc/hotplug.d/iface/99-owrt-remote b/files/etc/hotplug.d/iface/99-owrt-remote index 5d706cb..af35314 100755 --- a/files/etc/hotplug.d/iface/99-owrt-remote +++ b/files/etc/hotplug.d/iface/99-owrt-remote @@ -45,6 +45,37 @@ safe_num() { esac } +uci_get() { + local key default value + key="$1" + default="$2" + value="$(uci -q get "owrtremote.main.$key" 2>/dev/null || true)" + [ -n "$value" ] && { + printf '%s' "$value" + return + } + printf '%s' "$default" +} + +active_ssh_session() { + local port hex file + port="$(safe_num "$(uci_get ssh_port 22)" 22)" + hex="$(printf '%04X' "$port" 2>/dev/null || printf '0016')" + for file in /proc/net/tcp /proc/net/tcp6; do + [ -r "$file" ] || continue + awk -v port="$hex" ' + NR > 1 { + split($2, local_addr, ":") + if (local_addr[2] == port && $4 == "01") { + found = 1 + } + } + END { exit found ? 0 : 1 } + ' "$file" 2>/dev/null && return 0 + done + return 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" @@ -71,8 +102,13 @@ printf '%s\n' "$stamp" >"$STAMP_FILE" log "WAN event $ACTION on ${INTERFACE:-unknown}/${DEVICE:-unknown}, restart skipped by cooldown" exit 0 fi - printf '%s\n' "$now" >"$LAST_RESTART_FILE" + if active_ssh_session; then + log "WAN event $ACTION on ${INTERFACE:-unknown}/${DEVICE:-unknown}, restart postponed because SSH session is active" + exit 0 + fi + + printf '%s\n' "$now" >"$LAST_RESTART_FILE" log "WAN event $ACTION on ${INTERFACE:-unknown}/${DEVICE:-unknown}, restarting reverse tunnel" if /etc/init.d/owrt-remote restart >/dev/null 2>&1; then /usr/sbin/owrt-remote heartbeat wan-reconnect >/dev/null 2>&1 || true diff --git a/files/usr/sbin/owrt-remote b/files/usr/sbin/owrt-remote index 881904c..8386426 100644 --- a/files/usr/sbin/owrt-remote +++ b/files/usr/sbin/owrt-remote @@ -231,6 +231,25 @@ ssh_running() { ps_all | grep '[d]ropbear' >/dev/null 2>&1 } +active_ssh_session() { + local port hex file + port="$(safe_num "$(uci_get ssh_port 22)" 22)" + hex="$(printf '%04X' "$port" 2>/dev/null || printf '0016')" + for file in /proc/net/tcp /proc/net/tcp6; do + [ -r "$file" ] || continue + awk -v port="$hex" ' + NR > 1 { + split($2, local_addr, ":") + if (local_addr[2] == port && $4 == "01") { + found = 1 + } + } + END { exit found ? 0 : 1 } + ' "$file" 2>/dev/null && return 0 + done + return 1 +} + xray_bin_path() { local configured candidate configured="$(uci_get xray_bin /usr/bin/xray)" @@ -661,6 +680,10 @@ heartbeat_self_heal() { printf '%s\n' "$fails" >"$fail_file" [ "$fails" -lt "$threshold" ] && return 0 + if active_ssh_session; then + log "heartbeat failed ${threshold} times, restart postponed because SSH session is active" + return 0 + fi [ -f "$restart_file" ] && last_restart="$(cat "$restart_file" 2>/dev/null || echo 0)" last_restart="$(safe_num "$last_restart" 0)" [ $((now - last_restart)) -lt "$cooldown" ] && return 0