Skip to content

0xDVC/dns-server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 

Repository files navigation

dns server

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.

what it does

  • 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

build

go build -o dns-server main.go

usage

direct mode (serves localhost records, NXDOMAIN for others):

./dns-server

forwarding mode (serves localhost records, forwards unknown queries):

./dns-server --resolver 8.8.8.8:53

forwarding to cloudflare:

./dns-server --resolver 1.1.1.1:53

testing

testing forwarding mode:

# with dig
dig @127.0.0.1 -p 2053 google.com

# with nslookup
nslookup -port=2053 google.com 127.0.0.1

testing 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.localdomain

testing 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

how it works

  1. listens on 127.0.0.1:2053
  2. receives udp dns queries
  3. parses the query
  4. checks if domain is in local records (localhost, etc.)
  5. if local record found, returns authoritative answer
  6. if not local and resolver configured, forwards query
  7. if not local and no resolver, returns NXDOMAIN
  8. sends response back to client

what you'll see

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

license

do whatever you want with it

About

minimal dns server written in go. based on rfc 1035

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages