update_0.4 #4

Merged
pyrschtjag merged 1 commits from update_0.4 into main 2026-03-23 10:48:19 +04:00
Showing only changes of commit 2f6beda38b - Show all commits
+78 -41
View File
@@ -3,7 +3,7 @@
. /lib/functions.sh . /lib/functions.sh
_log() { _log() {
local msg="$1" level="${2:-info}" to_console="${3:-0}" local msg="$1" level="${2:-info}" to_console="${3:-1}"
logger -t xray-manager -p "daemon.$level" "$msg" logger -t xray-manager -p "daemon.$level" "$msg"
[ "$to_console" = "1" ] && echo "[$level] $msg" [ "$to_console" = "1" ] && echo "[$level] $msg"
} }
@@ -76,6 +76,7 @@ _load_config_full() {
_nft_init() { _nft_init() {
[ -z "$TABLE" ] && return 1 [ -z "$TABLE" ] && return 1
_log "Initializing NFT table: $TABLE" "info" "1"
nft add table ip "$TABLE" 2>/dev/null nft add table ip "$TABLE" 2>/dev/null
nft add chain ip "$TABLE" prerouting { type filter hook prerouting priority mangle \; policy accept \; } 2>/dev/null nft add chain ip "$TABLE" prerouting { type filter hook prerouting priority mangle \; policy accept \; } 2>/dev/null
@@ -123,7 +124,7 @@ _nft_apply_data() {
nft add set ip "$TABLE" "$nft_sname" { $set_params } 2>/dev/null nft add set ip "$TABLE" "$nft_sname" { $set_params } 2>/dev/null
if [ "$changed" = "1" ] && [ -s "$file" ] && [[ "$type" == *"ip"* ]]; then if [ "$changed" = "1" ] && [ -s "$file" ] && [[ "$type" == *"ip"* ]]; then
_log "Syncing '$nft_sname' to NFT..." _log " -> Syncing '$nft_sname' to NFT ruleset..." "info" "1"
nft flush set ip "$TABLE" "$nft_sname" nft flush set ip "$TABLE" "$nft_sname"
_nft_batch_insert "$TABLE" "$nft_sname" "$file" _nft_batch_insert "$TABLE" "$nft_sname" "$file"
fi fi
@@ -158,41 +159,84 @@ _process_item() {
local new_hash=$(_get_md5 "$val") local new_hash=$(_get_md5 "$val")
local old_hash=$(cat "$hfile" 2>/dev/null) local old_hash=$(cat "$hfile" 2>/dev/null)
# RELOAD MODE
if [ "$SKIP_URL" = "1" ] && { [ "$type" = "uip" ] || [ "$type" = "udom" ]; }; then if [ "$SKIP_URL" = "1" ] && { [ "$type" = "uip" ] || [ "$type" = "udom" ]; }; then
[ "$new_hash" != "$old_hash" ] && _log "$id: Config changed. Run update." "warn" "1" _log "Reloading $id..." "info" "1"
if [ "$new_hash" != "$old_hash" ]; then
_log " -> Warning: UCI config changed. Run 'update' to fetch new data." "warn" "1"
fi
[ -f "$fullpath" ] && _nft_apply_data "$id" "$fullpath" "$exc" "$dir" "$type" "0" [ -f "$fullpath" ] && _nft_apply_data "$id" "$fullpath" "$exc" "$dir" "$type" "0"
return return
fi fi
if [ "$new_hash" != "$old_hash" ] || [ ! -f "$fullpath" ]; then if [ "$new_hash" != "$old_hash" ] || [ ! -f "$fullpath" ] || [ "$SKIP_URL" = "0" ]; then
changed=1
case "$type" in case "$type" in
ip) echo "$val" | tr ' ' '\n' | sed 's/\r//g; /^#/d; /^[[:space:]]*$/d; /\//! s|$|/32|' | sort -u > "$fullpath" ;; ip)
echo "$val" | tr ' ' '\n' | sed 's/\r//g; /^#/d; /^[[:space:]]*$/d; /\//! s|$|/32|' | sort -u > "$fullpath.tmp"
if ! cmp -s "$fullpath.tmp" "$fullpath"; then
mv "$fullpath.tmp" "$fullpath"; changed=1
else
rm -f "$fullpath.tmp"
fi
;;
dom) dom)
echo "$val" | tr ' ' '\n' | awk -v t="$TABLE" -v i="s_$id" ' echo "$val" | tr ' ' '\n' | awk -v t="$TABLE" -v i="s_$id" '
/^[[:space:]]*#/ || /^[[:space:]]*$/ { next } /^[[:space:]]*#/ || /^[[:space:]]*$/ { next }
{ gsub(/\r/, ""); print "nftset=/" tolower($1) "/4#ip#" t "#" i }' | sort -u > "$fullpath" { gsub(/\r/, ""); print "nftset=/" tolower($1) "/4#ip#" t "#" i }' | sort -u > "$fullpath.tmp"
DNS_CHANGES=1 ;; if ! cmp -s "$fullpath.tmp" "$fullpath"; then
mv "$fullpath.tmp" "$fullpath"; changed=1; DNS_CHANGES=1
else
rm -f "$fullpath.tmp"
fi
;;
uip|udom) uip|udom)
_log "Processing $id..." "info" _log "Checking $id..." "info" "1"
local tmp_all="$CACHE_DIR/dl_all_$id"; : > "$tmp_all" local tmp_all="$CACHE_DIR/dl_all_$id"; : > "$tmp_all"
local dl_ok=0 local dl_ok=0
for url in $val; do for url in $val; do
curl -sSfL --connect-timeout 10 "$url" >> "$tmp_all" && dl_ok=1 local url_hash=$(echo "$url" | md5sum | cut -c1-8)
echo "" >> "$tmp_all" local etag_file="$CACHE_DIR/${id}_${url_hash}.etag"
local header_file="$CACHE_DIR/header_$id"
local old_etag=$(cat "$etag_file" 2>/dev/null | tr -d '\r\n ')
local etag_header=""; [ -n "$old_etag" ] && etag_header="-H 'If-None-Match: \"$old_etag\"'"
eval "curl -sSfL --connect-timeout 10 $etag_header -D '$header_file' '$url'" >> "$tmp_all"
local exit_code=$?
local current_etag=$(grep -i "^etag:" "$header_file" | sed 's/[Ee][Tt][Aa][Gg]: //; s/W\///; s/["\r\n ]//g')
if grep -q "HTTP/.* 304" "$header_file" 2>/dev/null; then
_log " -> $url_hash: 304 Not Modified" "info" "1"
[ -f "$fullpath" ] && cat "$fullpath" >> "$tmp_all"
dl_ok=1
elif [ $exit_code -eq 0 ]; then
_log " -> $url_hash: 200 OK" "info" "1"
[ -n "$current_etag" ] && echo "$current_etag" > "$etag_file"
dl_ok=1
else
_log " -> $url_hash: Error (Code: $exit_code)" "warn" "1"
[ -f "$fullpath" ] && cat "$fullpath" >> "$tmp_all"
fi
rm -f "$header_file"; echo "" >> "$tmp_all"
done done
if [ "$dl_ok" = "1" ] && [ -s "$tmp_all" ]; then if [ "$dl_ok" = "1" ] && [ -s "$tmp_all" ]; then
local out="$CACHE_DIR/out_$id" local out="$CACHE_DIR/out_$id"
if [ "$type" = "uip" ]; then if [ "$type" = "uip" ]; then
sed 's/\r//g; /^#/d; /^[[:space:]]*$/d' "$tmp_all" | \ sed 's/\r//g; /^#/d; /^[[:space:]]*$/d' "$tmp_all" | awk '{print ($1 ~ /\//) ? $1 : $1"/32"}' | sort -u > "$out"
awk '{print ($1 ~ /\//) ? $1 : $1"/32"}' | sort -u > "$out"
else else
tr '[:upper:]' '[:lower:]' < "$tmp_all" | \ tr '[:upper:]' '[:lower:]' < "$tmp_all" | grep -oE '([a-z0-9-]+\.)+[a-z]{2,}' | sort -u | \
grep -oE '([a-z0-9-]+\.)+[a-z]{2,}' | sort -u | \
sed "s|^|nftset=/|; s|\$|/4#ip#$TABLE#s_$id|" > "$out" sed "s|^|nftset=/|; s|\$|/4#ip#$TABLE#s_$id|" > "$out"
DNS_CHANGES=1
fi fi
mv "$out" "$fullpath"; changed=1
if [ -f "$fullpath" ] && cmp -s "$out" "$fullpath"; then
_log " -> $id: No changes in content." "info" "1"
rm -f "$out"
else
_log " -> $id: Content updated." "info" "1"
mv "$out" "$fullpath"; changed=1
[ "$type" = "udom" ] && DNS_CHANGES=1
fi
fi fi
rm -f "$tmp_all" ;; rm -f "$tmp_all" ;;
esac esac
@@ -205,15 +249,12 @@ _process_section() {
local s="$1" name dir exc ips doms uips udoms local s="$1" name dir exc ips doms uips udoms
config_get name "$s" name; config_get dir "$s" direction "dst"; config_get exc "$s" exclude "0" config_get name "$s" name; config_get dir "$s" direction "dst"; config_get exc "$s" exclude "0"
[ -z "$name" ] && return [ -z "$name" ] && return
name=${name//[^a-zA-Z0-9_]/} name=${name//[^a-zA-Z0-9_]/}
_append_list() { CURRENT_VAL="${CURRENT_VAL}${CURRENT_VAL:+ }$1"; } _append_list() { CURRENT_VAL="${CURRENT_VAL}${CURRENT_VAL:+ }$1"; }
CURRENT_VAL=""; config_list_foreach "$s" ip _append_list; ips="$CURRENT_VAL" CURRENT_VAL=""; config_list_foreach "$s" ip _append_list; ips="$CURRENT_VAL"
CURRENT_VAL=""; config_list_foreach "$s" domain _append_list; doms="$CURRENT_VAL" CURRENT_VAL=""; config_list_foreach "$s" domain _append_list; doms="$CURRENT_VAL"
CURRENT_VAL=""; config_list_foreach "$s" url_ip _append_list; uips="$CURRENT_VAL" CURRENT_VAL=""; config_list_foreach "$s" url_ip _append_list; uips="$CURRENT_VAL"
CURRENT_VAL=""; config_list_foreach "$s" url_domain _append_list; udoms="$CURRENT_VAL" CURRENT_VAL=""; config_list_foreach "$s" url_domain _append_list; udoms="$CURRENT_VAL"
[ -n "$ips" ] && _process_item "${name}_ip" "$ips" "ip" "$dir" "$exc" [ -n "$ips" ] && _process_item "${name}_ip" "$ips" "ip" "$dir" "$exc"
[ -n "$doms" ] && _process_item "${name}_dom" "$doms" "dom" "$dir" "$exc" [ -n "$doms" ] && _process_item "${name}_dom" "$doms" "dom" "$dir" "$exc"
[ -n "$uips" ] && _process_item "${name}_uip" "$uips" "uip" "$dir" "$exc" [ -n "$uips" ] && _process_item "${name}_uip" "$uips" "uip" "$dir" "$exc"
@@ -225,9 +266,8 @@ _cleanup_orphans() {
for f in "$IPNET_DIR"/* "$DNSMASQ_DIR"/*; do for f in "$IPNET_DIR"/* "$DNSMASQ_DIR"/*; do
[ -e "$f" ] || continue [ -e "$f" ] || continue
if ! grep -Fxq "$f" "$ACTIVE_LIST" 2>/dev/null; then if ! grep -Fxq "$f" "$ACTIVE_LIST" 2>/dev/null; then
local base="${f##*/}" local base="${f##*/}"; local sname="${base%.*}"
local sname="${base%.*}" _log "Orphan detected: $sname. Cleaning up..." "info" "1"
_log "Orphan detected: $sname. Cleaning up..." "info"
nft delete set ip "$TABLE" "s_$sname" 2>/dev/null nft delete set ip "$TABLE" "s_$sname" 2>/dev/null
[[ "$f" == "$DNSMASQ_DIR"* ]] && DNS_CHANGES=1 [[ "$f" == "$DNSMASQ_DIR"* ]] && DNS_CHANGES=1
rm -f "$f" "$CACHE_DIR/$sname.hash" rm -f "$f" "$CACHE_DIR/$sname.hash"
@@ -238,27 +278,30 @@ _cleanup_orphans() {
_run() { _run() {
DNS_CHANGES=0 DNS_CHANGES=0
if [ "$FULL_LOAD" = "1" ]; then if [ "$FULL_LOAD" = "1" ]; then
_log "Starting Xray-Manager (Full Load)..." "info" "1"
_load_config_full || return 1 _load_config_full || return 1
else else
_log "Running Xray-Manager (Partial Load)..." "info" "1"
_init_vars || return 1 _init_vars || return 1
fi fi
: > "$ACTIVE_LIST" : > "$ACTIVE_LIST"
_nft_init || return 1 _nft_init || return 1
local lnet_val=$(uci -q get xray-manager.@lnet[0].ip) local lnet_val=$(uci -q get xray-manager.@lnet[0].ip)
[ -n "$lnet_val" ] && _process_item "lnet" "$lnet_val" "ip" "dst" "1" [ -n "$lnet_val" ] && _process_item "lnet" "$lnet_val" "ip" "dst" "1"
config_foreach _process_section "xray-list" config_foreach _process_section "xray-list"
_cleanup_orphans _cleanup_orphans
if [ "$DNS_CHANGES" = "1" ]; then
[ "$DNS_CHANGES" = "1" ] && /etc/init.d/dnsmasq restart _log "Domains changes detected. Restarting dnsmasq..." "info" "1"
/etc/init.d/dnsmasq restart
else
_log "No domains changes. dnsmasq restart skipped." "info" "1"
fi
} }
_stop() { _stop() {
_load_config_full || exit 1 _load_config_full || exit 1
_log "Stopping Xray-Manager and flushing NFT table..." "info" "1"
nft delete table ip "$TABLE" 2>/dev/null nft delete table ip "$TABLE" 2>/dev/null
ip rule del fwmark "$TPROXY_MARK" table 110 2>/dev/null ip rule del fwmark "$TPROXY_MARK" table 110 2>/dev/null
ip route del local default dev lo table 110 2>/dev/null ip route del local default dev lo table 110 2>/dev/null
@@ -267,16 +310,10 @@ _stop() {
} }
case "$1" in case "$1" in
start) start) export SKIP_URL=0 FULL_LOAD=1; _run ;;
export SKIP_URL=0 FULL_LOAD=1; _run ;; restart) _stop; export SKIP_URL=0 FULL_LOAD=1; _run ;;
restart) reload) export SKIP_URL=1 FULL_LOAD=0; _run ;;
_stop; export SKIP_URL=0 FULL_LOAD=1; _run ;; update) export SKIP_URL=0 FULL_LOAD=0; _run ;;
reload) stop) _stop ;;
export SKIP_URL=1 FULL_LOAD=0; _run ;; *) echo "Usage: $0 {start|reload|update|stop|restart}" ;;
update)
export SKIP_URL=0 FULL_LOAD=0; _run ;;
stop)
_stop ;;
*)
echo "Usage: $0 {start|reload|update|stop|restart}" ;;
esac esac