Skip to content

Instantly share code, notes, and snippets.

@CameronHall
Last active July 7, 2023 05:03
Show Gist options
  • Save CameronHall/fd9fbb193d76abd82d1fb61895761cd7 to your computer and use it in GitHub Desktop.
Save CameronHall/fd9fbb193d76abd82d1fb61895761cd7 to your computer and use it in GitHub Desktop.
Installation of dnsmasq macOS Ventura 13.3.1 (a). Requires sudo
#!/bin/bash
set -e
COLOUR_GREEN="\033[0;32m"
COLOUR_RED="\033[0;31m"
COLOUR_OFF='\033[0m'
function echo_success {
echo -e "${COLOUR_GREEN}$1${COLOUR_OFF}"
}
function echo_failure {
echo -e "${COLOUR_RED}$1${COLOUR_OFF}"
}
function install_dnsmasq {
if ! brew list dnsmasq &> /dev/null; then
echo "Installing dnsmasq"
brew install dnsmasq
fi
if ! grep "^conf-dir=$(brew --prefix)/etc/dnsmasq.d" $(brew --prefix)/etc/dnsmasq.conf 1> /dev/null; then
echo "conf-dir=$(brew --prefix)/etc/dnsmasq.d" >> $(brew --prefix)/etc/dnsmasq.conf
fi
if [ ! -d "$(brew --prefix)/etc/dnsmasq.d" ]; then
mkdir -p $(brew --prefix)/etc/dnsmasq.d
fi
if ! grep -R ".test" $(brew --prefix)/etc/dnsmasq.d 1> /dev/null; then
echo "address=/.test/127.0.0.1" >> $(brew --prefix)/etc/dnsmasq.d/test-tld
fi
sudo brew services start dnsmasq
echo "Configuring resolver"
if [ ! -d "/etc/resolver" ]; then
TEMP_DIR=`mktemp -d`
mkdir -p $TEMP_DIR/resolver
echo "nameserver 127.0.0.1" >> $TEMP_DIR/resolver/test
sudo cp -R "${TEMP_DIR}/resolver" /etc/
rm -rf $TEMP_DIR/resolver/
fi
dnsmasq_validate &> /dev/null
echo_success "Installed and validated dnsmasq."
}
function dnsmasq_uninstall {
if brew list dnsmasq &> /dev/null; then
echo "Stopping dnsmasq service."
sudo brew services stop dnsmasq &> /dev/null
echo "Uninstalling dnsmasq"
if ! brew remove dnsmasq &> /dev/null; then
echo_failure "Brew removal failed; manually removing..."
sudo rm -rf /opt/homebrew/Cellar/dnsmasq/*
fi
fi
echo "Cleaning up dnsmasq config"
rm -rf $(brew --prefix)/etc/dnsmasq.*
echo "Cleaning up resolver config"
sudo rm -rf /etc/resolver
echo_success "Uninstallation of dnsmasq completed successfully."
}
function dnsmasq_validate {
if ! dig +short +time=1 +tries=1 local.test @127.0.0.1 &> /dev/null; then
echo_failure "Local DNS is not responding to queries for *.test"
exit 1
fi
echo_success "Local DNS is responding to queries for *.test"
}
case "$1" in
install)
install_dnsmasq
;;
uninstall)
dnsmasq_uninstall
;;
validate)
dnsmasq_validate
;;
*)
echo "This script will install and configure dnsmasq to redirect all DNS look ups for .test domains to localhost.
One of the following arguments must be provided;
- install
- uninstall
- validate
"
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment