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()
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 need_reload= need_resolve=1
if [ -z "$FORCE" ] && [ -z "$RESOLVE" ] && [ -f "$STATE_DIR/$state/hash" ] ; then
local saved_hash
......@@ -346,7 +348,6 @@ process_routes()
vlog "[$name]$label hash: saved=$saved_hash current=$current_hash"
if [ "$current_hash" = "$saved_hash" ] ; then
# 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 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"
......@@ -375,40 +376,53 @@ process_routes()
vlog "[$name]$label state: force=$FORCE resolve=$RESOLVE hash_exists=$([ -f "$STATE_DIR/$state/hash" ] && echo yes || echo no)"
fi
if [ -n "$RESOLVE" ] ; then
log "[$name]$label Re-resolving..."
else
log "[$name]$label Changes detected, resolving..."
# 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
# Split: pure IP files go through cat, domain files through resolve
local resolved_new="$STATE_DIR/$state/resolved.new"
local ip_lists="" domain_lists=""
for f in $lists ; do
if grep -v '^#' "$f" | grep -q '[a-zA-Z]' ; then
domain_lists="$domain_lists $f"
if [ -n "$need_resolve" ] ; then
if [ -n "$RESOLVE" ] ; then
log "[$name]$label Re-resolving..."
else
ip_lists="$ip_lists $f"
log "[$name]$label Changes detected, resolving..."
fi
done
vlog "[$name]$label ip_lists:$(echo $ip_lists | wc -w) domain_lists:$(echo $domain_lists | wc -w)"
{
for f in $ip_lists ; do
if [ "$ipcmd" = "ip -6" ] ; then
# IPv6 mode: only pass lines containing ":"
grep -v '^#' "$f" | grep -v '^$' | grep ':'
# Split: pure IP files go through cat, domain files through resolve
local ip_lists="" domain_lists=""
for f in $lists ; do
if grep -v '^#' "$f" | grep -q '[a-zA-Z]' ; then
domain_lists="$domain_lists $f"
else
grep -v '^#' "$f" | grep -v '^$'
ip_lists="$ip_lists $f"
fi
done
if [ -n "$domain_lists" ] ; then
for f in $domain_lists ; do
cat_expanded "$f"
done | grep -v '^#' | grep -v '^$' | $resolve_func | \
grep -v '^#' | grep -v '^$'
fi
} | sort -u > "$resolved_new"
vlog "[$name]$label ip_lists:$(echo $ip_lists | wc -w) domain_lists:$(echo $domain_lists | wc -w)"
{
for f in $ip_lists ; do
if [ "$ipcmd" = "ip -6" ] ; then
# IPv6 mode: only pass lines containing ":"
grep -v '^#' "$f" | grep -v '^$' | grep ':'
else
grep -v '^#' "$f" | grep -v '^$'
fi
done
if [ -n "$domain_lists" ] ; then
for f in $domain_lists ; do
cat_expanded "$f"
done | grep -v '^#' | grep -v '^$' | $resolve_func | \
grep -v '^#' | grep -v '^$'
fi
} | sort -u > "$resolved_new"
fi
# Check if resolved IPs actually changed
vlog "[$name]$label resolved $(wc -l < "$resolved_new") IPs, checking against saved state"
......@@ -462,6 +476,7 @@ process_routes()
# Save state
echo "$current_hash" > "$STATE_DIR/$state/hash"
echo "$lists_hash" > "$STATE_DIR/$state/lists_hash"
echo "$table" > "$STATE_DIR/$state/table"
cp "$gwdir/gateway" "$STATE_DIR/$state/gateway"
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