Skip to content

Commit d9213c3

Browse files
authored
local DNS server via dnsmasq (#2168)
* Use dnsmasq to create virtual hosts and mock DNS management for custom domains - dnsmasq docker image - dnsmasq network bridge - point *.sndev to 127.0.0.1 - set-dnsmasq script - -- add/remove/list dns records in dnsmasq.conf - add 'domains' to sndev - 'sndev domains dns' referencing set-dnsmasq script * restart dnsmasq if add/remove succeeded * add domain to /etc/hosts; cleanup * tell if the command needs sudo permission * add directions for dnsmasq DNS server usage * add --no-hosts flag to skip asking to edit /etc/hosts * add domains command to README.md * add dnsmasq instructions to README.md * correct exit on usage function; final cleanup and comments * portable bash; use default network for dnsmasq; set a version for dnsmasq image * POSIX compliance, add env var to .env.development, adjust README * ignore dnsmasq.conf edits, use template instead * use extra configs for dnsmasq, more POSIX compliance * fix --no-hosts flag recognition, light cleanup * shift 4 only if the command has enough args; more error messages; adjust TXT type only on list * different sed syntax for macOS
1 parent 8ba572d commit d9213c3

File tree

7 files changed

+397
-2
lines changed

7 files changed

+397
-2
lines changed

.env.development

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,9 @@ CPU_SHARES_IMPORTANT=1024
190190
CPU_SHARES_MODERATE=512
191191
CPU_SHARES_LOW=256
192192

193-
NEXT_TELEMETRY_DISABLED=1
193+
NEXT_TELEMETRY_DISABLED=1
194+
195+
# custom domains stuff
196+
# local DNS server for custom domain verification, by default it's dnsmasq.
197+
# reachable by containers on 172.30.0.2(:53), outside of docker with 0.0.0.0:5353
198+
DOMAINS_DNS_SERVER=172.30.0.2

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,7 @@ scripts/twitter-link-extract.config.json
7070
scripts/twitter-links.db
7171

7272
# pay-awards
73-
scripts/pay-awards.config.json
73+
scripts/pay-awards.config.json
74+
75+
# dnsmasq
76+
docker/dnsmasq/dnsmasq.d/*

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ COMMANDS
8787
psql open psql on db
8888
prisma run prisma commands
8989

90+
domains:
91+
domains custom domains dev management
92+
9093
dev:
9194
pr fetch and checkout a pr
9295
lint run linters
@@ -150,6 +153,17 @@ After `nlp-setup` is done, restart your containers to enable semantic search:
150153
> ./sndev restart
151154
```
152155

156+
#### Local DNS via dnsmasq
157+
158+
To enable dnsmasq:
159+
160+
- domains should be enabled in `COMPOSE_PROFILES`:
161+
162+
```.env
163+
COMPOSE_PROFILES=...,domains,...
164+
```
165+
166+
To add/remove DNS records you can now use `./sndev domains dns`. More on this [here](#add-or-remove-dns-records-in-local).
153167

154168
<br>
155169

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

450464
<br>
451465

466+
## Custom domains
467+
468+
### Add or remove DNS records in local
469+
470+
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).
471+
472+
To add a DNS record the syntax is the following:
473+
474+
`./sndev domains dns add|remove cname|txt <name/domain> <value>`
475+
476+
For TXT records, you can also use `""` quoted strings on `value`.
477+
478+
To list all DNS records present in the dnsmasq config: `./sndev domains dns list`
479+
480+
#### Access a local custom domain added via dnsmasq
481+
sndev will use the dnsmasq DNS server by default, but chances are that you might want to access the domain via your browser.
482+
483+
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.
484+
452485
# Internals
453486

454487
<br>

docker-compose.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ services:
119119
command:
120120
- npm run worker:dev
121121
cpu_shares: "${CPU_SHARES_IMPORTANT}"
122+
networks:
123+
- default
124+
- domains-network
122125
imgproxy:
123126
container_name: imgproxy
124127
image: darthsim/imgproxy:v3.23.0
@@ -806,6 +809,28 @@ services:
806809
CONNECT: "localhost:${LNBITS_WEB_PORT}"
807810
TORDIR: "/app/.tor"
808811
cpu_shares: "${CPU_SHARES_LOW}"
812+
dnsmasq:
813+
image: 4km3/dnsmasq:2.90-r3
814+
profiles:
815+
- domains
816+
container_name: dnsmasq
817+
restart: unless-stopped
818+
ports:
819+
- "5353:53/tcp"
820+
- "5353:53/udp"
821+
command:
822+
- --no-daemon
823+
- --address=/.sndev/127.0.0.1
824+
- --conf-file=/etc/dnsmasq.conf
825+
- --conf-dir=/etc/dnsmasq.d
826+
volumes:
827+
- ./docker/dnsmasq/dnsmasq.conf:/etc/dnsmasq.conf
828+
- ./docker/dnsmasq/dnsmasq.d:/etc/dnsmasq.d
829+
cpu_shares: "${CPU_SHARES_LOW}"
830+
networks:
831+
domains-network:
832+
ipv4_address: 172.30.0.2
833+
809834
volumes:
810835
db:
811836
os:
@@ -819,3 +844,13 @@ volumes:
819844
nwc_recv:
820845
tordata:
821846
eclair:
847+
dnsmasq:
848+
849+
networks:
850+
default: {}
851+
domains-network:
852+
name: domains-network
853+
driver: bridge
854+
ipam:
855+
config:
856+
- subnet: 172.30.0.0/24

docker/dnsmasq/dnsmasq.conf

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
server=1.1.1.1
2+
no-resolv
3+
bind-interfaces
4+
listen-address=0.0.0.0
5+
6+
log-queries
7+
log-facility=/var/log/dnsmasq.log
8+
9+
# example of cname and txt for custom domains verification
10+
# this is to be edited by sndev cli or manually
11+
cname=www.pizza.sndev,sn.sndev
12+
txt-record=_snverify.www.pizza.sndev,"EXAMPLE_TXT_VALUE"

0 commit comments

Comments
 (0)