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:
Ainar Garipov
2021-04-15 19:00:31 +03:00
parent a1450c5595
commit d83091fc1f
11 changed files with 128 additions and 54 deletions

View File

@@ -237,7 +237,7 @@ func (clients *clientsContainer) handleFindClient(w http.ResponseWriter, r *http
var cj clientJSON
if !ok {
var found bool
cj, found = clients.findTemporary(ip, idStr)
cj, found = clients.findRuntime(ip, idStr)
if !found {
continue
}
@@ -258,9 +258,9 @@ func (clients *clientsContainer) handleFindClient(w http.ResponseWriter, r *http
}
}
// findTemporary looks up the IP in temporary storages, like autohosts or
// blocklists.
func (clients *clientsContainer) findTemporary(ip net.IP, idStr string) (cj clientJSON, found bool) {
// findRuntime looks up the IP in runtime and temporary storages, like
// /etc/hosts tables, DHCP leases, or blocklists.
func (clients *clientsContainer) findRuntime(ip net.IP, idStr string) (cj clientJSON, found bool) {
if ip == nil {
return cj, false
}

View File

@@ -94,10 +94,10 @@ type dnsConfig struct {
FiltersUpdateIntervalHours uint32 `yaml:"filters_update_interval"` // time period to update filters (in hours)
DnsfilterConf dnsfilter.Config `yaml:",inline"`
// AutohostTLD is the top-level domain used for known internal hosts.
// LocalDomainName is the domain name used for known internal hosts.
// For example, a machine called "myhost" can be addressed as
// "myhost.lan" when AutohostTLD is "lan".
AutohostTLD string `yaml:"autohost_tld"`
// "myhost.lan" when LocalDomainName is "lan".
LocalDomainName string `yaml:"local_domain_name"`
// ResolveClients enables and disables resolving clients with RDNS.
ResolveClients bool `yaml:"resolve_clients"`
@@ -156,7 +156,7 @@ var config = configuration{
},
FilteringEnabled: true, // whether or not use filter lists
FiltersUpdateIntervalHours: 24,
AutohostTLD: "lan",
LocalDomainName: "lan",
ResolveClients: true,
},
TLS: tlsConfigSettings{

View File

@@ -67,7 +67,7 @@ func initDNSServer() error {
Stats: Context.stats,
QueryLog: Context.queryLog,
SubnetDetector: Context.subnetDetector,
AutohostTLD: config.DNS.AutohostTLD,
LocalDomain: config.DNS.LocalDomainName,
}
if Context.dhcpServer != nil {
p.DHCPServer = Context.dhcpServer

View File

@@ -14,7 +14,7 @@ import (
)
// currentSchemaVersion is the current schema version.
const currentSchemaVersion = 8
const currentSchemaVersion = 9
// These aliases are provided for convenience.
type (
@@ -74,6 +74,7 @@ func upgradeConfigSchema(oldVersion int, diskConf yobj) (err error) {
upgradeSchema5to6,
upgradeSchema6to7,
upgradeSchema7to8,
upgradeSchema8to9,
}
n := 0
@@ -464,6 +465,43 @@ func upgradeSchema7to8(diskConf yobj) (err error) {
return nil
}
// upgradeSchema8to9 performs the following changes:
//
// # BEFORE:
// 'dns':
// 'autohost_tld': 'lan'
//
// # AFTER:
// 'dns':
// 'local_domain_name': 'lan'
//
func upgradeSchema8to9(diskConf yobj) (err error) {
log.Printf("Upgrade yaml: 8 to 9")
diskConf["schema_version"] = 9
dnsVal, ok := diskConf["dns"]
if !ok {
return nil
}
dns, ok := dnsVal.(yobj)
if !ok {
return fmt.Errorf("unexpected type of dns: %T", dnsVal)
}
autohostTLDVal := dns["autohost_tld"]
autohostTLD, ok := autohostTLDVal.(string)
if !ok {
return fmt.Errorf("undexpected type of dns.autohost_tld: %T", autohostTLDVal)
}
delete(dns, "autohost_tld")
dns["local_domain_name"] = autohostTLD
return nil
}
// TODO(a.garipov): Replace with log.Output when we port it to our logging
// package.
func funcName() string {

View File

@@ -13,7 +13,7 @@ func TestUpgradeSchema1to2(t *testing.T) {
diskConf := testDiskConf(1)
err := upgradeSchema1to2(diskConf)
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, diskConf["schema_version"], 2)
@@ -36,7 +36,7 @@ func TestUpgradeSchema2to3(t *testing.T) {
diskConf := testDiskConf(2)
err := upgradeSchema2to3(diskConf)
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, diskConf["schema_version"], 3)
@@ -74,7 +74,7 @@ func TestUpgradeSchema7to8(t *testing.T) {
}
err := upgradeSchema7to8(oldConf)
require.Nil(t, err)
require.NoError(t, err)
require.Equal(t, oldConf["schema_version"], 8)
@@ -90,6 +90,32 @@ func TestUpgradeSchema7to8(t *testing.T) {
assert.Equal(t, host, newBindHosts[0])
}
func TestUpgradeSchema8to9(t *testing.T) {
const tld = "foo"
oldConf := yobj{
"dns": yobj{
"autohost_tld": tld,
},
"schema_version": 8,
}
err := upgradeSchema8to9(oldConf)
require.NoError(t, err)
require.Equal(t, oldConf["schema_version"], 9)
dnsVal, ok := oldConf["dns"]
require.True(t, ok)
newDNSConf, ok := dnsVal.(yobj)
require.True(t, ok)
localDomainName, ok := newDNSConf["local_domain_name"].(string)
require.True(t, ok)
assert.Equal(t, tld, localDomainName)
}
// assertEqualExcept removes entries from configs and compares them.
func assertEqualExcept(t *testing.T, oldConf, newConf yobj, oldKeys, newKeys []string) {
t.Helper()