From 5c5411261a394efd7b6c868da3dfbe0e703fe38b Mon Sep 17 00:00:00 2001 From: pokamest Date: Thu, 13 May 2021 08:23:56 -0700 Subject: [PATCH] macos dns setup fixed --- client/configurators/openvpn_configurator.cpp | 7 ++ deploy/data/macos/update-resolv-conf.sh | 74 +++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100755 deploy/data/macos/update-resolv-conf.sh diff --git a/client/configurators/openvpn_configurator.cpp b/client/configurators/openvpn_configurator.cpp index ac93236f..da9974d5 100644 --- a/client/configurators/openvpn_configurator.cpp +++ b/client/configurators/openvpn_configurator.cpp @@ -225,6 +225,13 @@ QString OpenVpnConfigurator::processConfigWithLocalSettings(QString config) #ifdef Q_OS_MAC config.replace("block-outside-dns", ""); + QString dnsConf = QString( + "\nscript-security 2\n" + "up %1/update-resolv-conf.sh\n" + "down %1/update-resolv-conf.sh\n"). + arg(qApp->applicationDirPath()); + + config.append(dnsConf); #endif return config; diff --git a/deploy/data/macos/update-resolv-conf.sh b/deploy/data/macos/update-resolv-conf.sh new file mode 100755 index 00000000..cba62f77 --- /dev/null +++ b/deploy/data/macos/update-resolv-conf.sh @@ -0,0 +1,74 @@ +#!/bin/bash + +# Mac name-resolution updater based on @cl's script here: +# https://blog.netnerds.net/2011/10/openvpn-update-client-dns-on-mac-os-x-using-from-the-command-line/ +# Openvpn envar parsing taken from the script in debian's openvpn package. +# Smushed together and improved by @andrewgdotcom. + +# Parses DHCP options from openvpn to update resolv.conf +# To use set as 'up' and 'down' script in your openvpn *.conf: +# up /etc/openvpn/update-resolv-conf +# down /etc/openvpn/update-resolv-conf + +[ "$script_type" ] || exit 0 +[ "$dev" ] || exit 0 + +PATH=$PATH:/usr/sbin/ +NMSRVRS=() +SRCHS=() + +# Get adapter list +IFS=$'\n' read -d '' -ra adapters < <(networksetup -listallnetworkservices |grep -v denotes) || true + +split_into_parts() +{ + part1="$1" + part2="$2" + part3="$3" +} + +update_all_dns() +{ + for adapter in "${adapters[@]}" + do + echo updating dns for $adapter + # set dns server to the vpn dns server + if [[ "${SRCHS[@]}" ]]; then + networksetup -setsearchdomains "$adapter" "${SRCHS[@]}" + fi + if [[ "${NMSRVRS[@]}" ]]; then + networksetup -setdnsservers "$adapter" "${NMSRVRS[@]}" + fi + done +} + +clear_all_dns() +{ + for adapter in "${adapters[@]}" + do + echo updating dns for $adapter + networksetup -setdnsservers "$adapter" empty + networksetup -setsearchdomains "$adapter" empty + done +} + +case "$script_type" in + up) + for optionvarname in ${!foreign_option_*} ; do + option="${!optionvarname}" + echo "$option" + split_into_parts $option + if [ "$part1" = "dhcp-option" ] ; then + if [ "$part2" = "DNS" ] ; then + NMSRVRS=(${NMSRVRS[@]} $part3) + elif [ "$part2" = "DOMAIN" ] ; then + SRCHS=(${SRCHS[@]} $part3) + fi + fi + done + update_all_dns + ;; + down) + clear_all_dns + ;; +esac