diff options
author | sinanmohd <sinan@sinanmohd.com> | 2024-07-07 10:46:24 +0530 |
---|---|---|
committer | sinanmohd <sinan@sinanmohd.com> | 2024-07-07 10:46:24 +0530 |
commit | b80d81a5cacc146b7bf7c92a716858d72cefb2be (patch) | |
tree | 8cfd8f259630bda02846db822b81d7c65550ed14 /dns | |
parent | 4e2671608059ca5d99586eab3ff61ef1da9b0d23 (diff) |
dns: init
Diffstat (limited to 'dns')
-rw-r--r-- | dns/main.go | 54 |
1 files changed, 54 insertions, 0 deletions
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() +} |