Avoid router tunnel restarts during SSH
This commit is contained in:
1 parent
d27a52c1d1
commit
8420f38540
2 files changed
+60
-1
No files matched your search
@@ -45,6 +45,37 @@ safe_num() {
|
|||||||
esac
|
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"
|
mkdir -p "$STATE_DIR"
|
||||||
stamp="$(date +%s 2>/dev/null || echo 0)-$$-${ACTION:-event}-${INTERFACE:-iface}-${DEVICE:-dev}"
|
stamp="$(date +%s 2>/dev/null || echo 0)-$$-${ACTION:-event}-${INTERFACE:-iface}-${DEVICE:-dev}"
|
||||||
printf '%s\n' "$stamp" >"$STAMP_FILE"
|
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"
|
log "WAN event $ACTION on ${INTERFACE:-unknown}/${DEVICE:-unknown}, restart skipped by cooldown"
|
||||||
exit 0
|
exit 0
|
||||||
fi
|
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"
|
log "WAN event $ACTION on ${INTERFACE:-unknown}/${DEVICE:-unknown}, restarting reverse tunnel"
|
||||||
if /etc/init.d/owrt-remote restart >/dev/null 2>&1; then
|
if /etc/init.d/owrt-remote restart >/dev/null 2>&1; then
|
||||||
/usr/sbin/owrt-remote heartbeat wan-reconnect >/dev/null 2>&1 || true
|
/usr/sbin/owrt-remote heartbeat wan-reconnect >/dev/null 2>&1 || true
|
||||||
|
|||||||
@@ -231,6 +231,25 @@ ssh_running() {
|
|||||||
ps_all | grep '[d]ropbear' >/dev/null 2>&1
|
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() {
|
xray_bin_path() {
|
||||||
local configured candidate
|
local configured candidate
|
||||||
configured="$(uci_get xray_bin /usr/bin/xray)"
|
configured="$(uci_get xray_bin /usr/bin/xray)"
|
||||||
@@ -661,6 +680,10 @@ heartbeat_self_heal() {
|
|||||||
printf '%s\n' "$fails" >"$fail_file"
|
printf '%s\n' "$fails" >"$fail_file"
|
||||||
|
|
||||||
[ "$fails" -lt "$threshold" ] && return 0
|
[ "$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)"
|
[ -f "$restart_file" ] && last_restart="$(cat "$restart_file" 2>/dev/null || echo 0)"
|
||||||
last_restart="$(safe_num "$last_restart" 0)"
|
last_restart="$(safe_num "$last_restart" 0)"
|
||||||
[ $((now - last_restart)) -lt "$cooldown" ] && return 0
|
[ $((now - last_restart)) -lt "$cooldown" ] && return 0
|
||||||
|
|||||||
Reference in new issue
Block a user