From e0f3b4ab8b5e047ddad4b58e0f554b9d68209d79 Mon Sep 17 00:00:00 2001 From: root Date: Mon, 13 Apr 2026 13:50:57 +0400 Subject: [PATCH] fix: minor bugs feat: debug logs --- xray-manager.sh | 130 +++++++++++++++++++++++++++--------------------- 1 file changed, 74 insertions(+), 56 deletions(-) diff --git a/xray-manager.sh b/xray-manager.sh index dfd89ba..f3a965d 100755 --- a/xray-manager.sh +++ b/xray-manager.sh @@ -18,25 +18,29 @@ _log() { [ "$level" = "debug" ] && [ "$DEBUG" -ne 1 ] && return if [ "$DEBUG" = "1" ] && [ -n "$DEBUG_LOG" ]; then - echo "$msg" >> "$DEBUG_LOG" + echo "$(date '+%Y-%m-%d %H:%M:%S') $msg" >> "$DEBUG_LOG" fi - local prefix="" + local sys_level="info" case "$level" in - err) prefix="[error] " ;; - warn) prefix="[warn] " ;; - debug) prefix="[debug] " ;; - *) prefix="[info] " ;; + info) sys_level="info" ;; + crit) sys_level="crit" ;; + err) sys_level="err" ;; + warn) sys_level="warning" ;; + debug) sys_level="debug" ;; esac - [ "$level" != "debug" ] && logger -t xray-manager -p "daemon.$level" "$msg" - [ "$to_console" = "1" ] && echo "${prefix}${msg}" + [ "$level" != "debug" ] && logger -t xray-manager -p "daemon.$sys_level" "$msg" + + if [ "$to_console" = "1" ]; then + printf "%s\n" "$msg" + fi } _get_md5() { echo "$1" | md5sum | cut -d' ' -f1; } _init_vars() { - _log "[config] loading configuration" "debug" + _log "config: loading configuration" "debug" config_load xray-manager local s="main" TABLE=$(uci -q get xray-manager.$s.table_name) @@ -57,12 +61,12 @@ _init_vars() { for var in $required_vars; do eval val=\$$var if [ -z "$val" ]; then - _log "[config] variable '$var' is missing" "err" + _log "config: variable '$var' is missing" "crit" return 1 fi done - _log "[config] loaded table=$TABLE, port=$TPROXY_PORT, mark=$TPROXY_MARK" "debug" + _log "config: loaded table=$TABLE, port=$TPROXY_PORT, mark=$TPROXY_MARK" "debug" mkdir -p "$IPNET_DIR" "$DNSMASQ_DIR" "$CACHE_DIR" "${ACTIVE_LIST%/*}" if [ "$DEBUG" = "1" ] && [ -n "$DEBUG_LOG" ]; then @@ -73,12 +77,12 @@ _init_vars() { } _validate_config() { - _log "[config] validating uci syntax and paths" "debug" + _log "config: validating uci syntax" "debug" local dns_confdir dns_confdir=$(uci -q get dhcp.@dnsmasq[0].confdir) if [ -z "$dns_confdir" ] || ! echo "$dns_confdir" | grep -q "$DNSMASQ_DIR"; then - _log "[dnsmasq] $DNSMASQ_DIR is not set in dhcp.@dnsmasq[0].confdir" "warn" + _log "config: $DNSMASQ_DIR is not set in dnsmasq" "warn" fi local allowed_opts="name direction exclude parent ip domain url_ip url_domain" @@ -89,7 +93,7 @@ _validate_config() { config_get direction "$s" direction "dst" if [ -z "$name" ]; then - _log "[config] section $s is missing 'option name'" "err" + _log "config: section $s is missing name" "crit" exit 1 fi @@ -100,24 +104,25 @@ _validate_config() { esac if ! echo "$allowed_opts" | grep -qw "$current_opt"; then - _log "[$name] unknown option found: '$current_opt'" "err" + _log "$name: unknown option: '$current_opt'" "err" exit 1 fi done case "$direction" in src|dst|out|force_src) ;; - *) _log "[$name] invalid direction: $direction" "err"; exit 1 ;; + *) _log "$name: invalid direction: $direction" "err"; exit 1 ;; esac } config_foreach validate_list_section "xray-list" + _log "config: validation complete" "info" } _nft_init() { - _log "[$TABLE] initializing nft table" "debug" + _log "$TABLE: initializing nft table" "info" nft add table ip "$TABLE" 2>/dev/null - _log "[$TABLE] setting up base chains" "debug" + _log "$TABLE: setting up base chains" "debug" nft add chain ip "$TABLE" prerouting { type filter hook prerouting priority mangle \; policy accept \; } 2>/dev/null nft flush chain ip "$TABLE" prerouting nft add rule ip "$TABLE" prerouting fib daddr type local accept @@ -126,10 +131,8 @@ _nft_init() { nft flush chain ip "$TABLE" output nft add rule ip "$TABLE" output fib daddr type local accept - local mark_hex - mark_hex=$(printf '0x%x' "$TPROXY_MARK") - - _log "[route] configuring table $RT_TABLE, mark $mark_hex" "debug" + local mark_hex=$(printf '0x%x' "$TPROXY_MARK") + _log "route: configuring table $RT_TABLE, mark $mark_hex" "debug" ip route show table $RT_TABLE | grep -q "local default" || ip route add local default dev lo table $RT_TABLE ip rule show | grep -q "fwmark $mark_hex lookup $RT_TABLE" || ip rule add fwmark "$TPROXY_MARK" table $RT_TABLE } @@ -141,12 +144,12 @@ _nft_prepare_data() { [[ "$type" == *dom ]] && set_params="type ipv4_addr; flags interval,timeout; timeout $TIMEOUT; auto-merge;" - _log "[$sname] ensuring nft set existence" "debug" + _log "$sname: creating nft set" "debug" nft add set ip "$TABLE" "$sname" { $set_params } 2>/dev/null if [ -s "$file" ] && [[ "$type" == *"ip"* ]]; then if [ "$changed" = "1" ] || [ "$FULL_LOAD" = "1" ]; then - _log "[$sname] flushing and loading elements" "debug" + _log "$sname: flushing and loading elements" "debug" nft flush set ip "$TABLE" "$sname" { printf "add element ip %s %s { " "$TABLE" "$sname" @@ -158,7 +161,7 @@ _nft_prepare_data() { if [ "$dir" = "force_src" ]; then local cn="f_$id" - _log "[$cn] creating force-chain" "debug" + _log "$cn: creating force-chain" "debug" nft add chain ip "$TABLE" "$cn" 2>/dev/null nft flush chain ip "$TABLE" "$cn" nft add rule ip "$TABLE" "$cn" meta l4proto "{ tcp, udp }" tproxy to 127.0.0.1:"$TPROXY_PORT" ct mark set "$CLIENT_MARK" meta mark set "$TPROXY_MARK" accept 2>/dev/null @@ -174,7 +177,7 @@ _nft_apply_rule() { case "$mode" in jumps) [ "$dir" = "force_src" ] && { - _log "[$nft_sname] applying jump to f_$sname" "debug" + _log "$nft_sname: applying jump to f_$sname" "debug" nft add rule ip "$TABLE" prerouting ip saddr "@$nft_sname" jump "f_${sname}" 2>/dev/null } ;; @@ -183,7 +186,7 @@ _nft_apply_rule() { local target_info="" [ -n "$parent" ] && target_info=" target=$parent" - _log "[$nft_sname] applying exclude rule$target_info" "debug" + _log "$nft_sname: applying exclude rule$target_info" "debug" if [ -n "$parent" ]; then nft insert rule ip "$TABLE" "$parent" ip daddr "@$nft_sname" accept 2>/dev/null @@ -195,7 +198,7 @@ _nft_apply_rule() { main_rules) [ "$exc" = "1" ] || [ "$dir" = "force_src" ] && return - _log "[$nft_sname] applying tproxy rules" "debug" + _log "$nft_sname: applying tproxy rules" "debug" if [ "$mode_chain" = "prerouting" ] && [ "$dir" != "out" ]; then if [ "$dir" = "src" ]; then @@ -224,12 +227,16 @@ _process_item() { local old_hash=$(cat "$hfile" 2>/dev/null) local status="stable" - [ "$new_hash" != "$old_hash" ] && status="changed" - _log "[$id] processing item (type: $type, status: $status)" "debug" + if [ "$new_hash" != "$old_hash" ]; then + status="changed" + [ "$SKIP_URL" = "1" ] && [[ "$type" == u* ]] && NEED_UPDATE=1 + fi + + _log "$id status: $status" "debug" if [ "$SKIP_URL" = "1" ] && [[ "$type" == u* ]]; then - _log "[$id] skipping download (reload mode)" "debug" - [ ! -f "$fullpath" ] && { _log "[$id] data file missing" "warn"; NEED_UPDATE=1; } + _log "$id: skipping download (reload mode)" "debug" + [ ! -f "$fullpath" ] && { _log "$id: data file missing" "warn"; NEED_UPDATE=1; } [ -f "$fullpath" ] && _nft_prepare_data "$id" "$fullpath" "$type" 0 "$dir" return fi @@ -243,14 +250,14 @@ _process_item() { echo "$val" | tr ' ' '\n' | awk -v t="$TABLE" -v i="s_$id" '/^[[:space:]]*#/ || /^[[:space:]]*$/ { next } { gsub(/\r/, ""); printf "nftset=/%s/4#ip#%s#%s\n", tolower($1), t, i }' | sort -u > "$fullpath.tmp" ;; uip|udom) - _log "[$id] fetching remote list" "info" + _log "$id: fetching remote list" "info" local tmp_all="$CACHE_DIR/dl_$id" : > "$tmp_all" local dl_ok=0 for url in $val; do local uhash=$(echo "$url" | md5sum | cut -c1-8) - _log "[$id] downloading $url" "debug" + _log "$id: downloading $url" "debug" local cache_raw="$CACHE_DIR/${id}_$uhash.raw" local hfile_tmp="$CACHE_DIR/h_$id" local etag_f="$CACHE_DIR/${id}_$uhash.etag" @@ -260,16 +267,16 @@ _process_item() { local res=$? if grep -q "304" "$hfile_tmp" 2>/dev/null; then - _log "[$id] [$uhash] 304 not modified" "info" + _log "$id: 304 not modified" "info" [ -f "$cache_raw" ] && cat "$cache_raw" >> "$tmp_all" && dl_ok=1 elif [ $res -eq 0 ] && [ -s "$tmp_all.raw" ]; then - _log "[$id] [$uhash] 200 ok" "info" + _log "$id: 200 ok" "info" grep -i "^etag:" "$hfile_tmp" | awk -F': ' '{print $2}' | sed 's/W\///; s/["\r\n ]//g' > "$etag_f" cat "$tmp_all.raw" > "$cache_raw" cat "$tmp_all.raw" >> "$tmp_all" dl_ok=1 else - _log "[$id] [$uhash] download failed, using local cache" "warn" + _log "$id: download failed, using local cache" "warn" [ -f "$cache_raw" ] && cat "$cache_raw" >> "$tmp_all" && dl_ok=1 fi done @@ -288,7 +295,7 @@ _process_item() { mv "$fullpath.tmp" "$fullpath" changed=1 [[ "$type" == *dom ]] && DNS_CHANGES=1 - _log "[$id] list updated" "info" + _log "$id: list updated" "info" else rm -f "$fullpath.tmp" fi @@ -310,7 +317,7 @@ _process_section() { [ "$exc" = "1" ] && [ -n "$parent" ] && parent="f_${parent}_ip" if [ -z "$mode" ]; then - _log "[$name] configuring section" "info" + _log "$name: configuring section" "debug" _get_v() { V="${V}${V:+ }$1"; } V=""; config_list_foreach "$s" ip _get_v; [ -n "$V" ] && _process_item "${name}_ip" "$V" "ip" "$dir" "$exc" "$parent" V=""; config_list_foreach "$s" domain _get_v; [ -n "$V" ] && _process_item "${name}_dom" "$V" "dom" "$dir" "$exc" "$parent" @@ -323,44 +330,45 @@ _process_section() { [ -f "$DNSMASQ_DIR/${name}_udom.lst" ] && _nft_apply_rule "${name}_udom" "$dir" "$exc" "$parent" "$mode" if [ "$mode" = "exclude" ] && [ "$dir" = "force_src" ]; then - _log "[$name] force-src: injecting lnet exclusion" "debug" + _log "$name: force-src: injecting lnet exclusion" "debug" _nft_apply_rule "$LNET_NAME" "dst" "1" "f_${name}_ip" "exclude" fi fi } _run() { - _log "[main] starting execution ($1)" "info" + _log "main: starting execution ($1)" "info" _init_vars || return 1 _validate_config DNS_CHANGES=0; NEED_UPDATE=0; : > "$ACTIVE_LIST" [ "$FULL_LOAD" = "1" ] && _wait_for_net + _wait_for_net _nft_init - _log "[main] processing data sections" "debug" + _log "main: processing data sections" "debug" config_foreach _process_section "xray-list" _process_item "$LNET_NAME" "$LNET_RESERVED" "ip" "dst" "1" - _log "[main] applying nft rules" "info" + _log "main: applying nft rules" "info" config_foreach _process_section "xray-list" "jumps" _nft_apply_rule "$LNET_NAME" "dst" "1" "" "exclude" config_foreach _process_section "xray-list" "exclude" - _log "[main] applying proxy bypass" "debug" + _log "main: applying proxy bypass" "debug" [ -n "$PROXY_SERVERS" ] && nft add rule ip "$TABLE" output ip daddr "@s_proxy_servers" accept 2>/dev/null - _log "[main] applying tproxy rules" "debug" + _log "main: applying tproxy rules" "debug" mode_chain="prerouting"; config_foreach _process_section "xray-list" "main_rules" mode_chain="output"; config_foreach _process_section "xray-list" "main_rules" - _log "[main] cleaning up orphans" "debug" + _log "main: cleaning up orphans" "info" if [ -n "$IPNET_DIR" ] && [ -n "$DNSMASQ_DIR" ]; then for f in "$IPNET_DIR"/* "$DNSMASQ_DIR"/*; do [ -e "$f" ] || continue if ! grep -Fxq "$f" "$ACTIVE_LIST"; then local fname="${f##*/}" - _log "[orphan] removing $fname" "info" + _log "orphan: removing $fname" "info" case "$f" in "$DNSMASQ_DIR"/*) DNS_CHANGES=1 ;; esac nft delete set ip "$TABLE" "s_${fname%.*}" 2>/dev/null rm -f "$f" @@ -368,33 +376,43 @@ _run() { done fi - [ "$DNS_CHANGES" = "1" ] && { _log "[dnsmasq] restarting service" "info"; /etc/init.d/dnsmasq restart; } - [ "$SKIP_URL" = "1" ] && [ "$NEED_UPDATE" = "1" ] && { _log "[main] some lists are outdated, run update" "warn"; } + [ "$DNS_CHANGES" = "1" ] && { _log "dnsmasq: restarting service" "info"; /etc/init.d/dnsmasq restart; } + [ "$SKIP_URL" = "1" ] && [ "$NEED_UPDATE" = "1" ] && { + _log "main: outdated lists detected" "warn" + _log "main: run 'update' to refresh data" "warn" + } - _log "[main] execution finished" "info" + _log "main: execution finished" "info" } _wait_for_net() { - _log "[network] checking connectivity" "debug" + _log "network: checking connectivity" "info" local t=1 while [ "$t" -le 20 ]; do - if ping -q -c 1 -W 2 "$PING_ADDR" >/dev/null 2>&1; then - _log "[network] interface is up" "debug" - return 0 + if ping -q -c 1 -W 2 "$PING_ADDR" >/dev/null 2>&1; then + if nslookup "$TEST_DOMAIN" >/dev/null 2>&1; then + _log "network: access confirmed" "info" + return 0 + else + _log "network: dns resolution failed" "warn" + fi + else + _log "network: unreachable" "warn" fi sleep 2; t=$((t + 2)) done - _log "[network] timeout, switching to offline mode" "warn" + _log "network: timeout: switching to offline mode" "warn" export SKIP_URL=1 } _stop() { - _log "[main] stopping service and clearing rules" "info" + _log "main: stopping service" "info" _init_vars nft delete table ip "$TABLE" 2>/dev/null ip rule del fwmark "$TPROXY_MARK" table $RT_TABLE 2>/dev/null ip route del local default dev lo table $RT_TABLE 2>/dev/null /etc/init.d/dnsmasq restart + _log "main: service stopped" "info" } case "$1" in