wg-quick: darwin: restore DNS on down

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2018-05-16 03:09:33 +02:00
parent 9c18c70da6
commit 699777da8c

View file

@ -233,30 +233,42 @@ set_endpoint_direct_route() {
ENDPOINTS=( "${added[@]}" ) ENDPOINTS=( "${added[@]}" )
} }
set_dns() { declare -A SERVICE_DNS
# TODO: this should use scutil and be slightly more clever. But for now collect_new_service_dns() {
# we simply overwrite any _manually set_ DNS servers for all network # TODO: switch to scutil for all DNS modification
# services. This means we get into trouble if the user doesn't actually local service get_response
# want DNS via DHCP when setting this back to "empty". Because macOS is local -A found_services
# so horrible to deal with here, we'll simply wait for irate users to
# provide a patch themselves.
local service response
{ read -r _; while read -r service; do { read -r _; while read -r service; do
[[ $service == "*"* ]] && service="${service:1}" [[ $service == "*"* ]] && service="${service:1}"
found_services["$service"]=1
[[ -n ${SERVICE_DNS["$service"]} ]] && continue
get_response="$(cmd networksetup -getdnsservers "$service")"
[[ $get_response == *" "* ]] && get_response="Empty"
[[ -n $get_response ]] && SERVICE_DNS["$service"]="$get_response"
done; } < <(networksetup -listallnetworkservices)
for service in "${!SERVICE_DNS[@]}"; do
[[ ${found_services["$service"]} == 1 ]] || unset SERVICE_DNS["$service"]
done
}
set_dns() {
collect_new_service_dns
local service response
for service in "${!SERVICE_DNS[@]}"; do
while read -r response; do while read -r response; do
[[ $response == *Error* ]] && echo "$response" >&2 [[ $response == *Error* ]] && echo "$response" >&2
done < <(cmd networksetup -setdnsservers "$service" "${DNS[@]}") done < <(cmd networksetup -setdnsservers "$service" "${DNS[@]}")
done; } < <(networksetup -listallnetworkservices) done
} }
del_dns() { del_dns() {
local service response local service response
{ read -r _; while read -r service; do for service in "${!SERVICE_DNS[@]}"; do
[[ $service == "*"* ]] && service="${service:1}"
while read -r response; do while read -r response; do
[[ $response == *Error* ]] && echo "$response" >&2 [[ $response == *Error* ]] && echo "$response" >&2
done < <(cmd networksetup -setdnsservers "$service" Empty) done < <(cmd networksetup -setdnsservers "$service" ${SERVICE_DNS["$service"]} || true)
done; } < <(networksetup -listallnetworkservices) done
} }
monitor_daemon() { monitor_daemon() {