Pull request: all: allow local non-top-level domains
Updates #2961. Squashed commit of the following: commit 207eeb85caf6caee81a669302daf4e10a5b61585 Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Thu Apr 15 18:48:50 2021 +0300 all: allow local non-top-level domains
This commit is contained in:
@@ -264,7 +264,7 @@ func (s *Server) processInternalHosts(dctx *dnsContext) (rc resultCode) {
|
||||
}
|
||||
|
||||
reqHost := strings.ToLower(q.Name)
|
||||
host := strings.TrimSuffix(reqHost, s.autohostSuffix)
|
||||
host := strings.TrimSuffix(reqHost, s.localDomainSuffix)
|
||||
if host == reqHost {
|
||||
return resultCodeSuccess
|
||||
}
|
||||
|
||||
@@ -90,7 +90,7 @@ func TestServer_ProcessInternalHosts_localRestriction(t *testing.T) {
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s := &Server{
|
||||
autohostSuffix: defaultAutohostSuffix,
|
||||
localDomainSuffix: defaultLocalDomainSuffix,
|
||||
tableHostToIP: hostToIPTable{
|
||||
"example": knownIP,
|
||||
},
|
||||
@@ -157,35 +157,35 @@ func TestServer_ProcessInternalHosts(t *testing.T) {
|
||||
}{{
|
||||
name: "success_external",
|
||||
host: examplecom,
|
||||
suffix: defaultAutohostSuffix,
|
||||
suffix: defaultLocalDomainSuffix,
|
||||
wantIP: nil,
|
||||
wantRes: resultCodeSuccess,
|
||||
qtyp: dns.TypeA,
|
||||
}, {
|
||||
name: "success_external_non_a",
|
||||
host: examplecom,
|
||||
suffix: defaultAutohostSuffix,
|
||||
suffix: defaultLocalDomainSuffix,
|
||||
wantIP: nil,
|
||||
wantRes: resultCodeSuccess,
|
||||
qtyp: dns.TypeCNAME,
|
||||
}, {
|
||||
name: "success_internal",
|
||||
host: examplelan,
|
||||
suffix: defaultAutohostSuffix,
|
||||
suffix: defaultLocalDomainSuffix,
|
||||
wantIP: knownIP,
|
||||
wantRes: resultCodeSuccess,
|
||||
qtyp: dns.TypeA,
|
||||
}, {
|
||||
name: "success_internal_unknown",
|
||||
host: "example-new.lan",
|
||||
suffix: defaultAutohostSuffix,
|
||||
suffix: defaultLocalDomainSuffix,
|
||||
wantIP: nil,
|
||||
wantRes: resultCodeFinish,
|
||||
qtyp: dns.TypeA,
|
||||
}, {
|
||||
name: "success_internal_aaaa",
|
||||
host: examplelan,
|
||||
suffix: defaultAutohostSuffix,
|
||||
suffix: defaultLocalDomainSuffix,
|
||||
wantIP: nil,
|
||||
wantRes: resultCodeSuccess,
|
||||
qtyp: dns.TypeAAAA,
|
||||
@@ -201,7 +201,7 @@ func TestServer_ProcessInternalHosts(t *testing.T) {
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
s := &Server{
|
||||
autohostSuffix: tc.suffix,
|
||||
localDomainSuffix: tc.suffix,
|
||||
tableHostToIP: hostToIPTable{
|
||||
"example": knownIP,
|
||||
},
|
||||
|
||||
@@ -70,9 +70,9 @@ type Server struct {
|
||||
stats stats.Stats
|
||||
access *accessCtx
|
||||
|
||||
// autohostSuffix is the suffix used to detect internal hosts. It must
|
||||
// be a valid top-level domain plus dots on each side.
|
||||
autohostSuffix string
|
||||
// localDomainSuffix is the suffix used to detect internal hosts. It
|
||||
// must be a valid domain name plus dots on each side.
|
||||
localDomainSuffix string
|
||||
|
||||
ipset ipsetCtx
|
||||
subnetDetector *aghnet.SubnetDetector
|
||||
@@ -94,9 +94,11 @@ type Server struct {
|
||||
conf ServerConfig
|
||||
}
|
||||
|
||||
// defaultAutohostSuffix is the default suffix used to detect internal hosts
|
||||
// when no suffix is provided. See the documentation for Server.autohostSuffix.
|
||||
const defaultAutohostSuffix = ".lan."
|
||||
// defaultLocalDomainSuffix is the default suffix used to detect internal hosts
|
||||
// when no suffix is provided.
|
||||
//
|
||||
// See the documentation for Server.localDomainSuffix.
|
||||
const defaultLocalDomainSuffix = ".lan."
|
||||
|
||||
// DNSCreateParams are parameters to create a new server.
|
||||
type DNSCreateParams struct {
|
||||
@@ -105,11 +107,11 @@ type DNSCreateParams struct {
|
||||
QueryLog querylog.QueryLog
|
||||
DHCPServer dhcpd.ServerInterface
|
||||
SubnetDetector *aghnet.SubnetDetector
|
||||
AutohostTLD string
|
||||
LocalDomain string
|
||||
}
|
||||
|
||||
// tldToSuffix converts a top-level domain into an autohost suffix.
|
||||
func tldToSuffix(tld string) (suffix string) {
|
||||
// domainNameToSuffix converts a domain name into a local domain suffix.
|
||||
func domainNameToSuffix(tld string) (suffix string) {
|
||||
l := len(tld) + 2
|
||||
b := make([]byte, l)
|
||||
b[0] = '.'
|
||||
@@ -122,24 +124,24 @@ func tldToSuffix(tld string) (suffix string) {
|
||||
// NewServer creates a new instance of the dnsforward.Server
|
||||
// Note: this function must be called only once
|
||||
func NewServer(p DNSCreateParams) (s *Server, err error) {
|
||||
var autohostSuffix string
|
||||
if p.AutohostTLD == "" {
|
||||
autohostSuffix = defaultAutohostSuffix
|
||||
var localDomainSuffix string
|
||||
if p.LocalDomain == "" {
|
||||
localDomainSuffix = defaultLocalDomainSuffix
|
||||
} else {
|
||||
err = aghnet.ValidateDomainNameLabel(p.AutohostTLD)
|
||||
err = aghnet.ValidateDomainName(p.LocalDomain)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("autohost tld: %w", err)
|
||||
return nil, fmt.Errorf("local domain: %w", err)
|
||||
}
|
||||
|
||||
autohostSuffix = tldToSuffix(p.AutohostTLD)
|
||||
localDomainSuffix = domainNameToSuffix(p.LocalDomain)
|
||||
}
|
||||
|
||||
s = &Server{
|
||||
dnsFilter: p.DNSFilter,
|
||||
stats: p.Stats,
|
||||
queryLog: p.QueryLog,
|
||||
subnetDetector: p.SubnetDetector,
|
||||
autohostSuffix: autohostSuffix,
|
||||
dnsFilter: p.DNSFilter,
|
||||
stats: p.Stats,
|
||||
queryLog: p.QueryLog,
|
||||
subnetDetector: p.SubnetDetector,
|
||||
localDomainSuffix: localDomainSuffix,
|
||||
}
|
||||
|
||||
if p.DHCPServer != nil {
|
||||
|
||||
@@ -232,10 +232,10 @@ func sendTestMessages(t *testing.T, conn *dns.Conn) {
|
||||
for i := 0; i < testMessagesCount; i++ {
|
||||
req := createGoogleATestMessage()
|
||||
err := conn.WriteMsg(req)
|
||||
assert.Nilf(t, err, "cannot write message #%d: %s", i, err)
|
||||
assert.NoErrorf(t, err, "cannot write message #%d: %s", i, err)
|
||||
|
||||
res, err := conn.ReadMsg()
|
||||
assert.Nilf(t, err, "cannot read response to message #%d: %s", i, err)
|
||||
assert.NoErrorf(t, err, "cannot read response to message #%d: %s", i, err)
|
||||
assertGoogleAResponse(t, res)
|
||||
}
|
||||
}
|
||||
@@ -1088,7 +1088,6 @@ func TestPTRResponseFromHosts(t *testing.T) {
|
||||
_, _ = hf.WriteString(" 127.0.0.1 host # comment \n")
|
||||
_, _ = hf.WriteString(" ::1 localhost#comment \n")
|
||||
|
||||
// Init auto hosts.
|
||||
c.EtcHosts.Init(hf.Name())
|
||||
t.Cleanup(c.EtcHosts.Close)
|
||||
|
||||
@@ -1145,17 +1144,24 @@ func TestNewServer(t *testing.T) {
|
||||
in: DNSCreateParams{},
|
||||
wantErrMsg: "",
|
||||
}, {
|
||||
name: "success_autohost_tld",
|
||||
name: "success_local_tld",
|
||||
in: DNSCreateParams{
|
||||
AutohostTLD: "mynet",
|
||||
LocalDomain: "mynet",
|
||||
},
|
||||
wantErrMsg: "",
|
||||
}, {
|
||||
name: "bad_autohost_tld",
|
||||
name: "success_local_domain",
|
||||
in: DNSCreateParams{
|
||||
AutohostTLD: "!!!",
|
||||
LocalDomain: "my.local.net",
|
||||
},
|
||||
wantErrMsg: `autohost tld: invalid char '!' at index 0 in "!!!"`,
|
||||
wantErrMsg: "",
|
||||
}, {
|
||||
name: "bad_local_domain",
|
||||
in: DNSCreateParams{
|
||||
LocalDomain: "!!!",
|
||||
},
|
||||
wantErrMsg: `local domain: invalid domain name label at index 0: ` +
|
||||
`invalid char '!' at index 0 in "!!!"`,
|
||||
}}
|
||||
|
||||
for _, tc := range testCases {
|
||||
|
||||
Reference in New Issue
Block a user