Skip to content

Commit

Permalink
dnsforward: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Dec 20, 2023
1 parent 3a98dee commit bf74d67
Show file tree
Hide file tree
Showing 13 changed files with 63 additions and 63 deletions.
8 changes: 4 additions & 4 deletions internal/configmigrate/migrations_internal_test.go
Expand Up @@ -1671,7 +1671,7 @@ func TestUpgradeSchema27to28(t *testing.T) {
},
want: yobj{
"dns": yobj{
"upstream_mode": dnsforward.UpstreamModeTypeLoadBalance,
"upstream_mode": dnsforward.UpstreamModeLoadBalance,
},
"schema_version": newSchemaVer,
},
Expand All @@ -1685,7 +1685,7 @@ func TestUpgradeSchema27to28(t *testing.T) {
},
want: yobj{
"dns": yobj{
"upstream_mode": dnsforward.UpstreamModeTypeParallel,
"upstream_mode": dnsforward.UpstreamModeParallel,
},
"schema_version": newSchemaVer,
},
Expand All @@ -1699,7 +1699,7 @@ func TestUpgradeSchema27to28(t *testing.T) {
},
want: yobj{
"dns": yobj{
"upstream_mode": dnsforward.UpstreamModeTypeParallel,
"upstream_mode": dnsforward.UpstreamModeParallel,
},
"schema_version": newSchemaVer,
},
Expand All @@ -1713,7 +1713,7 @@ func TestUpgradeSchema27to28(t *testing.T) {
},
want: yobj{
"dns": yobj{
"upstream_mode": dnsforward.UpstreamModeTypeFastestAddr,
"upstream_mode": dnsforward.UpstreamModeFastestAddr,
},
"schema_version": newSchemaVer,
},
Expand Down
8 changes: 4 additions & 4 deletions internal/configmigrate/v28.go
Expand Up @@ -29,13 +29,13 @@ func migrateTo28(diskConf yobj) (err error) {
allServers, _, _ := fieldVal[bool](dns, "all_servers")
fastestAddr, _, _ := fieldVal[bool](dns, "fastest_addr")

var upstreamModeType dnsforward.UpstreamModeType
var upstreamModeType dnsforward.UpstreamMode
if allServers {
upstreamModeType = dnsforward.UpstreamModeTypeParallel
upstreamModeType = dnsforward.UpstreamModeParallel
} else if fastestAddr {
upstreamModeType = dnsforward.UpstreamModeTypeFastestAddr
upstreamModeType = dnsforward.UpstreamModeFastestAddr
} else {
upstreamModeType = dnsforward.UpstreamModeTypeLoadBalance
upstreamModeType = dnsforward.UpstreamModeLoadBalance
}

dns["upstream_mode"] = upstreamModeType
Expand Down
12 changes: 6 additions & 6 deletions internal/dnsforward/config.go
Expand Up @@ -90,7 +90,7 @@ type Config struct {
FallbackDNS []string `yaml:"fallback_dns"`

// UpstreamMode determines the logic through which upstreams will be used.
UpstreamMode UpstreamModeType `yaml:"upstream_mode"`
UpstreamMode UpstreamMode `yaml:"upstream_mode"`

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

// UpstreamModeType is a enumeration of upstream mode representations. See
// UpstreamMode is a enumeration of upstream mode representations. See
// [proxy.UpstreamModeType].
type UpstreamModeType string
type UpstreamMode string

const (
UpstreamModeTypeLoadBalance UpstreamModeType = "load_balance"
UpstreamModeTypeParallel UpstreamModeType = "parallel"
UpstreamModeTypeFastestAddr UpstreamModeType = "fastest_addr"
UpstreamModeLoadBalance UpstreamMode = "load_balance"
UpstreamModeParallel UpstreamMode = "parallel"
UpstreamModeFastestAddr UpstreamMode = "fastest_addr"
)

// newProxyConfig creates and validates configuration for the main proxy.
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/dns64_test.go
Expand Up @@ -290,7 +290,7 @@ func TestServer_HandleDNSRequest_dns64(t *testing.T) {
TCPListenAddrs: []*net.TCPAddr{{}},
UseDNS64: true,
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand Down
42 changes: 21 additions & 21 deletions internal/dnsforward/dnsforward_test.go
Expand Up @@ -177,7 +177,7 @@ func createTestTLS(t *testing.T, tlsConf TLSConfig) (s *Server, certPem []byte)
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand Down Expand Up @@ -306,7 +306,7 @@ func TestServer(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand Down Expand Up @@ -346,7 +346,7 @@ func TestServer_timeout(t *testing.T) {
srvConf := &ServerConfig{
UpstreamTimeout: testTimeout,
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand All @@ -365,7 +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.UpstreamMode = UpstreamModeLoadBalance
s.conf.Config.EDNSClientSubnet = &EDNSClientSubnet{
Enabled: false,
}
Expand All @@ -383,7 +383,7 @@ func TestServer_Prepare_fallbacks(t *testing.T) {
"#tls://1.1.1.1",
"8.8.8.8",
},
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand All @@ -406,7 +406,7 @@ func TestServerWithProtectionDisabled(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand Down Expand Up @@ -484,7 +484,7 @@ func TestServerRace(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
UpstreamDNS: []string{"8.8.8.8:53", "8.8.4.4:53"},
},
ConfigModified: func() {},
Expand Down Expand Up @@ -538,7 +538,7 @@ func TestSafeSearch(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -622,7 +622,7 @@ func TestInvalidRequest(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -652,7 +652,7 @@ func TestBlockedRequest(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -689,7 +689,7 @@ func TestServerCustomClientUpstream(t *testing.T) {
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
CacheSize: defaultCacheSize,
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -767,7 +767,7 @@ func TestBlockCNAMEProtectionEnabled(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -801,7 +801,7 @@ func TestBlockCNAME(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -877,7 +877,7 @@ func TestClientRulesForCNAMEMatching(t *testing.T) {
FilterHandler: func(_ netip.Addr, _ string, settings *filtering.Settings) {
settings.FilteringEnabled = false
},
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -923,7 +923,7 @@ func TestNullBlockedRequest(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -990,7 +990,7 @@ func TestBlockedCustomIP(t *testing.T) {
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamDNS: []string{"8.8.8.8:53", "8.8.4.4:53"},
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -1043,7 +1043,7 @@ func TestBlockedByHosts(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -1095,7 +1095,7 @@ func TestBlockedBySafeBrowsing(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -1155,7 +1155,7 @@ func TestRewrite(t *testing.T) {
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamDNS: []string{"8.8.8.8:53"},
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down Expand Up @@ -1284,7 +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
s.conf.Config.UpstreamMode = UpstreamModeLoadBalance

err = s.Prepare(&s.conf)
require.NoError(t, err)
Expand Down Expand Up @@ -1367,7 +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
s.conf.Config.UpstreamMode = UpstreamModeLoadBalance

err = s.Prepare(&s.conf)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/dnsrewrite_test.go
Expand Up @@ -38,7 +38,7 @@ func TestServer_FilterDNSRewrite(t *testing.T) {
BlockingMode: filtering.BlockingModeDefault,
}, ServerConfig{
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/filter_test.go
Expand Up @@ -31,7 +31,7 @@ func TestHandleDNSRequest_handleDNSRequest(t *testing.T) {
UDPListenAddrs: []*net.UDPAddr{{}},
TCPListenAddrs: []*net.TCPAddr{{}},
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{
Enabled: false,
},
Expand Down
20 changes: 10 additions & 10 deletions internal/dnsforward/http.go
Expand Up @@ -163,13 +163,13 @@ func (s *Server) getDNSConfig() (c *jsonDNSConfig) {

var upstreamMode jsonUpstreamMode
switch s.conf.UpstreamMode {
case UpstreamModeTypeLoadBalance:
case UpstreamModeLoadBalance:
// TODO(d.kolyshev): Support jsonUpstreamModeLoadBalance on frontend instead
// of jsonUpstreamModeEmpty.
upstreamMode = jsonUpstreamModeEmpty
case UpstreamModeTypeParallel:
case UpstreamModeParallel:
upstreamMode = jsonUpstreamModeParallel
case UpstreamModeTypeFastestAddr:
case UpstreamModeFastestAddr:
upstreamMode = jsonUpstreamModeFastestAddr
}

Expand Down Expand Up @@ -473,7 +473,7 @@ func (s *Server) setConfig(dc *jsonDNSConfig) (shouldRestart bool) {
if dc.UpstreamMode != nil {
s.conf.UpstreamMode = mustParseUpstreamMode(*dc.UpstreamMode)
} else {
s.conf.UpstreamMode = UpstreamModeTypeLoadBalance
s.conf.UpstreamMode = UpstreamModeLoadBalance
}

if dc.EDNSCSUseCustom != nil && *dc.EDNSCSUseCustom {
Expand All @@ -486,16 +486,16 @@ func (s *Server) setConfig(dc *jsonDNSConfig) (shouldRestart bool) {
return s.setConfigRestartable(dc)
}

// mustParseUpstreamMode returns an upstream mode parsed from string. Panics in
// case of invalid value.
func mustParseUpstreamMode(mode jsonUpstreamMode) (um UpstreamModeType) {
// mustParseUpstreamMode returns an upstream mode parsed from jsonUpstreamMode.
// Panics in case of invalid value.
func mustParseUpstreamMode(mode jsonUpstreamMode) (um UpstreamMode) {
switch mode {
case jsonUpstreamModeEmpty, jsonUpstreamModeLoadBalance:
return UpstreamModeTypeLoadBalance
return UpstreamModeLoadBalance
case jsonUpstreamModeParallel:
return UpstreamModeTypeParallel
return UpstreamModeParallel
case jsonUpstreamModeFastestAddr:
return UpstreamModeTypeFastestAddr
return UpstreamModeFastestAddr
default:
// Should never happen, since the value should be validated.
panic(fmt.Errorf("unexpected upstream mode: %q", mode))
Expand Down
10 changes: 5 additions & 5 deletions internal/dnsforward/http_test.go
Expand Up @@ -77,7 +77,7 @@ func TestDNSForwardHTTP_handleGetConfig(t *testing.T) {
FallbackDNS: []string{"9.9.9.10"},
RatelimitSubnetLenIPv4: 24,
RatelimitSubnetLenIPv6: 56,
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ConfigModified: func() {},
Expand All @@ -104,15 +104,15 @@ func TestDNSForwardHTTP_handleGetConfig(t *testing.T) {
}, {
conf: func() ServerConfig {
conf := defaultConf
conf.UpstreamMode = UpstreamModeTypeFastestAddr
conf.UpstreamMode = UpstreamModeFastestAddr

return conf
},
name: "fastest_addr",
}, {
conf: func() ServerConfig {
conf := defaultConf
conf.UpstreamMode = UpstreamModeTypeParallel
conf.UpstreamMode = UpstreamModeParallel

return conf
},
Expand Down Expand Up @@ -158,7 +158,7 @@ func TestDNSForwardHTTP_handleSetConfig(t *testing.T) {
UpstreamDNS: []string{"8.8.8.8:53", "8.8.4.4:53"},
RatelimitSubnetLenIPv4: 24,
RatelimitSubnetLenIPv6: 56,
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ConfigModified: func() {},
Expand Down Expand Up @@ -525,7 +525,7 @@ func TestServer_HandleTestUpstreamDNS(t *testing.T) {
TCPListenAddrs: []*net.TCPAddr{{}},
UpstreamTimeout: upsTimeout,
Config: Config{
UpstreamMode: UpstreamModeTypeLoadBalance,
UpstreamMode: UpstreamModeLoadBalance,
EDNSClientSubnet: &EDNSClientSubnet{Enabled: false},
},
ServePlainDNS: true,
Expand Down

0 comments on commit bf74d67

Please sign in to comment.