Commit f650bde2 authored by Vitaly Lipatov's avatar Vitaly Lipatov

functions: check extra DNS availability before resolving

Add check_extra_dns() to probe external DNS (8.8.8.8) reachability. Skip it when unreachable to avoid timeouts. Include availability in route-update.sh hash so routes re-resolve when DNS comes back. Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent 43f7c03a
......@@ -43,18 +43,31 @@ resolve_gw()
# --- DNS resolution ---
# Check if external DNS is reachable. Sets EXTRA_DNS=IP or EXTRA_DNS=
# Include $EXTRA_DNS in hash so re-resolve triggers when availability changes
EXTRA_DNS_SERVER=8.8.8.8
check_extra_dns()
{
if dig @$EXTRA_DNS_SERVER +short +time=1 +tries=1 google.com A >/dev/null 2>&1 ; then
EXTRA_DNS=$EXTRA_DNS_SERVER
else
EXTRA_DNS=
fi
}
get_ipv4_list()
{
echo "$1" | grep -q "[a-z]" || return 0
dig "$1" A | grep -v "^;" | grep "IN[[:space:]]*A[[:space:]]" | sed -e "s|.*[[:space:]]||" | sort
dig @8.8.8.8 "$1" A | grep -v "^;" | grep "IN[[:space:]]*A[[:space:]]" | sed -e "s|.*[[:space:]]||" | sort
[ -n "$EXTRA_DNS" ] && dig @$EXTRA_DNS "$1" A | grep -v "^;" | grep "IN[[:space:]]*A[[:space:]]" | sed -e "s|.*[[:space:]]||" | sort
}
get_ipv6_list()
{
echo "$1" | grep -q "[a-z]" || return 0
dig "$1" AAAA | grep -v "^;" | grep "IN[[:space:]]*AAAA[[:space:]]" | sed -e "s|.*[[:space:]]||" | sort
dig @8.8.8.8 "$1" AAAA | grep -v "^;" | grep "IN[[:space:]]*AAAA[[:space:]]" | sed -e "s|.*[[:space:]]||" | sort
[ -n "$EXTRA_DNS" ] && dig @$EXTRA_DNS "$1" AAAA | grep -v "^;" | grep "IN[[:space:]]*AAAA[[:space:]]" | sed -e "s|.*[[:space:]]||" | sort
}
is_ipv4()
......@@ -122,11 +135,10 @@ get_ipv4_list_bulk()
[ -s "$domains" ] || return 0
# Bulk async resolve: local resolver + Google DNS
# -a: async, -Fi: inline parseable (answers+errors to stdout), -f: read from stdin
# Bulk async resolve: local resolver + Google DNS (if reachable)
{
adnshost -a -t a -Fi -f < "$domains"
adnshost -a -t a -Fi -f --config "nameserver 8.8.8.8" < "$domains"
[ -n "$EXTRA_DNS" ] && adnshost -a -t a -Fi -f --config "nameserver $EXTRA_DNS" < "$domains"
} > "$adns_out" 2>/dev/null
# Output resolved IPs grouped by domain
......@@ -166,10 +178,10 @@ get_ipv6_list_bulk()
[ -s "$domains" ] || return 0
# Bulk async resolve: local resolver + Google DNS
# Bulk async resolve: local resolver + Google DNS (if reachable)
{
adnshost -a -t aaaa -Fi -f < "$domains"
adnshost -a -t aaaa -Fi -f --config "nameserver 8.8.8.8" < "$domains"
[ -n "$EXTRA_DNS" ] && adnshost -a -t aaaa -Fi -f --config "nameserver $EXTRA_DNS" < "$domains"
} > "$adns_out" 2>/dev/null
# Output resolved IPs grouped by domain
......
......@@ -285,7 +285,7 @@ process_routes()
# 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=$( { echo "dns=$EXTRA_DNS"; cat "$gwdir/gateway" "$gwdir/options" $lists; } 2>/dev/null | md5sum | awk '{print $1}')
local need_reload= need_resolve=1
if [ -z "$FORCE" ] && [ -z "$RESOLVE" ] && [ -f "$STATE_DIR/$state/hash" ] ; then
......@@ -465,6 +465,9 @@ cleanup_state()
[ -n "$SHOW" ] && log "Dry-run mode (no changes will be made)"
[ -n "$VERBOSE" ] && log "Verbose mode, state_dir=$STATE_DIR"
check_extra_dns
[ -n "$EXTRA_DNS" ] && vlog "Extra DNS: $EXTRA_DNS" || log "Extra DNS ($EXTRA_DNS_SERVER) unreachable, local resolver only"
process_routes "$ROUTES_DIR" get_ipv4_list_bulk "ip" ""
process_routes "$ROUTES6_DIR" get_ipv6_list_bulk "ip -6" " (v6)"
cleanup_state
......
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