a simple dns server in go that forwards queries to an upstream resolver
implements rfc 1035 dns protocol from scratch(learning project)
at its core, this IS a dns server. it speaks the dns protocol, parses queries, and returns responses. it has basic authoritative serving capabilities for localhost records, and can forward unknown queries.
- parses and responds to dns queries per rfc 1035
- handles dns message compression
- serves authoritative A records for localhost, localhost.localdomain, broadcasthost
- forwards unknown queries to upstream resolvers when configured
- returns NXDOMAIN for unknown queries when no resolver is configured
- uses atomic ids to prevent response collision
go build -o dns-server main.godirect mode (serves localhost records, NXDOMAIN for others):
./dns-serverforwarding mode (serves localhost records, forwards unknown queries):
./dns-server --resolver 8.8.8.8:53forwarding to cloudflare:
./dns-server --resolver 1.1.1.1:53testing forwarding mode:
# with dig
dig @127.0.0.1 -p 2053 google.com
# with nslookup
nslookup -port=2053 google.com 127.0.0.1testing localhost resolution:
# these should return 127.0.0.1
dig @127.0.0.1 -p 2053 localhost
dig @127.0.0.1 -p 2053 localhost.localdomaintesting direct mode (NXDOMAIN):
# this will return NXDOMAIN (domain not found)
dig @127.0.0.1 -p 2053 nonexistent.domain
# or test with any unknown domain in direct mode
dig @127.0.0.1 -p 2053 google.com- listens on
127.0.0.1:2053 - receives udp dns queries
- parses the query
- checks if domain is in local records (localhost, etc.)
- if local record found, returns authoritative answer
- if not local and resolver configured, forwards query
- if not local and no resolver, returns NXDOMAIN
- sends response back to client
forwarding mode:
dns server listening on 127.0.0.1:2053
forwarding mode: using resolver 8.8.8.8:53 for unknown domains
authoritative for: localhost, localhost.localdomain, broadcasthost
received 29 bytes from 127.0.0.1:54321
query id: 4660, questions: 1
forwarding query
sent response with 1 answers
direct mode:
dns server listening on 127.0.0.1:2053
direct mode: authoritative for localhost records, NXDOMAIN for others
serves: localhost, localhost.localdomain, broadcasthost
received 29 bytes from 127.0.0.1:54321
query id: 4660, questions: 1
sent response with 0 answers
do whatever you want with it