mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-06 17:12:07 +08:00
fix(macos): preserve DNS and proxy state across network changes
This commit is contained in:
+50
-17
@@ -32,7 +32,9 @@ function is_valid_ip() {
|
||||
|
||||
# Prints one value of a SystemConfiguration dictionary, or nothing when the key is absent.
|
||||
function sc_value() {
|
||||
printf 'show %s\nquit\n' "$1" | scutil | sed -n "s/^ *$2 : //p"
|
||||
local dictionary
|
||||
dictionary=$(printf 'show %s\nquit\n' "$1" | scutil) || return 1
|
||||
sed -n "s/^ *$2 : //p" <<<"$dictionary"
|
||||
}
|
||||
|
||||
# Prints the service's DNS servers, or "empty" when none is set; fails on any other answer.
|
||||
@@ -50,31 +52,62 @@ function dns_of() {
|
||||
echo "$dns"
|
||||
}
|
||||
|
||||
[ $# -lt 1 ] && echo "Usage: $0 <IP address>" && exit 1
|
||||
! is_valid_ip "$1" && echo "$1 is not a valid IP address." && exit 1
|
||||
[ $# -lt 1 ] && echo "Usage: $0 <newline-separated DNS servers> [legacy state directory]" >&2 && exit 1
|
||||
dns_servers=()
|
||||
while read -r server; do
|
||||
! is_valid_ip "$server" && echo "$server is not a valid IP address." >&2 && exit 1
|
||||
dns_servers+=("$server")
|
||||
done <<<"$1"
|
||||
|
||||
# Backups are keyed by service ID, which survives renaming the service and renumbering its interface.
|
||||
service_id=$(sc_value State:/Network/Global/IPv4 PrimaryService)
|
||||
hardware_port=$(sc_value "Setup:/Network/Service/$service_id" UserDefinedName)
|
||||
[ -z "$hardware_port" ] && echo "No primary network service." && exit 1
|
||||
service_id=$(sc_value State:/Network/Global/IPv4 PrimaryService) || exit 1
|
||||
hardware_port=$(sc_value "Setup:/Network/Service/$service_id" UserDefinedName) || exit 1
|
||||
[ -z "$hardware_port" ] && echo "No primary network service; waiting for the network." >&2 && exit 75
|
||||
|
||||
original_dns=$(dns_of "$hardware_port") || { echo "Cannot read DNS servers of $hardware_port." >&2; exit 1; }
|
||||
|
||||
backup=".original_dns/$service_id"
|
||||
mkdir -p .original_dns || exit 1
|
||||
# Older releases kept a single backup for whichever service was primary.
|
||||
if [ -f .original_dns.txt ]; then
|
||||
if [ -f "$backup" ]; then
|
||||
rm -f .original_dns.txt || exit 1
|
||||
else
|
||||
mv .original_dns.txt "$backup" || exit 1
|
||||
# Key old single-slot backups before import so a later network switch cannot reassign them.
|
||||
for state_dir in "$PWD" "${2:-$PWD}"; do
|
||||
if [ -f "$state_dir/.original_dns.txt" ]; then
|
||||
mkdir -p "$state_dir/.original_dns" || exit 1
|
||||
if [ -f "$state_dir/.original_dns/$service_id" ]; then
|
||||
rm -f "$state_dir/.original_dns.txt" || exit 1
|
||||
else
|
||||
mv "$state_dir/.original_dns.txt" "$state_dir/.original_dns/$service_id" || exit 1
|
||||
fi
|
||||
fi
|
||||
done
|
||||
if [ ! -f "$backup" ] && [ -n "$2" ] && [ -f "$2/$backup" ]; then
|
||||
cp "$2/$backup" ".original_dns/.$service_id" || exit 1
|
||||
mv ".original_dns/.$service_id" "$backup" || exit 1
|
||||
fi
|
||||
backup_header='# clash-verge-dns-managed'
|
||||
function save_backup() {
|
||||
printf '%s\n%s\n' "$backup_header" "$1" >".original_dns/.$service_id" &&
|
||||
mv ".original_dns/.$service_id" "$backup"
|
||||
}
|
||||
|
||||
saved_dns=
|
||||
legacy_backup=false
|
||||
if [ -f "$backup" ]; then
|
||||
saved_dns=$(cat "$backup") || exit 1
|
||||
case "$saved_dns" in
|
||||
"$backup_header"$'\n'*) saved_dns=${saved_dns#*$'\n'} ;;
|
||||
*) legacy_backup=true ;;
|
||||
esac
|
||||
fi
|
||||
# The placeholder is ours, not the user's: keep the backup taken before it was applied.
|
||||
if [ "$original_dns" != "$1" ]; then
|
||||
echo "$original_dns" >"$backup" || exit 1
|
||||
networksetup -setdnsservers "$hardware_port" "$1"
|
||||
# Only an unmarked historical backup owns the old single-address placeholder.
|
||||
if [ "$original_dns" != "114.114.114.114" ] || [ "$legacy_backup" != true ]; then
|
||||
save_backup "$original_dns" || exit 1
|
||||
legacy_backup=false
|
||||
fi
|
||||
networksetup -setdnsservers "$hardware_port" "${dns_servers[@]}"
|
||||
[ "$(dns_of "$hardware_port")" = "$1" ] || { echo "Failed to set DNS for $hardware_port." >&2; exit 1; }
|
||||
elif [ ! -f "$backup" ]; then
|
||||
echo "empty" >"$backup" || exit 1
|
||||
fi
|
||||
# Keep legacy ownership until the new list is verified, including a retry after a failed marker write.
|
||||
if [ "$legacy_backup" = true ]; then
|
||||
save_backup "$saved_dns" || exit 1
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user