Skip to content

Instantly share code, notes, and snippets.

@varenc
Last active October 22, 2023 11:50
Show Gist options
  • Save varenc/388fa10c68002e0d59c67d1b52492f79 to your computer and use it in GitHub Desktop.
Save varenc/388fa10c68002e0d59c67d1b52492f79 to your computer and use it in GitHub Desktop.
Temporarily bypass PiHole or DNSCrypt-proxy dns resolver on Mac
#####
# Temporarily bypass your ad blocking DNS server with this shell function. Automatically restore settings after some number of minutes.
#####
# This `dnsBypassTmp` zsh function will temporarily bypass your PiHole or DNSCrypt-proxy resolver.
# If a site is broken because of a domain you've blocked, this makes it easily to temporarily enabled that and get things working again.
#
# The only argument is the number of minutes to enable the bypass. Defaults to 2 minutes.
# example:
# $ dnsBypassTmp 5
#
#
# NOTES:
# Just add this to your ~/.zshrc to make this function available everywhere. Or wrap it in an executable script.
# Only works in macOS
# Only works in zsh (the new macOS default)
# All it does is use `networksetup` to change the dns server and then creates a sleeping background job to change it back.
# Notifies you when the timer is up using terminal-notifier if the util exists. Install it with `brew install terminal-notifier`. (Not required)
PrimaryNetService="Wi-Fi"
# What macOS calls your networking service. Usually "Wi-Fi" or "Ethernet". Check `networksetup -listallhardwareports`
# Or you can try to automatically determine your primary network service with the shell-fu below. (Note: Hardware Port and network device != Network Service)
#PrimaryNetService=$(networksetup -listnetworkserviceorder |grep -B1 "$(route get 1.1.1.1 | awk '/interface/ {print $2}')" | awk -F'\\) ' '/\([0-9]+\)/ {print $2}')
PrimaryResolver="127.0.0.1"
# This is your PiHole/DNSCrypt-proxy IP. Often its just "127.0.0.1" or localhost.
# Or set to "Empty" to use your default/DHCP assigned DNS resolver.
# Or just use `$(networksetup -getdnsservers $PrimaryNetService)` to make it whatever the last DNS server but be careful not to fetch that when a bypass is already running.
FallbackResolver="1.1.1.1"
# this is the fallback resolver to temporarily switch to. Can be your Gateway's DNS server or just use 1.1.1.1
dnsBypassTmp () {
# The only argument is the number of minutes to enable the bypass. Defaults to 2 minutes.
# example:
# dnsBypassTmp 5
TIME="${1:-2}"
OTHER_JOBS=$(pgrep -fl 'bash.*networksetup -setdnsserver.*Blow')
if [ $? -ne 1 ]
then
# TODO: Do this automatically in a safe way.
echo "=====\nWARNING: It looks like there's already a dnsBypassTmp timer running. Consider killing these with 'pkill -f \"bash.*networksetup -setdnsserver\"'"
echo $OTHER_JOBS "\n=====\n"
fi
networksetup -setdnsservers "$PrimaryNetService" $FallbackResolver
echo "Switching DNS to '$FallbackResolver'. Setting timer to switch back to '$PrimaryResolver' in $TIME minutes ( $(($TIME * 60)) seconds )"
(
nohup bash -c "sleep $(($TIME*60)); networksetup -setdnsservers \"$PrimaryNetService\" $PrimaryResolver; command -v terminal-notifier && terminal-notifier -message 'DNS filter re-enabled' -sound 'Blow' -s 'DNS UPDATE' -ignoreDnD" &
) 2> /dev/null
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment