本文整理汇总了Golang中github.com/miekg/dns.ResponseWriter.WriteMsg方法的典型用法代码示例。如果您正苦于以下问题:Golang ResponseWriter.WriteMsg方法的具体用法?Golang ResponseWriter.WriteMsg怎么用?Golang ResponseWriter.WriteMsg使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/miekg/dns.ResponseWriter
的用法示例。
在下文中一共展示了ResponseWriter.WriteMsg方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleRequest
// Handles an incoming DNS request packet. This function decides whether
// the hostname listed in the DNS packet is worthy of manipulation, or
// not. The IP addresses listed in the reply to the user for a target
// hostname are added to the routing table at this time before a
// reply is sent back to the user, otherwise the user agent of the client
// might connect faster than the routing changes can be made.
func handleRequest(w dns.ResponseWriter, req *dns.Msg) {
var m *dns.Msg
// check if the the hostname in the request matches the target
if len(req.Question) > 0 && isTargetZone(req.Question[0].Name) {
// handle `A` and `AAAA` types accordingly
// other record types will be forwarded without manipulation
switch req.Question[0].Qtype {
case dns.TypeA:
m = handleV4Hijack(w, req)
case dns.TypeAAAA:
m = handleV6Hijack(w, req)
}
}
// if no reply was previously set, forward it
if m == nil {
m = getServerReply(w, req)
}
// send reply back to user
w.WriteMsg(m)
}
示例2: handleRecurse
// handleRecurse is used to handle recursive DNS queries
func (d *DNSServer) handleRecurse(resp dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
network := "udp"
defer func(s time.Time) {
d.logger.Printf("[DEBUG] dns: request for %v (%s) (%v)", q, network, time.Now().Sub(s))
}(time.Now())
// Switch to TCP if the client is
if _, ok := resp.RemoteAddr().(*net.TCPAddr); ok {
network = "tcp"
}
// Recursively resolve
c := &dns.Client{Net: network}
r, rtt, err := c.Exchange(req, d.recursor)
// On failure, return a SERVFAIL message
if err != nil {
d.logger.Printf("[ERR] dns: recurse failed: %v", err)
m := &dns.Msg{}
m.SetReply(req)
m.RecursionAvailable = true
m.SetRcode(req, dns.RcodeServerFailure)
resp.WriteMsg(m)
return
}
d.logger.Printf("[DEBUG] dns: recurse RTT for %v (%v)", q, rtt)
// Forward the response
if err := resp.WriteMsg(r); err != nil {
d.logger.Printf("[WARN] dns: failed to respond: %v", err)
}
}
示例3: handleDNS
// handleDNS is a handler function to actualy perform the dns querey response
func (c *CatchAll) handleDNS(w dns.ResponseWriter, r *dns.Msg) {
defer w.Close()
var rr dns.RR
domainSpoof := r.Question[0].Name
msgResp := new(dns.Msg)
msgResp.SetReply(r)
msgResp.Compress = false
rr = new(dns.A)
if c.SpoofDomain {
rr.(*dns.A).Hdr = dns.RR_Header{Name: domainSpoof, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}
} else {
rr.(*dns.A).Hdr = dns.RR_Header{Name: c.Domain, Rrtype: dns.TypeA, Class: dns.ClassINET, Ttl: 0}
}
rr.(*dns.A).A = c.IP
switch r.Question[0].Qtype {
case dns.TypeA:
msgResp.Answer = append(msgResp.Answer, rr)
default:
log.Warnf("Unknown dns type %T", r.Question[0].Qtype)
return
}
w.WriteMsg(msgResp)
}
示例4: handleQuery
// handleQUery is used to handle DNS queries in the configured domain
func (d *DNSServer) handleQuery(resp dns.ResponseWriter, req *dns.Msg) {
q := req.Question[0]
defer func(s time.Time) {
d.logger.Printf("[DEBUG] dns: request for %v (%v)", q, time.Now().Sub(s))
}(time.Now())
// Switch to TCP if the client is
network := "udp"
if _, ok := resp.RemoteAddr().(*net.TCPAddr); ok {
network = "tcp"
}
// Setup the message response
m := new(dns.Msg)
m.SetReply(req)
m.Authoritative = true
m.RecursionAvailable = (len(d.recursors) > 0)
// Only add the SOA if requested
if req.Question[0].Qtype == dns.TypeSOA {
d.addSOA(d.domain, m)
}
// Dispatch the correct handler
d.dispatch(network, req, m)
// Write out the complete response
if err := resp.WriteMsg(m); err != nil {
d.logger.Printf("[WARN] dns: failed to respond: %v", err)
}
}
示例5: ServeDNS
// ServeDNS implements the middleware.Handler interface.
func (d Dnssec) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (int, error) {
state := middleware.State{W: w, Req: r}
do := state.Do()
qname := state.Name()
qtype := state.QType()
zone := middleware.Zones(d.zones).Matches(qname)
if zone == "" {
return d.Next.ServeDNS(ctx, w, r)
}
// Intercept queries for DNSKEY, but only if one of the zones matches the qname, otherwise we let
// the query through.
if qtype == dns.TypeDNSKEY {
for _, z := range d.zones {
if qname == z {
resp := d.getDNSKEY(state, z, do)
state.SizeAndDo(resp)
w.WriteMsg(resp)
return dns.RcodeSuccess, nil
}
}
}
drr := NewDnssecResponseWriter(w, d)
return d.Next.ServeDNS(ctx, drr, r)
}
示例6: metazone
// Create DNS packet with the config in line with the meta zone
// paper from Vixie
func metazone(w dns.ResponseWriter, req *dns.Msg, c *Config) {
logPrintf("metazone command")
// Only called when the class is CHAOS
// PTR zone. -> get a list of zone names
// Top level zone stuff -- list them
if strings.ToUpper(req.Question[0].Name) == "ZONE." {
m := new(dns.Msg)
m.SetReply(req)
for _, z := range c.Zones {
ptr, _ := dns.NewRR("zone. 0 CH PTR " + z.Origin)
m.Answer = append(m.Answer, ptr)
}
w.WriteMsg(m)
return
}
// Top level user stuff -- list them
if strings.ToUpper(req.Question[0].Name) == "USER." {
}
// <zone>.ZONE.
formerr(w, req)
return
}
示例7: 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)
}
}
示例8: ServeDNS
func (h dnsHandler) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
if len(r.Question) != 1 {
h.t.Fatalf("bad: %#v", r.Question)
}
name := "join.service.consul."
question := r.Question[0]
if question.Name != name || question.Qtype != dns.TypeANY {
h.t.Fatalf("bad: %#v", question)
}
m := new(dns.Msg)
m.SetReply(r)
m.Authoritative = true
m.RecursionAvailable = false
m.Answer = append(m.Answer, &dns.A{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeA,
Class: dns.ClassINET},
A: net.ParseIP("127.0.0.1"),
})
m.Answer = append(m.Answer, &dns.AAAA{
Hdr: dns.RR_Header{
Name: name,
Rrtype: dns.TypeAAAA,
Class: dns.ClassINET},
AAAA: net.ParseIP("2001:db8:a0b:12f0::1"),
})
if err := w.WriteMsg(m); err != nil {
h.t.Fatalf("err: %v", err)
}
}
示例9: ServeDNS
// ServeDNS resolution.
func (h *RandomUpstream) ServeDNS(w dns.ResponseWriter, r *dns.Msg) {
ns := h.upstream[rand.Intn(len(h.upstream))]
ns = defaultPort(ns)
for _, q := range r.Question {
log.Printf("[info] [%v] <== %s %s %v (ns %s)\n", r.Id,
dns.ClassToString[q.Qclass],
dns.TypeToString[q.Qtype],
q.Name,
ns)
}
client := &dns.Client{
Net: w.RemoteAddr().Network(),
}
res, rtt, err := client.Exchange(r, ns)
if err != nil {
msg := new(dns.Msg)
msg.SetRcode(r, dns.RcodeServerFailure)
w.WriteMsg(msg)
return
}
log.Printf("[info] [%v] ==> %s:", r.Id, rtt)
for _, a := range res.Answer {
log.Printf("[info] [%v] ----> %s\n", r.Id, a)
}
err = w.WriteMsg(res)
if err != nil {
log.Printf("[error] [%v] failed to respond – %s", r.Id, err)
}
}
示例10: handleDnsRequest
func handleDnsRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
records := make([]dns.RR, 0)
q := r.Question[0]
if q.Qtype == dns.TypeA && strings.HasSuffix(q.Name, ".docker.") {
docker, _ := dockerclient.NewDockerClient("unix:///var/run/docker.sock", &tls.Config{})
name := strings.SplitN(q.Name, ".", 2)[0]
containers, err := docker.ListContainers(false, false, fmt.Sprintf("{\"name\":[\"%s\"]}", name))
if err != nil {
log.Fatal(err)
}
for _, c := range containers {
info, _ := docker.InspectContainer(c.Id)
log.Printf("Container %s[%6s] has ip %s\n", name, info.Id, info.NetworkSettings.IPAddress)
records = append(records,
&dns.A{
Hdr: dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeA,
Class: dns.ClassINET,
Ttl: 60},
A: net.ParseIP(info.NetworkSettings.IPAddress),
})
}
}
m.Answer = append(m.Answer, records...)
defer w.WriteMsg(m)
}
示例11: handleRequest
func (s *jujuNameServer) handleRequest(w dns.ResponseWriter, r *dns.Msg) {
m := new(dns.Msg)
m.SetReply(r)
for _, q := range r.Question {
rr, err := s.answer(q)
if err != nil {
m.SetRcodeFormatError(r)
t := new(dns.TXT)
t.Hdr = dns.RR_Header{
Name: q.Name,
Rrtype: dns.TypeTXT,
Class: dns.ClassNONE,
}
t.Txt = []string{err.Error()}
m.Extra = append(m.Extra, t)
continue
} else if rr != nil {
m.Answer = append(m.Answer, rr)
}
}
m.Authoritative = true
// recursion isn't really available, but it's apparently
// necessary to set this to make nslookup happy.
m.RecursionAvailable = true
w.WriteMsg(m)
}
示例12: 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)
}
示例13: 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)
}
示例14: 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)
}
示例15: TransferHandler
func (s *soa) TransferHandler(w dns.ResponseWriter, req *dns.Msg) {
m := new(dns.Msg)
m.SetReply(req)
m.Answer = make([]dns.RR, 1)
m.Answer[0] = test.SOA(fmt.Sprintf("%s IN SOA bla. bla. %d 0 0 0 0 ", testZone, s.serial))
w.WriteMsg(m)
}