Release v116 with VPS router logs and reliable log delivery

Add per-router VPS log archives available offline, live delivery and last reported service state. Keep router logs in RAM with bounded rotation and deduplication. Align log pages and mobile statistics, and bump application and deployment tools to v116.
This commit is contained in:
kzolotarev95 committed 2026-10-14 08:05:52 +04:00
1 parent 4ceb3d031a
commit aa7970c98c
43 files changed
+1658 -121

No files matched your search

+148 -5
View File
@@ -5,8 +5,26 @@ LOG_LIMIT=1048576
LOG_KEEP_SECONDS=86400
log_clock() { cut -d. -f1 /proc/uptime; }
log_ram_storage() {
local resolved parent
if [ -e "$LOG_DIR" ] || [ -L "$LOG_DIR" ]; then
resolved="$(readlink -f "$LOG_DIR")" || return 1
else
# BusyBox realpath may require the last component to exist.
# Resolve the existing parent before creating the journal directory.
parent="$(readlink -f "$(dirname "$LOG_DIR")")" || return 1
resolved="$parent/$(basename "$LOG_DIR")"
fi
awk -v path="$resolved" '
(path == $2 || index(path, $2 "/") == 1 || $2 == "/") && length($2) > best {
best = length($2); filesystem = $3
}
END { exit(filesystem == "tmpfs" ? 0 : 1) }
' /proc/mounts
}
log_init() {
umask 077
log_ram_storage || { printf 'Remote journal requires RAM (tmpfs); refusing persistent storage.\n' >&2; return 1; }
mkdir -p "$LOG_DIR" || return 1
chmod 700 "$LOG_DIR"
}
@@ -41,8 +59,22 @@ log_unlock() {
log_reset_locked() {
: >"$LOG_DIR/current.log"
rm -f "$LOG_DIR/previous.log"
rm -f "$LOG_DIR/facts" "$LOG_DIR/facts.new" "$LOG_DIR/heartbeat-ok"
rm -f "$LOG_DIR/journal-id" "$LOG_DIR/total" "$LOG_DIR/sync-ack"
log_clock >"$LOG_DIR/age"
}
log_cursor_locked() {
local total
if [ ! -s "$LOG_DIR/journal-id" ]; then
cat /proc/sys/kernel/random/uuid >"$LOG_DIR/journal-id" 2>/dev/null ||
printf '%s-%s-%s\n' "$(date +%s)" "$(log_clock)" "$$" >"$LOG_DIR/journal-id"
fi
if [ ! -s "$LOG_DIR/total" ]; then
total="$(wc -c <"$LOG_DIR/current.log")"
[ ! -f "$LOG_DIR/previous.log" ] || total=$((total + $(wc -c <"$LOG_DIR/previous.log")))
printf '%s\n' "$total" >"$LOG_DIR/total"
fi
}
log_maintain_locked() {
local now age
now="$(log_clock)"
@@ -54,29 +86,83 @@ log_maintain_locked() {
}
log_redact() {
# Never retain command blocks containing credentials or authenticated URLs.
LC_ALL=C awk '
LC_ALL=C awk -v fallback="${1:-OK}" '
{
line = substr($0, 1, 4096); lower = tolower(line)
# Old info/access output may still arrive until Xray restarts.
if (lower ~ /\[(info|debug)\]/ || lower ~ / from .* accepted / || lower ~ /a unified platform for anti-censorship/) next
level = fallback
if (line ~ /^\[(OK|WARN|ERROR)\]/) { level = substr(line, 2, index(line, "]") - 2); sub(/^\[[^]]+\][ ]*/, "", line) }
else if (lower ~ /core: xray .* started/) level = "OK"
else if (lower ~ /\[error\]|ошибка|Ошибка|ОШИБКА|не удалось|Не удалось|результат=bad|failed|cannot |could not |error:|syntax error|permission denied|network is unreachable|connection refused|connection reset|timed out|not permitted|no selected wan|wan has no|wan direct requires|already used by another rule|does not belong to remote|не применилось|dns failed|crash loop/) level = "ERROR"
else if (lower ~ /\[warn(ing)?\]|предупреждение|Предупреждение|результат=warn|retry|not ready|missing|не найден|restart postponed|dns unavailable|fallback|reconnecting|restarting|failover/) level = "WARN"
if (lower ~ /(authorization|hub_token|vless_uuid|ssh_vless_uuid|password|passwd|web[._ -]?key|[?&]key=|[?&]token=|"uuid"|"privatekey"|"password")/ ||
lower ~ /[0-9a-f][0-9a-f][0-9a-f][0-9a-f]-[0-9a-f-]+-[0-9a-f-]+-[0-9a-f-]+/)
print "[строка с ключом или паролем скрыта]"
else print line
line = "[строка с ключом или паролем скрыта]"
print "[" level "] " line
}'
}
log_new_fact_locked() {
local key now heartbeat new_fact
# Compare event text without syslog timestamp, facility, PID or Xray time.
key="$(printf '%s\n' "$1" | tr '\t' ' ' | sed -e 's/^\[[^]]*\] *//' -e 's/.*owrt-remote\(\[[0-9][0-9]*\]\)\{0,1\}: *//' -e 's/^[0-9][0-9]*\/[0-9/:. ]* *//' -e 's/ *(\([0-9][0-9]*\)\/[0-9][0-9]*)//')"
heartbeat=0
case "$key" in
'Heartbeat: OK;'*)
heartbeat=1
if [ "$(cat "$LOG_DIR/heartbeat-ok" 2>/dev/null)" = "$key" ]; then return 1; fi
printf '%s\n' "$key" >"$LOG_DIR/heartbeat-ok"
# A recovery lets a later failure with the same cause appear again.
if [ -f "$LOG_DIR/facts" ]; then
awk -F '\t' '$2 !~ /Heartbeat:|heartbeat failed/ { print }' "$LOG_DIR/facts" >"$LOG_DIR/facts.new"
mv -f "$LOG_DIR/facts.new" "$LOG_DIR/facts"
fi ;;
esac
if [ "$heartbeat" = 1 ]; then return 0; fi
key="${1%%]*}] $key"
now="$(log_clock)"
# A bounded cache suppresses repeated identical facts while they continue.
# After a quiet period a new occurrence is recorded, rather than lost forever.
awk -F '\t' -v now="$now" -v key="$key" '
$2 == key { if (now - $1 < 120 && now >= $1) repeated = 1; next }
{ saved[++n] = $0 }
END {
for (i = (n > 63 ? n - 62 : 1); i <= n; i++) print saved[i]
print now "\t" key
exit(repeated ? 1 : 0)
}' "$LOG_DIR/facts" >"$LOG_DIR/facts.new" 2>/dev/null
new_fact=$?
mv -f "$LOG_DIR/facts.new" "$LOG_DIR/facts"
[ "$new_fact" = 0 ] || return 1
case "$key" in
*Heartbeat:*|*'heartbeat failed'*)
case "$1" in '[ERROR]'*|'[WARN]'*) rm -f "$LOG_DIR/heartbeat-ok" ;; esac ;;
*'Сервис: запуск') rm -f "$LOG_DIR/heartbeat-ok" ;;
esac
return 0
}
log_append() {
local line size
# Process one bounded line at a time and release the lock between writes.
log_redact | while IFS= read -r line; do
log_redact "${1:-OK}" | while IFS= read -r line; do
log_lock || exit 1
trap 'log_unlock' EXIT
trap 'exit 0' INT TERM
log_maintain_locked
log_cursor_locked
[ -f "$LOG_DIR/facts" ] || : >"$LOG_DIR/facts"
if ! log_new_fact_locked "$line"; then
log_unlock
trap - EXIT INT TERM
continue
fi
size="$(wc -c <"$LOG_DIR/current.log")"
if [ "$size" -ge "$LOG_LIMIT" ]; then
mv -f "$LOG_DIR/current.log" "$LOG_DIR/previous.log"
: >"$LOG_DIR/current.log"
fi
printf '%s\n' "$line" >>"$LOG_DIR/current.log"
printf '%s\n' "$(( $(cat "$LOG_DIR/total") + $(printf '%s\n' "$line" | wc -c) ))" >"$LOG_DIR/total"
log_unlock
trap - EXIT INT TERM
done
@@ -110,6 +196,57 @@ log_clear() {
log_reset_locked
log_unlock
}
log_export() {
local journal total previous current ack_id ack start available count clear_ack chunk
log_lock || return 1
log_maintain_locked
log_cursor_locked
journal="$(cat "$LOG_DIR/journal-id")"; total="$(cat "$LOG_DIR/total")"
ack_id=''; ack=0
if [ -s "$LOG_DIR/sync-ack" ]; then read -r ack_id ack <"$LOG_DIR/sync-ack"; fi
[ "$ack_id" = "$journal" ] || ack=0
case "$ack" in ''|*[!0-9]*) ack=0 ;; esac
[ "$ack" -le "$total" ] || ack=0
previous=0
if [ -f "$LOG_DIR/previous.log" ]; then previous="$(wc -c <"$LOG_DIR/previous.log")"; exec 3<"$LOG_DIR/previous.log"; else exec 3</dev/null; fi
current="$(wc -c <"$LOG_DIR/current.log")"; exec 4<"$LOG_DIR/current.log"
start=$((total - previous - current)); [ "$ack" -le "$start" ] || start="$ack"
available=$((total - start)); count="$available"; [ "$count" -le 65536 ] || count=65536
clear_ack="$(cat "$LOG_DIR/sync-clear-ack" 2>/dev/null)"
log_unlock
chunk="$LOG_DIR/export.$$"
trap 'rm -f "$chunk"; exec 3<&- 4<&-' EXIT
: >"$chunk"
if [ "$count" -gt 0 ]; then
{ head -c "$previous" <&3; head -c "$current" <&4; } | tail -c "$available" | head -c "$count" >"$chunk"
fi
count="$(wc -c <"$chunk")"
printf '{"journal_id":"%s","from":%s,"to":%s,"clear_ack":"%s","data":"' "$journal" "$start" "$((start + count))" "$clear_ack"
base64 <"$chunk" | tr -d '\r\n'
printf '"}\n'
rm -f "$chunk"; exec 3<&- 4<&-; trap - EXIT
}
log_ack() {
local journal offset clear_token
journal="$1"; offset="$2"; clear_token="${3:-}"
case "$journal$clear_token" in *[!a-zA-Z0-9._:-]*) return 1 ;; esac
case "$offset" in ''|*[!0-9]*) return 1 ;; esac
log_lock || return 1
log_maintain_locked
if [ -n "$clear_token" ] && [ "$(cat "$LOG_DIR/sync-clear-ack" 2>/dev/null)" != "$clear_token" ]; then
log_reset_locked
printf '%s\n' "$clear_token" >"$LOG_DIR/sync-clear-ack"
elif [ "$(cat "$LOG_DIR/journal-id" 2>/dev/null)" = "$journal" ]; then
printf '%s %s\n' "$journal" "$offset" >"$LOG_DIR/sync-ack"
fi
log_unlock
}
log_sync_loop() {
while :; do
/usr/sbin/owrt-remote logs-sync >/dev/null 2>&1
sleep 3
done
}
log_collect() {
# These variables must remain visible to EXIT even after this function returns.
log_init || return 1
@@ -134,10 +271,16 @@ log_collect() {
}
case "${1:-}" in
check) log_init ;;
collect) log_collect ;;
read) log_read "${2:-}" ;;
clear) log_clear ;;
export) log_export ;;
ack) log_ack "$2" "$3" "${4:-}" ;;
sync) log_sync_loop ;;
append) log_append ;;
event) { printf '%s owrt-remote: %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$2"; cat; } | log_append ;;
event)
case "${3:-ok}" in bad|error) level=ERROR ;; warn) level=WARN ;; *) level=OK ;; esac
{ printf '%s owrt-remote: %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$2"; cat; } | log_append "$level" ;;
*) printf 'Usage: logs.sh collect|read [live]|clear|append|event MESSAGE\n' >&2; exit 2 ;;
esac
+68 -2
View File
@@ -7,7 +7,8 @@ SECTION="main"
XRAY_RELEASE_BASE_URL="${XRAY_RELEASE_BASE_URL:-https://github.com/XTLS/Xray-core/releases/latest/download}"
log() {
printf '%s\n' "$*" >&2
# procd already captures stderr. Use only logger for managed processes.
[ "${OWRT_REMOTE_SERVICE_LOG:-0}" = 1 ] || printf '%s\n' "$*" >&2
logger -t owrt-remote "$*" 2>/dev/null || true
}
@@ -27,6 +28,7 @@ usage() {
owrt-remote run
owrt-remote heartbeat
owrt-remote heartbeat-loop
owrt-remote logs-sync
owrt-remote network-diagnostics
Настройки лежат в /etc/config/owrtremote.
@@ -559,7 +561,9 @@ render_client_config() {
cat >"$out" <<EOF
{
"log": {
"loglevel": "info"
"access": "none",
"error": "",
"loglevel": "warning"
},
"inbounds": [],
"outbounds": [
@@ -758,6 +762,65 @@ post_json() {
die "для heartbeat нужен uclient-fetch, wget или curl"
}
global_logs_state() {
local enabled xray
enabled=false; xray=stopped
truthy "$(uci_get enabled 0)" && enabled=true
xray_running && xray=running
printf '{"enabled":%s,"xray":"%s"}' "$enabled" "$xray"
}
sync_global_logs() (
local helper router_id batch file response endpoint base token journal offset clear_token rc sync_lock owner attempt
helper="${OWRT_REMOTE_LOG_HELPER:-/usr/lib/owrt-remote/logs.sh}"
[ -r "$helper" ] || return 0
token="$(uci_get hub_token '')"; [ -n "$token" ] || return 0
# The journal is independent of Remote's enabled flag and Xray lifecycle.
sh "$helper" check || return 1
sync_lock="${OWRT_REMOTE_LOG_DIR:-/tmp/owrt-remote-logs}/sync-lock"
if ! mkdir "$sync_lock" 2>/dev/null; then
# Give a sender time to write its PID, then recover an abandoned lock.
for attempt in 1 2 3 4 5 6 7 8 9 10; do
owner="$(cat "$sync_lock/pid" 2>/dev/null)"
case "$owner" in ''|*[!0-9]*) sleep 0.1 ;; *) break ;; esac
done
owner="$(cat "$sync_lock/pid" 2>/dev/null)"
case "$owner" in ''|*[!0-9]*) ;; *) kill -0 "$owner" 2>/dev/null && return 0 ;; esac
rm -f "$sync_lock/pid"; rmdir "$sync_lock" 2>/dev/null || return 0
mkdir "$sync_lock" 2>/dev/null || return 0
fi
printf '%s\n' "$$" >"$sync_lock/pid"
file=''; response=''
trap 'rm -f "$file" "$response" "$sync_lock/pid"; rmdir "$sync_lock" 2>/dev/null || true' EXIT
trap 'exit 1' INT TERM
if truthy "$(uci_get wan_direct 0)" && [ ! -s "$WAN_STATE/active-interface" ]; then wan_prepare || return 1; fi
batch="$(sh "$helper" export)" || return 1
router_id="$(safe_id "$(uci_get router_id main)")"
# export verifies that this directory is on tmpfs; payloads also stay in RAM.
umask 077
file="${OWRT_REMOTE_LOG_DIR:-/tmp/owrt-remote-logs}/sync.$$"; response="$file.response"
printf '{"id":%s,"batch":%s,"state":%s}\n' "$(json_string "$router_id")" "$batch" "$(global_logs_state)" >"$file"
for endpoint in $(heartbeat_candidate_endpoints); do
base="$(heartbeat_base_from_endpoint "$endpoint")"
if post_json "$base/api/router-logs" "$token" "$file" >"$response" 2>/dev/null &&
grep -Eq '"ok"[[:space:]]*:[[:space:]]*true' "$response"; then
journal="$(jsonfilter -i "$response" -e '@.journal_id' 2>/dev/null)"
offset="$(jsonfilter -i "$response" -e '@.offset' 2>/dev/null)"
clear_token="$(jsonfilter -i "$response" -e '@.clear_request' 2>/dev/null)"
sh "$helper" ack "$journal" "$offset" "$clear_token"
rc=$?
rm -f "$file" "$response"
rm -f "$sync_lock/pid"; rmdir "$sync_lock" 2>/dev/null || true
trap - EXIT INT TERM
return "$rc"
fi
done
rm -f "$file" "$response"
rm -f "$sync_lock/pid"; rmdir "$sync_lock" 2>/dev/null || true
trap - EXIT INT TERM
return 1
)
heartbeat_base_from_endpoint() {
printf '%s' "${1%/api/heartbeat}"
}
@@ -1284,6 +1347,9 @@ case "$cmd" in
heartbeat-loop)
heartbeat_loop
;;
logs-sync)
sync_global_logs
;;
*)
usage >&2
exit 2