Skip to content

Commit

Permalink
all: conf upstream mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Dec 18, 2023
1 parent d328327 commit 58b53cc
Show file tree
Hide file tree
Showing 15 changed files with 212 additions and 37 deletions.
2 changes: 1 addition & 1 deletion internal/configmigrate/configmigrate.go
Expand Up @@ -2,4 +2,4 @@
package configmigrate

// LastSchemaVersion is the most recent schema version.
const LastSchemaVersion uint = 27
const LastSchemaVersion uint = 28
82 changes: 82 additions & 0 deletions internal/configmigrate/migrations_internal_test.go
Expand Up @@ -4,6 +4,7 @@ import (
"testing"
"time"

"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
"github.com/AdguardTeam/AdGuardHome/internal/filtering"
"github.com/AdguardTeam/golibs/testutil"
"github.com/AdguardTeam/golibs/timeutil"
Expand Down Expand Up @@ -1646,3 +1647,84 @@ func TestUpgradeSchema26to27(t *testing.T) {
})
}
}

func TestUpgradeSchema27to28(t *testing.T) {
const newSchemaVer = 28

testCases := []struct {
in yobj
want yobj
name string
}{{
name: "empty",
in: yobj{},
want: yobj{
"schema_version": newSchemaVer,
},
}, {
name: "load_balance",
in: yobj{
"dns": yobj{
"all_servers": false,
"fastest_addr": false,
},
},
want: yobj{
"dns": yobj{
"upstream_mode": dnsforward.UpstreamModeTypeLoadBalance,
},
"schema_version": newSchemaVer,
},
}, {
name: "parallel",
in: yobj{
"dns": yobj{
"all_servers": true,
"fastest_addr": false,
},
},
want: yobj{
"dns": yobj{
"upstream_mode": dnsforward.UpstreamModeTypeParallel,
},
"schema_version": newSchemaVer,
},
}, {
name: "parallel_fastest",
in: yobj{
"dns": yobj{
"all_servers": true,
"fastest_addr": true,
},
},
want: yobj{
"dns": yobj{
"upstream_mode": dnsforward.UpstreamModeTypeParallel,
},
"schema_version": newSchemaVer,
},
}, {
name: "load_balance",
in: yobj{
"dns": yobj{
"all_servers": false,
"fastest_addr": true,
},
},
want: yobj{
"dns": yobj{
"upstream_mode": dnsforward.UpstreamModeTypeFastestAddr,
},
"schema_version": newSchemaVer,
},
}}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
err := migrateTo28(tc.in)
require.NoError(t, err)

assert.Equal(t, tc.want, tc.in)
})
}
}
47 changes: 47 additions & 0 deletions internal/configmigrate/v28.go
@@ -0,0 +1,47 @@
package configmigrate

import (
"github.com/AdguardTeam/AdGuardHome/internal/dnsforward"
)

// migrateTo28 performs the following changes:
//
// # BEFORE:
// 'dns':
// 'all_servers': true
// 'fastest_addr': true
// # …
// # …
//
// # AFTER:
// 'dns':
// 'upstream_mode': 'parallel'
// # …
// # …
func migrateTo28(diskConf yobj) (err error) {
diskConf["schema_version"] = 28

dns, ok, err := fieldVal[yobj](diskConf, "dns")
if !ok {
return err
}

allServers, _, _ := fieldVal[bool](dns, "all_servers")
fastestAddr, _, _ := fieldVal[bool](dns, "fastest_addr")

var upstreamModeType string
if allServers {
upstreamModeType = dnsforward.UpstreamModeTypeParallel
} else if fastestAddr {
upstreamModeType = dnsforward.UpstreamModeTypeFastestAddr
} else {
upstreamModeType = dnsforward.UpstreamModeTypeLoadBalance
}

dns["upstream_mode"] = upstreamModeType

delete(dns, "all_servers")
delete(dns, "fastest_addr")

return nil
}
26 changes: 14 additions & 12 deletions internal/dnsforward/config.go
Expand Up @@ -89,12 +89,9 @@ type Config struct {
// servers are not responding.
FallbackDNS []string `yaml:"fallback_dns"`

// AllServers, if true, parallel queries to all configured upstream servers
// are enabled.
AllServers bool `yaml:"all_servers"`

// FastestAddr, if true, use Fastest Address algorithm.
FastestAddr bool `yaml:"fastest_addr"`
// UpstreamMode determines the logic through which upstreams will be used.
// See UpstreamModeType* constants.
UpstreamMode string `yaml:"upstream_mode"`

// FastestTimeout replaces the default timeout for dialing IP addresses
// when FastestAddr is true.
Expand Down Expand Up @@ -294,6 +291,13 @@ type ServerConfig struct {
ServePlainDNS bool
}

// UpstreamMode types representation. See [proxy.UpstreamModeType].
const (
UpstreamModeTypeLoadBalance = "load_balance"
UpstreamModeTypeParallel = "parallel"
UpstreamModeTypeFastestAddr = "fastest_addr"
)

// newProxyConfig creates and validates configuration for the main proxy.
func (s *Server) newProxyConfig() (conf *proxy.Config, err error) {
srvConf := s.conf
Expand Down Expand Up @@ -328,12 +332,10 @@ func (s *Server) newProxyConfig() (conf *proxy.Config, err error) {
conf.CacheSizeBytes = int(srvConf.CacheSize)
}

setProxyUpstreamMode(
conf,
srvConf.AllServers,
srvConf.FastestAddr,
srvConf.FastestTimeout.Duration,
)
err = setProxyUpstreamMode(conf, srvConf.UpstreamMode, srvConf.FastestTimeout.Duration)
if err != nil {
return nil, fmt.Errorf("upstream mode: %w", err)
}

conf.BogusNXDomain, err = parseBogusNXDOMAIN(srvConf.BogusNXDomain)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/dnsforward/dns64_test.go
Expand Up @@ -290,6 +290,7 @@ func TestServer_HandleDNSRequest_dns64(t *testing.T) {
TCPListenAddrs: []*net.TCPAddr{{}},
UseDNS64: true,
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand Down
10 changes: 4 additions & 6 deletions internal/dnsforward/dnsforward.go
Expand Up @@ -703,12 +703,10 @@ func (s *Server) prepareInternalProxy() (err error) {
MaxGoroutines: int(s.conf.MaxGoroutines),
}

setProxyUpstreamMode(
conf,
srvConf.AllServers,
srvConf.FastestAddr,
srvConf.FastestTimeout.Duration,
)
err = setProxyUpstreamMode(conf, srvConf.UpstreamMode, srvConf.FastestTimeout.Duration)
if err != nil {
return fmt.Errorf("invalid upstream mode: %w", err)
}

// TODO(a.garipov): Make a proper constructor for proxy.Proxy.
p := &proxy.Proxy{
Expand Down
29 changes: 25 additions & 4 deletions internal/dnsforward/dnsforward_test.go
Expand Up @@ -177,6 +177,7 @@ func createTestTLS(t *testing.T, tlsConf TLSConfig) (s *Server, certPem []byte)
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand Down Expand Up @@ -305,6 +306,7 @@ func TestServer(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand Down Expand Up @@ -344,6 +346,7 @@ func TestServer_timeout(t *testing.T) {
srvConf := &ServerConfig{
UpstreamTimeout: testTimeout,
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand All @@ -362,6 +365,7 @@ func TestServer_timeout(t *testing.T) {
s, err := NewServer(DNSCreateParams{DNSFilter: createTestDNSFilter(t)})
require.NoError(t, err)

s.conf.Config.UpstreamMode = UpstreamModeTypeLoadBalance
s.conf.Config.EDNSClientSubnet = &EDNSClientSubnet{
Enabled: false,
}
Expand All @@ -379,6 +383,7 @@ func TestServer_Prepare_fallbacks(t *testing.T) {
"#tls://1.1.1.1",
"8.8.8.8",
},
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand All @@ -401,6 +406,7 @@ func TestServerWithProtectionDisabled(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand Down Expand Up @@ -478,7 +484,8 @@ func TestServerRace(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamDNS: []string{"8.8.8.8:53", "8.8.4.4:53"},
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamDNS: []string{"8.8.8.8:53", "8.8.4.4:53"},
},
ConfigModified: func() {},
ServePlainDNS: true,
Expand Down Expand Up @@ -531,6 +538,7 @@ func TestSafeSearch(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -614,6 +622,7 @@ func TestInvalidRequest(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -643,6 +652,7 @@ func TestBlockedRequest(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -678,7 +688,8 @@ func TestServerCustomClientUpstream(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
CacheSize: defaultCacheSize,
CacheSize: defaultCacheSize,
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -756,6 +767,7 @@ func TestBlockCNAMEProtectionEnabled(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -789,6 +801,7 @@ func TestBlockCNAME(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -864,6 +877,7 @@ func TestClientRulesForCNAMEMatching(t *testing.T) {
FilterHandler: func(_ netip.Addr, _ string, settings *filtering.Settings) {
settings.FilteringEnabled = false
},
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -909,6 +923,7 @@ func TestNullBlockedRequest(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -974,7 +989,8 @@ func TestBlockedCustomIP(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamDNS: []string{"8.8.8.8:53", "8.8.4.4:53"},
UpstreamDNS: []string{"8.8.8.8:53", "8.8.4.4:53"},
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -1027,6 +1043,7 @@ func TestBlockedByHosts(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -1078,6 +1095,7 @@ func TestBlockedBySafeBrowsing(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -1136,7 +1154,8 @@ func TestRewrite(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamDNS: []string{"8.8.8.8:53"},
UpstreamDNS: []string{"8.8.8.8:53"},
UpstreamMode: UpstreamModeTypeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -1265,6 +1284,7 @@ func TestPTRResponseFromDHCPLeases(t *testing.T) {
s.conf.TCPListenAddrs = []*net.TCPAddr{{}}
s.conf.UpstreamDNS = []string{"127.0.0.1:53"}
s.conf.Config.EDNSClientSubnet = &EDNSClientSubnet{Enabled: false}
s.conf.Config.UpstreamMode = UpstreamModeTypeLoadBalance

err = s.Prepare(&s.conf)
require.NoError(t, err)
Expand Down Expand Up @@ -1347,6 +1367,7 @@ func TestPTRResponseFromHosts(t *testing.T) {
s.conf.TCPListenAddrs = []*net.TCPAddr{{}}
s.conf.UpstreamDNS = []string{"127.0.0.1:53"}
s.conf.Config.EDNSClientSubnet = &EDNSClientSubnet{Enabled: false}
s.conf.Config.UpstreamMode = UpstreamModeTypeLoadBalance

err = s.Prepare(&s.conf)
require.NoError(t, err)
Expand Down

0 comments on commit 58b53cc

Please sign in to comment.