예제 #1
0
파일: main.go 프로젝트: orinocoz/go-dberror
func dnsHandler(w dns.ResponseWriter, r *dns.Msg) {
	defer w.Close()
	m := new(dns.Msg)
	m.SetReply(r)
	m.Compress = false

	for _, q := range r.Question {
		fmt.Printf("dns-srv: Query -- [%s] %s\n", q.Name, dns.TypeToString[q.Qtype])
		switch q.Qtype {
		case dns.TypeA:
			record := new(dns.A)
			record.Hdr = dns.RR_Header{
				Name:   q.Name,
				Rrtype: dns.TypeA,
				Class:  dns.ClassINET,
				Ttl:    0,
			}
			record.A = net.ParseIP("127.0.0.1")

			m.Answer = append(m.Answer, record)
		case dns.TypeMX:
			record := new(dns.MX)
			record.Hdr = dns.RR_Header{
				Name:   q.Name,
				Rrtype: dns.TypeMX,
				Class:  dns.ClassINET,
				Ttl:    0,
			}
			record.Mx = "mail." + q.Name
			record.Preference = 10

			m.Answer = append(m.Answer, record)
		}
	}

	w.WriteMsg(m)
	return
}
예제 #2
0
func (ts *testSrv) dnsHandler(w dns.ResponseWriter, r *dns.Msg) {
	m := new(dns.Msg)
	m.SetReply(r)
	m.Compress = false

	// Normally this test DNS server will return 127.0.0.1 for everything.
	// However, in some situations (for instance Docker), it's useful to return a
	// different hardcoded host. You can do so by setting the FAKE_DNS environment
	// variable.
	fakeDNS := os.Getenv("FAKE_DNS")
	if fakeDNS == "" {
		fakeDNS = "127.0.0.1"
	}
	for _, q := range r.Question {
		fmt.Printf("dns-srv: Query -- [%s] %s\n", q.Name, dns.TypeToString[q.Qtype])
		switch q.Qtype {
		case dns.TypeA:
			record := new(dns.A)
			record.Hdr = dns.RR_Header{
				Name:   q.Name,
				Rrtype: dns.TypeA,
				Class:  dns.ClassINET,
				Ttl:    0,
			}
			record.A = net.ParseIP(fakeDNS)

			m.Answer = append(m.Answer, record)
		case dns.TypeMX:
			record := new(dns.MX)
			record.Hdr = dns.RR_Header{
				Name:   q.Name,
				Rrtype: dns.TypeMX,
				Class:  dns.ClassINET,
				Ttl:    0,
			}
			record.Mx = "mail." + q.Name
			record.Preference = 10

			m.Answer = append(m.Answer, record)
		case dns.TypeTXT:
			ts.mu.RLock()
			value, present := ts.txtRecords[q.Name]
			ts.mu.RUnlock()
			if !present {
				continue
			}
			record := new(dns.TXT)
			record.Hdr = dns.RR_Header{
				Name:   q.Name,
				Rrtype: dns.TypeTXT,
				Class:  dns.ClassINET,
				Ttl:    0,
			}
			record.Txt = []string{value}
			m.Answer = append(m.Answer, record)
		case dns.TypeCAA:
			if q.Name == "bad-caa-reserved.com." || q.Name == "good-caa-reserved.com." {
				record := new(dns.CAA)
				record.Hdr = dns.RR_Header{
					Name:   q.Name,
					Rrtype: dns.TypeCAA,
					Class:  dns.ClassINET,
					Ttl:    0,
				}
				record.Tag = "issue"
				if q.Name == "bad-caa-reserved.com." {
					record.Value = "sad-hacker-ca.invalid"
				} else if q.Name == "good-caa-reserved.com." {
					record.Value = "happy-hacker-ca.invalid"
				}
				m.Answer = append(m.Answer, record)
			}
		}
	}

	auth := new(dns.SOA)
	auth.Hdr = dns.RR_Header{Name: "boulder.invalid.", Rrtype: dns.TypeSOA, Class: dns.ClassINET, Ttl: 0}
	auth.Ns = "ns.boulder.invalid."
	auth.Mbox = "master.boulder.invalid."
	auth.Serial = 1
	auth.Refresh = 1
	auth.Retry = 1
	auth.Expire = 1
	auth.Minttl = 1
	m.Ns = append(m.Ns, auth)

	w.WriteMsg(m)
	return
}
예제 #3
0
파일: dns.go 프로젝트: cdshann/minimega
func handleDnsRequest(w dns.ResponseWriter, req *dns.Msg) {
	r := new(dns.Msg)
	r.SetReply(req)
	r.Authoritative = true

	if len(req.Question) > 1 || req.Rcode != dns.OpcodeQuery {
		r.SetRcode(req, dns.RcodeNotImplemented)
	}

	if len(req.Question) == 0 {
		r.SetRcode(req, dns.RcodeFormatError)
	}

	if r.Rcode != dns.RcodeSuccess {
		w.WriteMsg(r)
		dnsReportChan <- 1
		return
	}

	q := req.Question[0]
	log.Debug("dns server: question=%v type=%v remote_host=%v", q.Name, q.Qtype, w.RemoteAddr())

	switch q.Qtype {
	case dns.TypeA:
		h, _ := randomHost()
		if h == "" || !isIPv4(h) {
			if *f_randomhosts {
				h = randomIPv4Addr()
			} else {
				// return NXDOMAIN
				r.SetRcode(req, dns.RcodeNameError)
				break
			}
		}

		resp := new(dns.A)
		resp.Hdr = dns.RR_Header{
			Name:   q.Name,
			Rrtype: dns.TypeA,
			Class:  dns.ClassINET,
			Ttl:    ttl,
		}
		resp.A = net.ParseIP(h)
		r.Answer = append(r.Answer, resp)
	case dns.TypeAAAA:
		h, _ := randomHost()
		if h == "" || !isIPv6(h) {
			if *f_randomhosts {
				h = randomIPv6Addr()
			} else {
				// return NXDOMAIN
				r.SetRcode(req, dns.RcodeNameError)
				break
			}
		}

		resp := new(dns.AAAA)
		resp.Hdr = dns.RR_Header{
			Name:   q.Name,
			Rrtype: dns.TypeAAAA,
			Class:  dns.ClassINET,
			Ttl:    ttl,
		}
		resp.AAAA = net.ParseIP(h)
		r.Answer = append(r.Answer, resp)
	case dns.TypeCNAME:
		resp := new(dns.CNAME)
		resp.Hdr = dns.RR_Header{
			Name:   q.Name,
			Rrtype: dns.TypeCNAME,
			Class:  dns.ClassINET,
			Ttl:    ttl,
		}
		resp.Target = fmt.Sprintf("cname.%s", q.Name)
		r.Answer = append(r.Answer, resp)
	case dns.TypeMX:
		resp := new(dns.MX)
		resp.Hdr = dns.RR_Header{
			Name:   q.Name,
			Rrtype: dns.TypeMX,
			Class:  dns.ClassINET,
			Ttl:    ttl,
		}
		resp.Mx = fmt.Sprintf("mx.%s", q.Name)
		r.Answer = append(r.Answer, resp)
	case dns.TypeSOA:
		resp := new(dns.SOA)
		resp.Hdr = dns.RR_Header{
			Name:   q.Name,
			Rrtype: dns.TypeSOA,
			Class:  dns.ClassINET,
			Ttl:    ttl,
		}
		resp.Ns = fmt.Sprintf("ns.%s", q.Name)
		resp.Mbox = fmt.Sprintf("admin-%s", q.Name)
		r.Answer = append(r.Answer, resp)
	}
	w.WriteMsg(r)
	dnsReportChan <- 1
}