From b80d81a5cacc146b7bf7c92a716858d72cefb2be Mon Sep 17 00:00:00 2001 From: sinanmohd Date: Sun, 7 Jul 2024 10:46:24 +0530 Subject: dns: init --- dns/main.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 dns/main.go (limited to 'dns') diff --git a/dns/main.go b/dns/main.go new file mode 100644 index 0000000..b3adfca --- /dev/null +++ b/dns/main.go @@ -0,0 +1,54 @@ +package dns + +import ( + "log" + "net" + + "github.com/miekg/dns" +) + +type Dns struct { + server dns.Server + config *dns.ClientConfig +} + +func (d *Dns) ServeDNS(w dns.ResponseWriter, req *dns.Msg) { + var resp *dns.Msg + var err error + client := new(dns.Client) + + req.RecursionDesired = true; + for _, upstream := range d.config.Servers { + resp, _, err = client.Exchange(req, net.JoinHostPort(upstream, d.config.Port)) + if err == nil { + break + } + + log.Printf("dns query: %s", err) + } + + w.WriteMsg(resp) +} + +func New() (*Dns, error) { + var d Dns + var err error + + d.server = dns.Server{ + Net: "udp", + ReusePort: true, + Handler: &d, + } + + d.config, err = dns.ClientConfigFromFile("/etc/resolv.conf") + if err != nil { + log.Printf("reading resolve.conf: %s", err) + return nil, err + } + + return &d, nil +} + +func (d *Dns) Run() { + d.server.ListenAndServe() +} -- cgit v1.2.3