2021-02-04 17:35:13 +00:00
|
|
|
package aghtest
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/miekg/dns"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestUpstream is a mock of real upstream.
|
|
|
|
type TestUpstream struct {
|
|
|
|
// CName is a map of hostname to canonical name.
|
|
|
|
CName map[string]string
|
|
|
|
// IPv4 is a map of hostname to IPv4.
|
|
|
|
IPv4 map[string][]net.IP
|
|
|
|
// IPv6 is a map of hostname to IPv6.
|
|
|
|
IPv6 map[string][]net.IP
|
|
|
|
// Reverse is a map of address to domain name.
|
|
|
|
Reverse map[string][]string
|
2021-05-31 17:11:06 +00:00
|
|
|
// Addr is the address for Address method.
|
|
|
|
Addr string
|
2021-02-04 17:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exchange implements upstream.Upstream interface for *TestUpstream.
|
2021-05-31 17:11:06 +00:00
|
|
|
//
|
|
|
|
// TODO(a.garipov): Split further into handlers.
|
2021-02-04 17:35:13 +00:00
|
|
|
func (u *TestUpstream) Exchange(m *dns.Msg) (resp *dns.Msg, err error) {
|
|
|
|
resp = &dns.Msg{}
|
|
|
|
resp.SetReply(m)
|
|
|
|
|
|
|
|
if len(m.Question) == 0 {
|
|
|
|
return nil, fmt.Errorf("question should not be empty")
|
|
|
|
}
|
2021-05-31 17:11:06 +00:00
|
|
|
|
2021-02-04 17:35:13 +00:00
|
|
|
name := m.Question[0].Name
|
|
|
|
|
|
|
|
if cname, ok := u.CName[name]; ok {
|
2021-05-31 17:11:06 +00:00
|
|
|
ans := &dns.CNAME{
|
2021-02-04 17:35:13 +00:00
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: name,
|
|
|
|
Rrtype: dns.TypeCNAME,
|
|
|
|
},
|
|
|
|
Target: cname,
|
2021-05-31 17:11:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
resp.Answer = append(resp.Answer, ans)
|
2021-02-04 17:35:13 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 17:11:06 +00:00
|
|
|
rrType := m.Question[0].Qtype
|
|
|
|
hdr := dns.RR_Header{
|
|
|
|
Name: name,
|
|
|
|
Rrtype: rrType,
|
|
|
|
}
|
|
|
|
|
|
|
|
var names []string
|
2021-02-04 17:35:13 +00:00
|
|
|
var ips []net.IP
|
|
|
|
switch m.Question[0].Qtype {
|
|
|
|
case dns.TypeA:
|
2021-05-31 17:11:06 +00:00
|
|
|
ips = u.IPv4[name]
|
2021-02-04 17:35:13 +00:00
|
|
|
case dns.TypeAAAA:
|
2021-05-31 17:11:06 +00:00
|
|
|
ips = u.IPv6[name]
|
2021-02-04 17:35:13 +00:00
|
|
|
case dns.TypePTR:
|
2021-05-31 17:11:06 +00:00
|
|
|
names = u.Reverse[name]
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ip := range ips {
|
|
|
|
var ans dns.RR
|
|
|
|
if rrType == dns.TypeA {
|
|
|
|
ans = &dns.A{
|
|
|
|
Hdr: hdr,
|
|
|
|
A: ip,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Answer = append(resp.Answer, ans)
|
|
|
|
|
|
|
|
continue
|
2021-02-04 17:35:13 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 17:11:06 +00:00
|
|
|
ans = &dns.AAAA{
|
|
|
|
Hdr: hdr,
|
|
|
|
AAAA: ip,
|
2021-02-04 17:35:13 +00:00
|
|
|
}
|
2021-05-31 17:11:06 +00:00
|
|
|
|
|
|
|
resp.Answer = append(resp.Answer, ans)
|
2021-02-04 17:35:13 +00:00
|
|
|
}
|
|
|
|
|
2021-05-31 17:11:06 +00:00
|
|
|
for _, n := range names {
|
|
|
|
ans := &dns.PTR{
|
|
|
|
Hdr: hdr,
|
|
|
|
Ptr: n,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp.Answer = append(resp.Answer, ans)
|
2021-02-04 17:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(resp.Answer) == 0 {
|
|
|
|
resp.SetRcode(m, dns.RcodeNameError)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address implements upstream.Upstream interface for *TestUpstream.
|
|
|
|
func (u *TestUpstream) Address() string {
|
|
|
|
return u.Addr
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestBlockUpstream implements upstream.Upstream interface for replacing real
|
|
|
|
// upstream in tests.
|
|
|
|
type TestBlockUpstream struct {
|
2021-05-31 17:11:06 +00:00
|
|
|
Hostname string
|
|
|
|
|
|
|
|
// lock protects reqNum.
|
|
|
|
lock sync.RWMutex
|
|
|
|
reqNum int
|
|
|
|
|
|
|
|
Block bool
|
2021-02-04 17:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exchange returns a message unique for TestBlockUpstream's Hostname-Block
|
|
|
|
// pair.
|
|
|
|
func (u *TestBlockUpstream) Exchange(r *dns.Msg) (*dns.Msg, error) {
|
|
|
|
u.lock.Lock()
|
|
|
|
defer u.lock.Unlock()
|
2021-05-31 17:11:06 +00:00
|
|
|
u.reqNum++
|
2021-02-04 17:35:13 +00:00
|
|
|
|
|
|
|
hash := sha256.Sum256([]byte(u.Hostname))
|
|
|
|
hashToReturn := hex.EncodeToString(hash[:])
|
|
|
|
if !u.Block {
|
|
|
|
hashToReturn = hex.EncodeToString(hash[:])[:2] + strings.Repeat("ab", 28)
|
|
|
|
}
|
|
|
|
|
|
|
|
m := &dns.Msg{}
|
|
|
|
m.Answer = []dns.RR{
|
|
|
|
&dns.TXT{
|
|
|
|
Hdr: dns.RR_Header{
|
|
|
|
Name: r.Question[0].Name,
|
|
|
|
},
|
|
|
|
Txt: []string{
|
|
|
|
hashToReturn,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Address always returns an empty string.
|
|
|
|
func (u *TestBlockUpstream) Address() string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// RequestsCount returns the number of handled requests. It's safe for
|
|
|
|
// concurrent use.
|
|
|
|
func (u *TestBlockUpstream) RequestsCount() int {
|
|
|
|
u.lock.Lock()
|
|
|
|
defer u.lock.Unlock()
|
|
|
|
|
2021-05-31 17:11:06 +00:00
|
|
|
return u.reqNum
|
2021-02-04 17:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TestErrUpstream implements upstream.Upstream interface for replacing real
|
|
|
|
// upstream in tests.
|
2021-03-31 12:00:47 +00:00
|
|
|
type TestErrUpstream struct {
|
|
|
|
// The error returned by Exchange may be unwraped to the Err.
|
|
|
|
Err error
|
|
|
|
}
|
2021-02-04 17:35:13 +00:00
|
|
|
|
|
|
|
// Exchange always returns nil Msg and non-nil error.
|
|
|
|
func (u *TestErrUpstream) Exchange(*dns.Msg) (*dns.Msg, error) {
|
2021-03-31 12:00:47 +00:00
|
|
|
return nil, fmt.Errorf("errupstream: %w", u.Err)
|
2021-02-04 17:35:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Address always returns an empty string.
|
|
|
|
func (u *TestErrUpstream) Address() string {
|
|
|
|
return ""
|
|
|
|
}
|