Skip to content

Commit

Permalink
dns timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
Place1 committed Jan 28, 2020
1 parent 090ace7 commit 08d5973
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions internal/services/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,20 @@ type DNSServer struct {
}

func NewDNSServer(upstream []string) (*DNSServer, error) {
logrus.Infof("starting dns server")

if len(upstream) == 0 {
upstream = []string{"1.1.1.1"}
}

logrus.Infof("starting dns server with upstreams: %v", upstream)

dnsServer := &DNSServer{
server: &dns.Server{
Addr: "0.0.0.0:53",
Net: "udp",
},
client: &dns.Client{
SingleInflight: true,
Timeout: 5 * time.Second,
},
cache: cache.New(10*time.Minute, 10*time.Minute),
upstream: upstream,
Expand All @@ -59,13 +60,15 @@ func (d *DNSServer) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
dns.HandleFailed(w, r)
}
}()

if logrus.GetLevel() == logrus.DebugLevel {
// log behind a condition to ensure we don't call prettyPrintMsg
// when the log level would filter out the message anyway
logrus.Debugf("dns query: %s", prettyPrintMsg(r))
}

switch r.Opcode {
case dns.OpcodeQuery:
if logrus.GetLevel() == logrus.DebugLevel {
// log behind a condition to ensure we don't call prettyPrintMsg
// when the log level would filter out the message anyway
logrus.Debugf("dns query: %s", prettyPrintMsg(r))
}
m, err := d.Lookup(r)
if err != nil {
logrus.Errorf("failed lookup record with error: %s\n%s", err.Error(), r)
Expand All @@ -74,8 +77,12 @@ func (d *DNSServer) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
}
m.SetReply(r)
w.WriteMsg(m)
return
default:
m := &dns.Msg{}
m.SetReply(r)
w.WriteMsg(m)
}

}

func (d *DNSServer) Lookup(m *dns.Msg) (*dns.Msg, error) {
Expand Down

0 comments on commit 08d5973

Please sign in to comment.