Skip to content

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

Merged
merged 16 commits into from
May 21, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ COMMANDS
psql open psql on db
prisma run prisma commands

domains:
domains custom domains dev management

dev:
pr fetch and checkout a pr
lint run linters
Expand Down Expand Up @@ -150,6 +153,25 @@ After `nlp-setup` is done, restart your containers to enable semantic search:
> ./sndev restart
```

#### Local DNS via dnsmasq

To enable dnsmasq:

- domains should be enabled in `COMPOSE_PROFILES`:

```.env
COMPOSE_PROFILES=...,domains,...
```

If you're working with custom domains:

- make sure that the following ENV variable is present:

```.env.development
DOMAINS_DNS_SERVER=172.20.0.2:53 # points to dnsmasq container
```

To add/remove DNS records you can now use `./sndev domains dns`. More on this [here](#add-or-remove-dns-records-in-local).

<br>

Expand Down Expand Up @@ -449,6 +471,25 @@ To enable Web Push locally, you will need to set the `VAPID_*` env vars. `VAPID_

<br>

## Custom domains

### Add or remove DNS records in local

A worker dedicated to verifying custom domains, checks, among other things, if a domain has the correct DNS records and values. This would normally require a real domain and access to its DNS configuration. Therefore we use dnsmasq to have local DNS, make sure you have [enabled it](#local-dns-via-dnsmasq).

To add a DNS record the syntax is the following:

`./sndev domains dns add|remove cname|txt <name/domain> <value>`

For TXT records, you can also use `""` quoted strings on `value`.

To list all DNS records present in the dnsmasq config: `./sndev domains dns list`

#### Access a local custom domain added via dnsmasq
sndev will use the dnsmasq DNS server by default, but chances are that you might want to access the domain via your browser.

For every edit on dnsmasq, it will give you the option to either edit the `/etc/hosts` file or use the dnsmasq DNS server which can be reached on `127.0.0.1:5353`. You can avoid getting asked to edit the `/etc/hosts` file by adding the `--no-hosts` parameter.

# Internals

<br>
Expand Down
32 changes: 32 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ services:
command:
- npm run worker:dev
cpu_shares: "${CPU_SHARES_IMPORTANT}"
networks:
- default
- domains-network
imgproxy:
container_name: imgproxy
image: darthsim/imgproxy:v3.23.0
Expand Down Expand Up @@ -806,6 +809,25 @@ services:
CONNECT: "localhost:${LNBITS_WEB_PORT}"
TORDIR: "/app/.tor"
cpu_shares: "${CPU_SHARES_LOW}"
dnsmasq:
image: 4km3/dnsmasq:2.90-r3
profiles:
- domains
container_name: dnsmasq
restart: unless-stopped
ports:
- "5353:53/tcp"
- "5353:53/udp"
command:
- --no-daemon
- --address=/.sndev/127.0.0.1
volumes:
- ./docker/dnsmasq/dnsmasq.conf:/etc/dnsmasq.conf
cpu_shares: "${CPU_SHARES_LOW}"
networks:
domains-network:
ipv4_address: 172.30.0.2

volumes:
db:
os:
Expand All @@ -819,3 +841,13 @@ volumes:
nwc_recv:
tordata:
eclair:
dnsmasq:

networks:
default: {}
domains-network:
name: domains-network
driver: bridge
ipam:
config:
- subnet: 172.30.0.0/24
12 changes: 12 additions & 0 deletions docker/dnsmasq/dnsmasq.conf
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"
244 changes: 244 additions & 0 deletions scripts/set-dnsmasq
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
#!/usr/bin/env bash
Copy link
Member

@huumn huumn May 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We try to make sure sndev scripts conform to POSIX shell so that we don't have to assume which shell a contributor has.

This isn't a dealbreaker, but some folks will probably have issues running these dns commands.

You can test your POSIX compliance with shellcheck.

Copy link
Member Author

Choose a reason for hiding this comment

The 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

Loading