Pull request: all: custom autohost tlds

Updates #2393.

Squashed commit of the following:

commit 87034134e240480938cdeec14d6b44294bf6442c
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Mar 25 15:48:46 2021 +0300

    dnsforward: fix

commit abf3a1ce8ed7a148d1cc631007fb0422f6da4ae6
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Mar 25 15:21:11 2021 +0300

    dnsforward: imp code, validation

commit fac389bdafc093ce17a7e0831166b89293b550be
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Thu Mar 25 14:54:45 2021 +0300

    all: add validation, imp docs, tests

commit 21b4532afe59f3b89383cb330c9a7d49ec124b6e
Author: Ainar Garipov <A.Garipov@AdGuard.COM>
Date:   Wed Mar 24 19:09:43 2021 +0300

    all: custom autohost tlds
This commit is contained in:
Ainar Garipov
2021-03-25 16:00:27 +03:00
parent ba3fc242ab
commit a7f9e0122b
12 changed files with 389 additions and 121 deletions

View File

@@ -15,6 +15,8 @@ import (
// To transfer information between modules
type dnsContext struct {
// TODO(a.garipov): Remove this and rewrite processors to be methods of
// *Server instead.
srv *Server
proxyCtx *proxy.DNSContext
// setts are the filtering settings for the client.
@@ -75,7 +77,7 @@ func (s *Server) handleDNSRequest(_ *proxy.Proxy, d *proxy.DNSContext) error {
// appropriate handler.
mods := []modProcessFunc{
processInitial,
processInternalHosts,
s.processInternalHosts,
processInternalIPAddrs,
processClientID,
processFilteringBeforeRequest,
@@ -136,7 +138,7 @@ func isHostnameOK(hostname string) bool {
(c >= 'A' && c <= 'Z') ||
(c >= '0' && c <= '9') ||
c == '.' || c == '-') {
log.Debug("DNS: skipping invalid hostname %s from DHCP", hostname)
log.Debug("dns: skipping invalid hostname %s from DHCP", hostname)
return false
}
}
@@ -172,7 +174,7 @@ func (s *Server) onDHCPLeaseChanged(flags int) {
hostToIP[lowhost] = ip
}
log.Debug("DNS: added %d A/PTR entries from DHCP", len(m))
log.Debug("dns: added %d A/PTR entries from DHCP", len(m))
s.tableHostToIPLock.Lock()
s.tableHostToIP = hostToIP
@@ -183,20 +185,22 @@ func (s *Server) onDHCPLeaseChanged(flags int) {
s.tablePTRLock.Unlock()
}
// Respond to A requests if the target host name is associated with a lease from our DHCP server
func processInternalHosts(ctx *dnsContext) (rc resultCode) {
s := ctx.srv
req := ctx.proxyCtx.Req
if !(req.Question[0].Qtype == dns.TypeA || req.Question[0].Qtype == dns.TypeAAAA) {
// processInternalHosts respond to A requests if the target hostname is known to
// the server.
//
// TODO(a.garipov): Adapt to AAAA as well.
func (s *Server) processInternalHosts(dctx *dnsContext) (rc resultCode) {
req := dctx.proxyCtx.Req
q := req.Question[0]
if q.Qtype != dns.TypeA {
return resultCodeSuccess
}
host := req.Question[0].Name
host = strings.ToLower(host)
if !strings.HasSuffix(host, ".lan.") {
reqHost := strings.ToLower(q.Name)
host := strings.TrimSuffix(reqHost, s.autohostSuffix)
if host == reqHost {
return resultCodeSuccess
}
host = strings.TrimSuffix(host, ".lan.")
s.tableHostToIPLock.Lock()
if s.tableHostToIP == nil {
@@ -209,24 +213,22 @@ func processInternalHosts(ctx *dnsContext) (rc resultCode) {
return resultCodeSuccess
}
log.Debug("DNS: internal record: %s -> %s", req.Question[0].Name, ip)
log.Debug("dns: internal record: %s -> %s", q.Name, ip)
resp := s.makeResponse(req)
if req.Question[0].Qtype == dns.TypeA {
a := &dns.A{}
a.Hdr = dns.RR_Header{
Name: req.Question[0].Name,
Rrtype: dns.TypeA,
Ttl: s.conf.BlockedResponseTTL,
Class: dns.ClassINET,
if q.Qtype == dns.TypeA {
a := &dns.A{
Hdr: s.hdr(req, dns.TypeA),
A: make([]byte, len(ip)),
}
a.A = make([]byte, 4)
copy(a.A, ip)
resp.Answer = append(resp.Answer, a)
}
ctx.proxyCtx.Res = resp
dctx.proxyCtx.Res = resp
return resultCodeSuccess
}
@@ -257,7 +259,7 @@ func processInternalIPAddrs(ctx *dnsContext) (rc resultCode) {
return resultCodeSuccess
}
log.Debug("DNS: reverse-lookup: %s -> %s", arpa, host)
log.Debug("dns: reverse-lookup: %s -> %s", arpa, host)
resp := s.makeResponse(req)
ptr := &dns.PTR{}
@@ -325,7 +327,7 @@ func processUpstream(ctx *dnsContext) (rc resultCode) {
if s.conf.EnableDNSSEC {
opt := d.Req.IsEdns0()
if opt == nil {
log.Debug("DNS: Adding OPT record with DNSSEC flag")
log.Debug("dns: Adding OPT record with DNSSEC flag")
d.Req.SetEdns0(4096, true)
} else if !opt.Do() {
opt.SetDo(true)