Commit 061132a4 authored by Vitaly Lipatov's avatar Vitaly Lipatov

route-update.sh: skip re-resolve when only gateway/options changed

Separate lists_hash tracks list file changes independently. When only gateway or options file changed, reuse saved resolved IPs instead of re-resolving all domains. Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 0a11e284
...@@ -337,8 +337,10 @@ process_routes() ...@@ -337,8 +337,10 @@ process_routes()
fi fi
fi fi
# Compute hash of all list contents + gateway + options # Compute hashes: lists only (for resolve skip) and full (for change detect)
local lists_hash=$(md5_lists $lists)
local current_hash=$(md5_lists "$gwdir/gateway" "$gwdir/options" $lists) local current_hash=$(md5_lists "$gwdir/gateway" "$gwdir/options" $lists)
local need_reload= need_resolve=1
if [ -z "$FORCE" ] && [ -z "$RESOLVE" ] && [ -f "$STATE_DIR/$state/hash" ] ; then if [ -z "$FORCE" ] && [ -z "$RESOLVE" ] && [ -f "$STATE_DIR/$state/hash" ] ; then
local saved_hash local saved_hash
...@@ -346,7 +348,6 @@ process_routes() ...@@ -346,7 +348,6 @@ process_routes()
vlog "[$name]$label hash: saved=$saved_hash current=$current_hash" vlog "[$name]$label hash: saved=$saved_hash current=$current_hash"
if [ "$current_hash" = "$saved_hash" ] ; then if [ "$current_hash" = "$saved_hash" ] ; then
# Check if rules/routes were lost (e.g. after network restart) # Check if rules/routes were lost (e.g. after network restart)
local need_reload=
local rule_count=$($ipcmd -N rule show | grep -c "lookup $table") local rule_count=$($ipcmd -N rule show | grep -c "lookup $table")
local route_count=$($ipcmd route show table "$table" 2>/dev/null | wc -l) local route_count=$($ipcmd route show table "$table" 2>/dev/null | wc -l)
vlog "[$name]$label hash match, checking state: rules=$rule_count routes=$route_count" vlog "[$name]$label hash match, checking state: rules=$rule_count routes=$route_count"
...@@ -375,6 +376,19 @@ process_routes() ...@@ -375,6 +376,19 @@ process_routes()
vlog "[$name]$label state: force=$FORCE resolve=$RESOLVE hash_exists=$([ -f "$STATE_DIR/$state/hash" ] && echo yes || echo no)" vlog "[$name]$label state: force=$FORCE resolve=$RESOLVE hash_exists=$([ -f "$STATE_DIR/$state/hash" ] && echo yes || echo no)"
fi fi
# Skip re-resolve if only gateway/options changed (lists unchanged)
local resolved_new="$STATE_DIR/$state/resolved.new"
if [ -z "$FORCE" ] && [ -z "$RESOLVE" ] && [ -f "$STATE_DIR/$state/lists_hash" ] && [ -f "$STATE_DIR/$state/resolved" ] ; then
local saved_lists_hash
read -r saved_lists_hash < "$STATE_DIR/$state/lists_hash"
if [ "$saved_lists_hash" = "$lists_hash" ] ; then
need_resolve=
cp "$STATE_DIR/$state/resolved" "$resolved_new"
log "[$name]$label Gateway/options changed, reusing resolved IPs"
fi
fi
if [ -n "$need_resolve" ] ; then
if [ -n "$RESOLVE" ] ; then if [ -n "$RESOLVE" ] ; then
log "[$name]$label Re-resolving..." log "[$name]$label Re-resolving..."
else else
...@@ -382,7 +396,6 @@ process_routes() ...@@ -382,7 +396,6 @@ process_routes()
fi fi
# Split: pure IP files go through cat, domain files through resolve # Split: pure IP files go through cat, domain files through resolve
local resolved_new="$STATE_DIR/$state/resolved.new"
local ip_lists="" domain_lists="" local ip_lists="" domain_lists=""
for f in $lists ; do for f in $lists ; do
if grep -v '^#' "$f" | grep -q '[a-zA-Z]' ; then if grep -v '^#' "$f" | grep -q '[a-zA-Z]' ; then
...@@ -409,6 +422,7 @@ process_routes() ...@@ -409,6 +422,7 @@ process_routes()
grep -v '^#' | grep -v '^$' grep -v '^#' | grep -v '^$'
fi fi
} | sort -u > "$resolved_new" } | sort -u > "$resolved_new"
fi
# Check if resolved IPs actually changed # Check if resolved IPs actually changed
vlog "[$name]$label resolved $(wc -l < "$resolved_new") IPs, checking against saved state" vlog "[$name]$label resolved $(wc -l < "$resolved_new") IPs, checking against saved state"
...@@ -462,6 +476,7 @@ process_routes() ...@@ -462,6 +476,7 @@ process_routes()
# Save state # Save state
echo "$current_hash" > "$STATE_DIR/$state/hash" echo "$current_hash" > "$STATE_DIR/$state/hash"
echo "$lists_hash" > "$STATE_DIR/$state/lists_hash"
echo "$table" > "$STATE_DIR/$state/table" echo "$table" > "$STATE_DIR/$state/table"
cp "$gwdir/gateway" "$STATE_DIR/$state/gateway" cp "$gwdir/gateway" "$STATE_DIR/$state/gateway"
mv "$resolved_new" "$STATE_DIR/$state/resolved" mv "$resolved_new" "$STATE_DIR/$state/resolved"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment