Skip to content

Instantly share code, notes, and snippets.

@imiric
Created August 10, 2020 14:37
Show Gist options
  • Save imiric/a7f467fc3b0a6a4807954ebd59ff0520 to your computer and use it in GitHub Desktop.
Save imiric/a7f467fc3b0a6a4807954ebd59ff0520 to your computer and use it in GitHub Desktop.
DNS lookup test with sdns
package main
import (
"context"
"fmt"
"log"
"net"
"os"
"time"
"github.com/davecgh/go-spew/spew"
"github.com/miekg/dns"
"github.com/semihalev/sdns/authcache"
sdnsc "github.com/semihalev/sdns/config"
sdns "github.com/semihalev/sdns/middleware/resolver"
)
var nameservers = []string{
"1.1.1.1:53",
"8.8.8.8:53",
}
func authServers() *authcache.AuthServers {
servers := &authcache.AuthServers{}
servers.Zone = "."
for _, ns := range nameservers {
host, _, _ := net.SplitHostPort(ns)
if ip := net.ParseIP(host); ip != nil {
servers.List = append(servers.List, authcache.NewAuthServer(ns, authcache.IPv4))
}
}
return servers
}
func resolverConfig() *sdnsc.Config {
cfg := new(sdnsc.Config)
cfg.RootServers = nameservers
cfg.Maxdepth = 30
cfg.Expire = 600
cfg.CacheSize = 1024
cfg.Timeout.Duration = 2 * time.Second
return cfg
}
func makeReq(hostname string) *dns.Msg {
req := new(dns.Msg)
req.SetQuestion(fmt.Sprintf("%s.", hostname), dns.TypeA)
req.RecursionDesired = true
return req
}
func main() {
if len(os.Args) != 2 {
log.Println("must provide hostname to lookup as argument")
os.Exit(1)
}
hostname := os.Args[1]
req := makeReq(hostname)
r := sdns.NewResolver(resolverConfig())
ctx := context.Background()
resp, err := r.Resolve(ctx, req, authServers(), false, 30, 0, false, nil)
if err != nil {
log.Println(err)
os.Exit(1)
}
for _, a := range resp.Answer {
spew.Dump(a)
}
}
@imiric
Copy link
Author

imiric commented Aug 10, 2020

Example output:

> go run lookup.go k6.io
(*dns.A)(0xc000206000)(k6.io.   60      IN      A       54.192.87.93)
(*dns.A)(0xc000206040)(k6.io.   60      IN      A       54.192.87.71)
(*dns.A)(0xc000206080)(k6.io.   60      IN      A       54.192.87.109)
(*dns.A)(0xc000206100)(k6.io.   60      IN      A       54.192.87.73)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment