master
kayos@tcp.direct 11 months ago
parent 156734b77c
commit 33af02d58e
  1. 213
      dnsmap.go
  2. 15
      go.mod
  3. 40
      go.sum
  4. 76
      queries.go

@ -0,0 +1,213 @@
package Name5
import (
"sync"
"time"
"github.com/miekg/dns"
"github.com/rs/zerolog/log"
)
type ipname struct {
Name string
IPs []string
}
// DNSMap is used for resolving and keeping track of DNS query results and their relationships
type DNSMap struct {
IPToPTR map[string]string
NameToIP map[string]*ipname
Waitlist4 map[string]int
Waitlist6 map[string]int
CNAMETargets map[string]int
born time.Time
mu *sync.RWMutex
}
// NewDNSMap creates a new DNSMap type
func NewDNSMap(name string) *DNSMap {
name = dns.Fqdn(name)
dnsmap := &DNSMap{ // ipaddr[domain][ipaddr]boolean
IPToPTR: make(map[string]string),
NameToIP: make(map[string]*ipname),
Waitlist4: make(map[string]int),
Waitlist6: make(map[string]int),
CNAMETargets: make(map[string]int),
born: time.Now(),
mu: &sync.RWMutex{},
}
dnsmap.addToWaitlist(dns.Fqdn(name))
go dnsmap.process(Query4(name))
go dnsmap.process(Query6(name))
dnsmap.waitUntilDone()
return dnsmap
}
func (d *DNSMap) waitUntilDone() {
for {
d.mu.RLock()
if time.Since(d.born) > 5*time.Minute {
d.mu.RUnlock()
log.Error().Msg("DNSMap timed out after 5 minutes")
return
}
if len(d.Waitlist4) == 0 && len(d.Waitlist6) == 0 {
d.mu.RUnlock()
return
}
d.mu.RUnlock()
time.Sleep(20 * time.Millisecond)
}
}
func (d *DNSMap) addToWaitlist(name string) {
name = dns.Fqdn(name)
d.mu.Lock()
if _, ok := d.Waitlist4[name]; !ok {
d.Waitlist4[name] = 1
}
if _, ok := d.Waitlist6[name]; !ok {
d.Waitlist6[name] = 1
}
d.mu.Unlock()
}
func (d *DNSMap) isInWaitlist(name string) bool {
d.mu.RLock()
defer d.mu.RUnlock()
name = dns.Fqdn(name)
_, ok4 := d.Waitlist4[name]
_, ok6 := d.Waitlist6[name]
if !ok4 && !ok6 {
return false
}
return true
}
func (d *DNSMap) isPTRResolved(object string) bool {
d.mu.RLock()
defer d.mu.RUnlock()
if _, ok := d.IPToPTR[object]; ok {
return true
}
return false
}
func (d *DNSMap) process(in *dns.Msg) {
var (
addrs []string
question string
ptr = ""
cname = ""
qtype uint16
)
question = in.Question[0].Name
qtype = in.Question[0].Qtype
for _, resource := range in.Answer {
switch record := resource.(type) {
case *dns.NULL:
log.Error().Caller().Msg(record.Data)
continue
case *dns.A:
addrs = append(addrs, record.A.String())
ptrReq, _ := dns.ReverseAddr(record.A.String())
d.addToWaitlist(ptrReq)
d.process(QueryPTR(ptrReq))
case *dns.AAAA:
addrs = append(addrs, record.AAAA.String())
ptrReq, _ := dns.ReverseAddr(record.AAAA.String())
d.addToWaitlist(ptrReq)
d.process(QueryPTR(ptrReq))
case *dns.PTR:
ptr = record.Ptr
d.mu.RLock()
if _, ok := d.NameToIP[ptr]; !ok {
d.mu.RUnlock()
if !d.isInWaitlist(ptr) {
d.addToWaitlist(ptr)
go d.process(Query4(ptr))
go d.process(Query6(ptr))
}
} else {
d.mu.RUnlock()
}
case *dns.CNAME:
cname = record.Target
d.mu.Lock()
d.CNAMETargets[cname] = 1
d.mu.Unlock()
d.mu.RLock()
if _, ok := d.NameToIP[cname]; !ok {
d.mu.RUnlock()
if !d.isInWaitlist(cname) {
d.addToWaitlist(cname)
go d.process(Query4(cname))
go d.process(Query6(cname))
}
} else {
d.mu.RUnlock()
}
default:
log.Warn().Caller().Interface("resource", record).Msg("unhandled record")
}
}
if cname == "" && ptr == "" && len(addrs) == 0 {
d.removeWait(question, qtype)
}
if ptr != "" {
d.mu.Lock()
if _, ok := d.IPToPTR[question]; !ok {
d.IPToPTR[question] = ptr
}
d.mu.Unlock()
}
if len(addrs) > 0 {
d.mu.Lock()
var resolved *ipname
var ok bool
if resolved, ok = d.NameToIP[question]; ok {
resolved.IPs = append(resolved.IPs, addrs...)
} else {
d.NameToIP[question] = &ipname{Name: question, IPs: addrs}
}
d.mu.Unlock()
}
if ptr != "" {
d.mu.Lock()
d.IPToPTR[question] = ptr
d.mu.Unlock()
}
d.removeWait(question, qtype)
}
func (d *DNSMap) removeWait(question string, qtype uint16) {
if !d.isInWaitlist(question) {
return
}
d.mu.Lock()
switch qtype {
case dns.TypeA:
delete(d.Waitlist4, question)
case dns.TypeAAAA:
delete(d.Waitlist6, question)
case dns.TypeCNAME:
case dns.TypePTR:
delete(d.Waitlist4, question)
delete(d.Waitlist6, question)
default:
log.Warn().Caller().Str("question", question).
Uint16("qtype", qtype).
Msg("unhandled blank response")
}
d.mu.Unlock()
return
}

@ -0,0 +1,15 @@
module Name5
go 1.17
require (
github.com/miekg/dns v1.1.43
github.com/rs/zerolog v1.25.0
github.com/yunginnanet/Rate5 v0.4.3
)
require (
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
)

@ -0,0 +1,40 @@
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/miekg/dns v1.1.43 h1:JKfpVSCB84vrAmHzyrsxB5NAr5kLoMXZArPSw7Qlgyg=
github.com/miekg/dns v1.1.43/go.mod h1:+evo5L0630/F6ca/Z9+GAqzhjGyn8/c+TBaOyfEl0V4=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.25.0 h1:Rj7XygbUHKUlDPcVdoLyR91fJBsduXj5fRxyqIQj/II=
github.com/rs/zerolog v1.25.0/go.mod h1:7KHcEGe0QZPOm2IE4Kpb5rTh6n1h2hIgS5OOnu1rUaI=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yunginnanet/Rate5 v0.4.3 h1:DuMa5QPKF0Kr7wJpnHNaSJ0wNkVc17A3DlVceqTYxKo=
github.com/yunginnanet/Rate5 v0.4.3/go.mod h1:aaaV1FLFmdBk1AD7uGQF53hgfPQg9yfBmIfDxtJuYZs=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 h1:4nGaVu0QrbjT/AK2PRLuQfQuh6DJve+pELhqTdAj3x0=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 h1:gG67DSER+11cZvqIMb8S8bt0vZtiN6xWYARwirrOSfE=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

@ -0,0 +1,76 @@
package Name5
import (
"time"
"github.com/miekg/dns"
"github.com/rs/zerolog/log"
"github.com/yunginnanet/Rate5"
)
// TODO: benchmarks
// TODO: better ratelimiting logic
// global dummy type for rater
type resolveSlower struct {
key string
}
func (rs *resolveSlower) UniqueKey() string {
return rs.key
}
var dnsRater *rate5.Limiter
/*
SmuggleError crafts a custom DNS answer to include our error message.
This allows the function to only have one return, thus simplifying our pipeline. (maybe)
*/
func SmuggleError(err error) *dns.Msg {
dnserr := new(dns.Msg)
dnserr.Answer = make([]dns.RR, 1)
dnserr.Answer[0] = new(dns.NULL)
dnserr.Answer[0].(*dns.NULL).Data = err.Error()
return dnserr
}
func query(domain string, qtype uint16) *dns.Msg {
if dnsRater.Check(&resolveSlower{key: "yeet"}) {
time.Sleep(10 * time.Millisecond)
}
domain = dns.Fqdn(domain)
m1 := new(dns.Msg)
m1.Id = dns.Id()
m1.RecursionDesired = true
m1.Question = make([]dns.Question, 1)
m1.Question[0] = dns.Question{Name: domain, Qtype: qtype, Qclass: dns.ClassINET}
lookup := &dns.Client{
Timeout: time.Duration(6) * time.Second,
SingleInflight: true,
}
in, _, err := lookup.Exchange(m1, "127.0.0.1:53")
if err != nil {
log.Error().Err(err).Msg("query")
resp := SmuggleError(err)
resp.Question = append(resp.Question, m1.Question[0])
return resp
}
in.Answer = dns.Dedup(in.Answer, nil)
in.Ns = dns.Dedup(in.Ns, nil)
in.Extra = dns.Dedup(in.Extra, nil)
return in
}
// Query4 requests an A record answer
func Query4(domain string) *dns.Msg {
return query(domain, 1)
}
// Query6 requests an AAAA record answer
func Query6(domain string) *dns.Msg {
return query(domain, 28)
}
// QueryPTR retrievs reverse DNS records
func QueryPTR(ip string) *dns.Msg {
return query(ip, 12)
}
Loading…
Cancel
Save