Skip to content

Instantly share code, notes, and snippets.

@guillaumerose
Created October 13, 2020 11:40
Show Gist options
  • Save guillaumerose/7b59481a9a56eec9f4c2c7918dc0333f to your computer and use it in GitHub Desktop.
Save guillaumerose/7b59481a9a56eec9f4c2c7918dc0333f to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"log"
"strconv"
"github.com/miekg/dns"
)
var records = map[string]string{
"test.service.": "192.168.0.2",
}
func parseQuery(m *dns.Msg) {
for _, q := range m.Question {
switch q.Qtype {
case dns.TypeA:
ip := records[q.Name]
if ip != "" {
rr, err := dns.NewRR(fmt.Sprintf("%s A %s", q.Name, ip))
if err == nil {
m.Answer = append(m.Answer, rr)
}
}
}
}
}
func handleDnsRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
m.Compress = false
switch r.Opcode {
case dns.OpcodeQuery:
parseQuery(m)
}
w.WriteMsg(m)
}
func main() {
// attach request handler func
dns.HandleFunc("service.", handleDnsRequest)
// start server
port := 5354
server := &dns.Server{Addr: ":" + strconv.Itoa(port), Net: "udp"}
log.Printf("Starting at %d\n", port)
err := server.ListenAndServe()
defer server.Shutdown()
if err != nil {
log.Fatalf("Failed to start server: %s\n ", err.Error())
}
}
$ cat input
test.service. A
// After
$ dnsperf -s 127.0.0.1 -p 5354 -l 15 < input
DNS Performance Testing Tool
Version 2.3.4
[Status] Command line: dnsperf -s 127.0.0.1 -p 5354 -l 15
[Status] Sending queries (to 127.0.0.1)
[Status] Started at: Tue Oct 13 13:37:37 2020
[Status] Stopping after 15.000000 seconds
[Status] Testing complete (time limit)
Statistics:
Queries sent: 1989032
Queries completed: 1989032 (100.00%)
Queries lost: 0 (0.00%)
Response codes: NOERROR 1989032 (100.00%)
Average packet size: request 30, response 58
Run time (s): 15.000419
Queries per second: 132598.429417
Average Latency (s): 0.000648 (min 0.000021, max 0.008631)
Latency StdDev (s): 0.000459
// Before
$ dnsperf -s 127.0.0.1 -p 5354 -l 15 < input
DNS Performance Testing Tool
Version 2.3.4
[Status] Command line: dnsperf -s 127.0.0.1 -p 5354 -l 15
[Status] Sending queries (to 127.0.0.1)
[Status] Started at: Tue Oct 13 13:38:45 2020
[Status] Stopping after 15.000000 seconds
[Status] Testing complete (time limit)
Statistics:
Queries sent: 1917456
Queries completed: 1917456 (100.00%)
Queries lost: 0 (0.00%)
Response codes: NOERROR 1917456 (100.00%)
Average packet size: request 30, response 58
Run time (s): 15.000557
Queries per second: 127825.653407
Average Latency (s): 0.000616 (min 0.000021, max 0.007693)
Latency StdDev (s): 0.000442
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment