-
-
Notifications
You must be signed in to change notification settings - Fork 131
local DNS server via dnsmasq #2168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 10 commits
4ccdbe4
5bdcee3
686575c
17905d4
ab36a4d
138ff90
60184cf
e1a0bed
0bb151c
b6b9501
02f2bf9
d8d0137
37222d8
43256b3
bd8c447
5daebaf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
server=1.1.1.1 | ||
no-resolv | ||
bind-interfaces | ||
listen-address=0.0.0.0 | ||
|
||
log-queries | ||
log-facility=/var/log/dnsmasq.log | ||
|
||
# example of cname and txt for custom domains verification | ||
# this is to be edited by sndev cli or manually | ||
cname=www.pizza.sndev,sn.sndev | ||
txt-record=_snverify.www.pizza.sndev,"EXAMPLE_TXT_VALUE" |
Soxasora marked this conversation as resolved.
Show resolved
Hide resolved
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,244 @@ | ||
#!/usr/bin/env bash | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We try to make sure This isn't a dealbreaker, but some folks will probably have issues running these dns commands. You can test your POSIX compliance with shellcheck. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you! Turned out that the sudo problem you were facing was because of SC2024, didn't notice it on my config, sorry for the overlook ^^ |
||
# Script to handle local DNS testing via dnsmasq | ||
# supports add/remove/list of CNAME and TXT records | ||
# on dnsmasq config changes it will restart the dnsmasq container | ||
# it also asks to add the record to the /etc/hosts file if --no-hosts is not used | ||
|
||
# prep | ||
set -e | ||
DNSMASQ_CONF_PATH="./docker/dnsmasq/dnsmasq.conf" | ||
INTENT=$1 # add, remove, list | ||
TYPE=$2 # cname, txt | ||
NAME=$3 # www.pizza.com | ||
VALUE=$4 # stacker.news or "7vfyvQO...vMALqvqkTQ=" | ||
NO_HOSTS=false # handled via --no-hosts flag | ||
|
||
# check if running on Windows | ||
# this script doesn't support Windows /etc/hosts editing | ||
IS_WINDOWS=false | ||
if [[ "$OSTYPE" == "msys" ]] || [[ "$OSTYPE" == "cygwin" ]] || [[ "$OSTYPE" == "win32" ]]; then | ||
IS_WINDOWS=true | ||
fi | ||
|
||
# handle flags | ||
while [ $# -gt 0 ]; do | ||
case "$1" in | ||
--no-hosts) | ||
NO_HOSTS=true | ||
shift | ||
;; | ||
-h|--help) | ||
usage | ||
exit 0 | ||
;; | ||
*) | ||
break | ||
;; | ||
esac | ||
done | ||
|
||
# general usage | ||
usage() { | ||
cat <<EOF | ||
Set mock DNS records for custom domains in dnsmasq conf. | ||
Use .sndev domains as they automatically resolve to 127.0.0.1. | ||
|
||
USAGE | ||
$ sndev domains dns [COMMAND] | ||
|
||
COMMANDS | ||
add <cname|txt> <name> <value> [--no-hosts] | ||
remove <cname|txt> <name> <value> [--no-hosts] | ||
list <cname|txt> | ||
|
||
FLAGS | ||
--no-hosts Skip asking to add/remove record to /etc/hosts | ||
Useful if you're using dnsmasq [127.0.0.1:5353] as your DNS server. | ||
|
||
EXAMPLES | ||
$ sndev domains dns add cname www.pizza.sndev sn.sndev | ||
$ sndev domains dns remove txt _snverify.www.pizza.sndev "7vfyvQO...vMALqvqkTQ" | ||
$ sndev domains dns list cname|txt | ||
EOF | ||
exit 1 | ||
} | ||
|
||
# stop if dnsmasq config file is not found | ||
if [ ! -f "$DNSMASQ_CONF_PATH" ]; then | ||
echo "error: dnsmasq config file not found at $DNSMASQ_CONF_PATH" | ||
exit 1 | ||
fi | ||
|
||
# if we're adding or removing a record, we need to check for required args | ||
if [ "$INTENT" == "add" ] || [ "$INTENT" == "remove" ]; then | ||
if [ $# -lt 4 ]; then | ||
usage | ||
fi | ||
# if we're listing records, we need to have at least the TYPE arg | ||
elif [ $# -lt 2 ]; then | ||
usage | ||
fi | ||
|
||
# creates a line compatible with dnsmasq config file | ||
if [ "$TYPE" = "cname" ]; then | ||
LINE="cname=${NAME},${VALUE}" | ||
elif [ "$TYPE" = "txt" ]; then | ||
LINE="txt-record=${NAME},\"${VALUE//\"/\\\"}\"" | ||
else | ||
echo "Invalid record type: $TYPE" | ||
usage | ||
fi | ||
|
||
# adjust TXT TYPE for dnsmasq | ||
if [ "$TYPE" = "txt" ]; then | ||
TYPE="txt-record" | ||
fi | ||
|
||
# add a record to the dnsmasq config | ||
add_record() { | ||
# check if the record already exists | ||
if grep -Fxq "$LINE" "$DNSMASQ_CONF_PATH"; then | ||
echo "Record already exists: $LINE" | ||
exit 1 | ||
fi | ||
|
||
# add the record to the dnsmasq config | ||
echo "Adding record: $LINE" | ||
echo -e "\n$LINE" >> "$DNSMASQ_CONF_PATH" | ||
|
||
# if we're adding a CNAME record and --no-hosts is not used, we need to ask to add the record to the /etc/hosts file | ||
if [ "$TYPE" = "cname" ] && [ "$NO_HOSTS" = false ]; then | ||
# dnsmasq pamphlet | ||
echo -e " | ||
While sndev will use dnsmasq DNS server, your system won't use it by default. | ||
You can either manually point DNS to 127.0.0.1:5353 to access it system-wide, | ||
or add this record to /etc/hosts to access it via browser. | ||
" | ||
# ask to add the record to the /etc/hosts file | ||
read -p "[sudo] Do you want to add '127.0.0.1 $NAME' to /etc/hosts? [y/N] " response | ||
if [[ "$response" =~ ^[Yy]$ ]]; then | ||
# add the record to the /etc/hosts file | ||
add_record_to_hosts "$NAME" | ||
# if the record wasn't added, we need to inform the user | ||
if [ $? -ne 0 ]; then | ||
echo "/etc/hosts hasn't been touched." | ||
fi | ||
fi | ||
fi | ||
echo "Done." | ||
exit 0 | ||
} | ||
|
||
# remove a record from the dnsmasq config | ||
remove_record() { | ||
# check if the record exists | ||
if ! grep -Fxq "$LINE" "$DNSMASQ_CONF_PATH"; then | ||
echo "Can't find record: $LINE" | ||
echo "The record may have been removed or the name/value is incorrect or incomplete." | ||
echo "Use 'sndev domains dns list' to see all records." | ||
exit 1 | ||
fi | ||
|
||
# remove the record from the dnsmasq config | ||
echo "Removing record: $LINE" | ||
sed -i "/^$LINE$/d" "$DNSMASQ_CONF_PATH" | ||
|
||
# if we're removing a CNAME record and --no-hosts is not used, we need to ask to remove the record from the /etc/hosts file | ||
if [ "$TYPE" = "cname" ] && [ "$NO_HOSTS" = false ]; then | ||
# ask to remove the record from the /etc/hosts file | ||
read -p "[sudo] Do you want to remove this record from /etc/hosts? [y/N] " response | ||
if [[ "$response" =~ ^[Yy]$ ]]; then | ||
# remove the record from the /etc/hosts file | ||
remove_record_from_hosts "$NAME" | ||
# if the record wasn't removed, we need to inform the user | ||
if [ $? -ne 0 ]; then | ||
echo "/etc/hosts hasn't been touched." | ||
fi | ||
fi | ||
fi | ||
echo "Done." | ||
exit 0 | ||
} | ||
|
||
# list all records of a given type | ||
list_records() { | ||
grep "^$TYPE=" "$DNSMASQ_CONF_PATH" || echo "No $TYPE records found." | ||
} | ||
|
||
# add a record to the /etc/hosts file | ||
add_record_to_hosts() { | ||
local domain="$1" | ||
|
||
# this script doesn't support Windows /etc/hosts editing | ||
if [ "$IS_WINDOWS" = true ]; then | ||
echo "Adding records to /etc/hosts via this script is not supported on Windows" | ||
return 1 | ||
fi | ||
|
||
# check if the record already exists in the /etc/hosts file | ||
check_record_hosts_exists $domain | ||
if [ $? -eq 0 ]; then | ||
echo "Record already exists in /etc/hosts: $domain" | ||
return 1 | ||
fi | ||
|
||
# add the record to the /etc/hosts file | ||
echo "Adding record to /etc/hosts: $domain" | ||
echo "This operation requires sudo privileges" | ||
if ! sudo -n true 2>/dev/null; then | ||
sudo echo "127.0.0.1 $domain" >> "/etc/hosts" || return 1 | ||
else | ||
sudo echo "127.0.0.1 $domain" >> "/etc/hosts" | ||
fi | ||
echo "$domain added to /etc/hosts." | ||
echo "You can now access http://$domain:3000 via browser." | ||
return 0 | ||
} | ||
|
||
remove_record_from_hosts() { | ||
local domain="$1" | ||
|
||
# this script doesn't support Windows /etc/hosts editing | ||
if [ "$IS_WINDOWS" = true ]; then | ||
echo "Removing records from /etc/hosts via this script is not supported on Windows" | ||
return 1 | ||
fi | ||
|
||
# check if the record exists in the /etc/hosts file | ||
check_record_hosts_exists $domain | ||
if [ $? -eq 1 ]; then | ||
echo "Record not found in /etc/hosts: $domain" | ||
return 1 | ||
fi | ||
|
||
# remove the record from the /etc/hosts file | ||
echo "Removing record from /etc/hosts: $domain" | ||
echo "This operation requires sudo privileges" | ||
if ! sudo -n true 2>/dev/null; then | ||
sudo sed -i "/^127.0.0.1 $domain$/d" "/etc/hosts" || return 1 | ||
else | ||
sudo sed -i "/^127.0.0.1 $domain$/d" "/etc/hosts" | ||
fi | ||
echo "$domain removed from /etc/hosts." | ||
return 0 | ||
} | ||
|
||
# check if a record exists in the /etc/hosts file | ||
check_record_hosts_exists() { | ||
local domain="$1" | ||
|
||
# grep for the record | ||
if grep -Fxq "127.0.0.1 $domain" "/etc/hosts"; then | ||
return 0 | ||
fi | ||
return 1 | ||
} | ||
|
||
# switch intents | ||
case "$INTENT" in | ||
add) add_record ;; | ||
remove) remove_record ;; | ||
list) list_records ;; | ||
*) usage ;; | ||
esac | ||
|
Uh oh!
There was an error while loading. Please reload this page.