本文整理匯總了Golang中github.com/miekg/dns.HandleFailed函數的典型用法代碼示例。如果您正苦於以下問題:Golang HandleFailed函數的具體用法?Golang HandleFailed怎麽用?Golang HandleFailed使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了HandleFailed函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: ProxyDNS
// ProxyDNS returns a handler for a proxy DNS server.
func ProxyDNS(server *app.App) func(w dns.ResponseWriter, req *dns.Msg) {
return func(w dns.ResponseWriter, req *dns.Msg) {
if len(req.Question) == 0 {
dns.HandleFailed(w, req)
return
}
name := req.Question[0].Name
record, err := models.FindRecordBySubOfFQDN(server.DB, name)
if err != nil || record.ID == "" {
dns.HandleFailed(w, req)
return
}
if record.Blacklist {
dns.HandleFailed(w, req)
return
}
transport := "udp"
if _, ok := w.RemoteAddr().(*net.TCPAddr); ok {
transport = "tcp"
}
c := &dns.Client{Net: transport}
resp, _, err := c.Exchange(req, record.HandlerHost+":"+strconv.Itoa(record.HandlerPort))
if err != nil {
dns.HandleFailed(w, req)
return
}
if err := w.WriteMsg(resp); err != nil {
dns.HandleFailed(w, req)
return
}
}
}
示例2: proxy
func proxy(addr string, w dns.ResponseWriter, req *dns.Msg) {
transport := "udp"
if _, ok := w.RemoteAddr().(*net.TCPAddr); ok {
transport = "tcp"
}
if isTransfer(req) {
if transport != "tcp" {
dns.HandleFailed(w, req)
return
}
t := new(dns.Transfer)
c, err := t.In(req, addr)
if err != nil {
dns.HandleFailed(w, req)
return
}
if err = t.Out(w, req, c); err != nil {
dns.HandleFailed(w, req)
return
}
return
}
c := &dns.Client{Net: transport}
resp, _, err := c.Exchange(req, addr)
if err != nil {
dns.HandleFailed(w, req)
return
}
w.WriteMsg(resp)
}
示例3: handleDNSInternal
func (s *DNS) handleDNSInternal(w dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
if q.Qtype == dns.TypeA && q.Qclass == dns.ClassINET {
if record, ok := s.cache.Get(q.Name); ok {
log.Printf("Found internal record for %s", q.Name)
m := new(dns.Msg)
m.SetReply(req)
rr_header := dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 0,
}
a := &dns.A{rr_header, net.ParseIP(record.ip)}
m.Answer = append(m.Answer, a)
w.WriteMsg(m)
return
}
log.Printf("No internal record found for %s", q.Name)
dns.HandleFailed(w, req)
}
log.Printf("Only handling type A requests, skipping")
dns.HandleFailed(w, req)
}
示例4: handle
func (h *Handler) handle(proto string, w dns.ResponseWriter, r *dns.Msg) {
ques := question.NewQuestion(r.Question[0])
subnet := ""
if ip, ok := w.RemoteAddr().(*net.UDPAddr); ok {
subnet = networks.Find(ip.IP)
}
if ip, ok := w.RemoteAddr().(*net.TCPAddr); ok {
subnet = networks.Find(ip.IP)
}
if subnet == "" {
dns.HandleFailed(w, r)
return
}
if ques.IsIpQuery && ques.TopDomain == "vpn" {
msg, err := h.reslvr.LookupUser(proto, ques, subnet, r)
if err != nil {
dns.HandleFailed(w, r)
return
}
w.WriteMsg(msg)
} else {
servers := database.DnsServers[subnet]
res, err := h.reslvr.Lookup(proto, servers, r)
if err != nil {
dns.HandleFailed(w, r)
return
}
w.WriteMsg(res)
}
}
示例5: NewServer
// NewServer creates a new Server with the given options.
func NewServer(o Options) (*Server, error) {
if err := o.validate(); err != nil {
return nil, err
}
s := Server{
c: &dns.Client{},
s: &dns.Server{
Net: o.Net,
Addr: o.Bind,
},
white: o.Whitelist != "",
hosts: hosts{},
hostsRX: hostsRX{},
privateHosts: map[string]struct{}{},
privateHostsRX: map[string]*regexp.Regexp{},
}
hostListPath := o.Whitelist
if hostListPath == "" {
hostListPath = o.Blacklist
}
s.hostsFile.path = hostListPath
if err := s.loadHostEntries(); err != nil {
return nil, err
}
if o.Poll != 0 {
go s.monitorHostEntries(o.Poll)
}
s.s.Handler = dns.HandlerFunc(func(w dns.ResponseWriter, r *dns.Msg) {
// If no upstream proxy is present, drop the query:
if len(o.Resolve) == 0 {
dns.HandleFailed(w, r)
return
}
// Filter Questions:
if r.Question = s.filter(r.Question); len(r.Question) == 0 {
w.WriteMsg(r)
return
}
// Proxy Query:
for _, addr := range o.Resolve {
in, _, err := s.c.Exchange(r, addr)
if err != nil {
continue
}
w.WriteMsg(in)
return
}
dns.HandleFailed(w, r)
})
return &s, nil
}
示例6: handleReverseDNSLookup
func (s *DNS) handleReverseDNSLookup(w dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
if q.Qtype == dns.TypePTR && q.Qclass == dns.ClassINET {
if record, ok := s.cache.Get(q.Name); ok {
log.Printf("Found internal record for %s", q.Name)
m := new(dns.Msg)
m.SetReply(req)
rr_header := dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypePTR,
Class: dns.ClassINET,
Ttl: 0,
}
a := &dns.PTR{rr_header, record.fqdn}
m.Answer = append(m.Answer, a)
w.WriteMsg(m)
return
}
log.Printf("Forwarding request to external recursor for: %s", q.Name)
// Forward the request
s.handleDNSExternal(w, req)
}
dns.HandleFailed(w, req)
}
示例7: handleDNSExternal
func (s *DNS) handleDNSExternal(w dns.ResponseWriter, req *dns.Msg) {
network := "udp"
if _, ok := w.RemoteAddr().(*net.TCPAddr); ok {
network = "tcp"
}
c := &dns.Client{Net: network}
var r *dns.Msg
var err error
for _, recursor := range s.recursors {
if recursor == "" {
log.Printf("Found empty recursor")
continue
}
log.Printf("Forwarding request to external recursor for: %s", req.Question[0].Name)
r, _, err = c.Exchange(req, recursor)
if err == nil {
if err := w.WriteMsg(r); err != nil {
log.Printf("DNS lookup failed %v", err)
}
return
}
}
dns.HandleFailed(w, req)
}
示例8: do
func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
Q := Question{UnFqdn(q.Name), dns.TypeToString[q.Qtype], dns.ClassToString[q.Qclass]}
Debug("Question: %s", Q.String())
// Query hosts
if settings.Hosts.Enable && h.isIPQuery(q) {
if ip, ok := h.hosts.Get(Q.qname); ok {
m := new(dns.Msg)
m.SetReply(req)
rr_header := dns.RR_Header{Name: q.Name, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: settings.Hosts.TTL}
a := &dns.A{rr_header, net.ParseIP(ip)}
m.Answer = append(m.Answer, a)
w.WriteMsg(m)
Debug("%s found in hosts", Q.qname)
return
}
}
// Only query cache when qtype == 'A' , qclass == 'IN'
key := KeyGen(Q)
if h.isIPQuery(q) {
mesg, err := h.cache.Get(key)
if err != nil {
Debug("%s didn't hit cache: %s", Q.String(), err)
} else {
Debug("%s hit cache", Q.String())
mesg.Id = req.Id
w.WriteMsg(mesg)
return
}
}
mesg, err := h.resolver.Lookup(Net, req)
if err != nil {
Debug("%s", err)
dns.HandleFailed(w, req)
return
}
w.WriteMsg(mesg)
if h.isIPQuery(q) {
err = h.cache.Set(key, mesg)
if err != nil {
Debug("Set %s cache failed: %s", Q.String(), err.Error())
}
Debug("Insert %s into cache", Q.String())
}
}
示例9: recurse
func (s *Server) recurse(w dns.ResponseWriter, req *dns.Msg) {
if s.recurseTo == "" {
dns.HandleFailed(w, req)
return
}
c := new(dns.Client)
in, _, err := c.Exchange(req, s.recurseTo)
if err == nil {
if in.MsgHdr.Truncated {
c.Net = "tcp"
in, _, err = c.Exchange(req, s.recurseTo)
}
w.WriteMsg(in)
return
}
log.Warnf("Recursive error: %+v", err)
dns.HandleFailed(w, req)
}
示例10: handleRequest
func handleRequest(w dns.ResponseWriter, r *dns.Msg) {
kv := llog.KV{}
// Can be nil during testing
if raddr := w.RemoteAddr(); raddr != nil {
kv["srcAddr"] = raddr.String()
}
if err := validateRequest(r); err != nil {
kv["err"] = err
llog.Warn("invalid request", kv)
sendFormatError(w, r)
return
}
start := time.Now()
kv["question"] = r.Question[0].Name
kv["questionType"] = r.Question[0].Qtype
llog.Info("handling request", kv)
rr := NewReq{r, make(chan *dns.Msg)}
newCh <- rr
m := <-rr.ReplyCh
kv["ms"] = int64(time.Since(start).Nanoseconds() / 1e6)
if m == nil {
llog.Warn("error handling request", kv)
dns.HandleFailed(w, r)
return
}
//we need to make sure the sent ID matches the replied one
//it might be different if we combined in-flight messages
//copy the struct so we don't affect anything else handling this
m2 := *m
m2.Id = r.Id
m = &m2
//we always want to compress since there's no downsides afaik
m.Compress = true
kv["rcode"], _ = dns.RcodeToString[m.Rcode]
if len(m.Answer) > 0 {
kv["answer"] = m.Answer[0]
kv["answerCnt"] = len(m.Answer)
}
kv["len"] = m.Len()
llog.Info("responding to request", kv)
err := w.WriteMsg(m)
if err != nil {
kv["err"] = err
llog.Warn("error writing response", kv)
//no need to handle HandleFailed here because we cannot write
}
}
示例11: sendTruncated
func sendTruncated(w dns.ResponseWriter, msgHdr dns.MsgHdr) {
emptyResp := new(dns.Msg)
emptyResp.MsgHdr = msgHdr
emptyResp.Response = true
if _, isTCP := w.RemoteAddr().(*net.TCPAddr); isTCP {
dns.HandleFailed(w, emptyResp)
return
}
emptyResp.Truncated = true
w.WriteMsg(emptyResp)
}
示例12: route
func route(w dns.ResponseWriter, req *dns.Msg) {
if len(req.Question) == 0 || !allowed(w, req) {
dns.HandleFailed(w, req)
return
}
for name, addr := range routes {
if strings.HasSuffix(req.Question[0].Name, name) {
proxy(addr, w, req)
return
}
}
proxy(*defaultServer, w, req)
}
示例13: serve
func serve(w dns.ResponseWriter, req *dns.Msg) {
var resp dns.Msg
var err error
key := msgKey(req)
cacheLock.Lock()
cached, ok := cache[key]
// cached value will not change until return because of SingleInflight
cacheLock.Unlock()
if !ok {
log.Printf("%04X┐%s\n", req.Id, key)
for attempt := 1; attempt <= queryAttempts; attempt++ {
cached, _, err = dnsclient.Exchange(req, *flUpstream)
if err != nil {
cacheLock.Lock()
_, ok = cache[key]
cacheLock.Unlock()
if ok {
// concurrent exchange succeeded
err = nil
break
}
sep := "·"
if attempt == queryAttempts {
sep = "╳"
}
log.Printf("%04X%s%s: [%d/%d] %v\n", req.Id, sep, key,
attempt, queryAttempts, err)
}
}
if err != nil {
dns.HandleFailed(w, req)
return
}
cacheLock.Lock()
cached2, ok := cache[key]
if ok {
// concurrent exchange has already updated the cache
cached = cached2
log.Printf("%04X┴%04X\n", req.Id, cached.Id)
} else {
cache[key] = cached
log.Printf("%04X└%s = %s\n", req.Id, key, answersSummary(cached))
}
cacheLock.Unlock()
}
resp = *cached
resp.Id = req.Id
err = w.WriteMsg(&resp)
eprint(err)
}
示例14: do
func (s *Server) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
if len(req.Question) != 1 {
dns.HandleFailed(w, req)
return
}
zone, name := s.zones.match(req.Question[0].Name, req.Question[0].Qtype)
if zone == nil {
s.recurse(w, req)
return
}
s.zones.Lock()
defer s.zones.Unlock()
m := new(dns.Msg)
m.SetReply(req)
var answerKnown bool
dnsreq := dns.RR_Header{Name: req.Question[0].Name, Rrtype: req.Question[0].Qtype, Class: req.Question[0].Qclass}
for _, r := range (*zone)[dnsreq] {
m.Answer = append(m.Answer, r)
answerKnown = true
}
// TODO: more logs here
s.roundRobin(zone, dnsreq)
if !answerKnown && s.recurseTo != "" {
s.recurse(w, req)
return
}
// Add Authority section
for _, r := range (*zone)[dns.RR_Header{Name: name, Rrtype: dns.TypeNS, Class: dns.ClassINET}] {
m.Ns = append(m.Ns, r)
// Resolve Authority if possible and serve as Extra
for _, r := range (*zone)[dns.RR_Header{Name: r.(*dns.NS).Ns, Rrtype: dns.TypeA, Class: dns.ClassINET}] {
m.Extra = append(m.Extra, r)
}
for _, r := range (*zone)[dns.RR_Header{Name: r.(*dns.NS).Ns, Rrtype: dns.TypeAAAA, Class: dns.ClassINET}] {
m.Extra = append(m.Extra, r)
}
}
m.Authoritative = true
w.WriteMsg(m)
}
示例15: do
func (h *GODNSHandler) do(Net string, w dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
Q := Question{UnFqdn(q.Name), dns.TypeToString[q.Qtype], dns.ClassToString[q.Qclass]}
fmt.Println("DNS Lookup ", Q.String())
IPQuery := h.isIPQuery(q)
key := fmt.Sprintf("%s-%s", h.GetHour(), Q.String())
fmt.Println("Cache key: ", key)
if IPQuery > 0 {
mesg, ok := h.Cache.Get(key)
if ok == true {
fmt.Println("Hit cache", Q.String())
rmesg := mesg.(*dns.Msg)
rmesg.Id = req.Id
w.WriteMsg(BuildDNSMsg(rmesg))
return
}
}
mesg, err := h.resolver.Lookup(Net, req)
if err != nil {
mesg, err = h.resolver.Lookup(Net, req) // try to lookup again
if err != nil {
fmt.Println("Resolve query error ", err)
dns.HandleFailed(w, req)
}
return
}
w.WriteMsg(BuildDNSMsg(mesg))
if IPQuery > 0 && len(mesg.Answer) > 0 {
h.Cache.Add(key, mesg)
fmt.Println("Insert into cache", Q.String())
}
}