Pull request: AG-28771 conf upstream mode
Squashed commit of the following: commit afb5a0d8a499bccf7761baea40910f39c92b8a20 Merge: 09ac43c85abf20c6deAuthor: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Dec 25 12:55:45 2023 +0200 Merge remote-tracking branch 'origin/master' into conf-ups-mode commit 09ac43c859ef8cbd3bb0488d1a945589cd59ca19 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Dec 22 14:36:07 2023 +0200 openapi: imp docs commit d0fbd4349e4bddde73c6e92f75854acfc481ac0d Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Dec 22 11:47:10 2023 +0200 all: changelog commit 105f9c50738733b0736a768fb9ee09d2e7fbf42e Merge: 62a2cf12d4bc5c346aAuthor: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Dec 22 11:27:21 2023 +0200 Merge remote-tracking branch 'origin/master' into conf-ups-mode # Conflicts: # openapi/CHANGELOG.md commit 62a2cf12df694611888e840a5041a9c517cdfddb Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Fri Dec 22 10:52:59 2023 +0200 openapi: imp docs commit 87956c49240da44b216489920feff69996e3502b Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Thu Dec 21 12:08:07 2023 +0200 dnsforward: imp code commit bf74d67ad112735d557be3d8fac75964cd99e375 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Dec 20 15:46:38 2023 +0200 dnsforward: imp code commit 3a98dee88809a25118a14a1f07eeecbfccb14cd9 Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Dec 20 15:41:06 2023 +0200 dnsforward: imp code commit 1499da1fa0319ac3ad914171e807446f2c4d2fdb Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Dec 20 13:36:28 2023 +0200 dnsforward: imp code commit 228c61a5a0f73cc13655cef8bdaa1995b3f7fced Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Wed Dec 20 13:06:11 2023 +0200 dnsforward: imp code commit 069ee22c6d904db4e983135ce87a9fe8d12b7e9a Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Dec 19 12:39:25 2023 +0200 dnsforward: imp code commit 90919f99a975862dcb07ac82fb740e4404e48bae Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Dec 19 12:10:43 2023 +0200 confmigrate: fix commit a8c329950423b59098d1f2b16d1da7100dd54f8d Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Tue Dec 19 12:08:05 2023 +0200 dnsforward: imp code commit 58b53ccd97d353fab0df29f13425b5e341c8fdeb Author: Dimitry Kolyshev <dkolyshev@adguard.com> Date: Mon Dec 18 15:10:01 2023 +0200 all: conf upstream mode
This commit is contained in:
@@ -70,7 +70,7 @@ type jsonDNSConfig struct {
|
||||
DisableIPv6 *bool `json:"disable_ipv6"`
|
||||
|
||||
// UpstreamMode defines the way DNS requests are constructed.
|
||||
UpstreamMode *string `json:"upstream_mode"`
|
||||
UpstreamMode *jsonUpstreamMode `json:"upstream_mode"`
|
||||
|
||||
// BlockedResponseTTL is the TTL for blocked responses.
|
||||
BlockedResponseTTL *uint32 `json:"blocked_response_ttl"`
|
||||
@@ -114,6 +114,21 @@ type jsonDNSConfig struct {
|
||||
DefaultLocalPTRUpstreams []string `json:"default_local_ptr_upstreams,omitempty"`
|
||||
}
|
||||
|
||||
// jsonUpstreamMode is a enumeration of upstream modes.
|
||||
type jsonUpstreamMode string
|
||||
|
||||
const (
|
||||
// jsonUpstreamModeEmpty is the default value on frontend, it is used as
|
||||
// jsonUpstreamModeLoadBalance mode.
|
||||
//
|
||||
// Deprecated: Use jsonUpstreamModeLoadBalance instead.
|
||||
jsonUpstreamModeEmpty jsonUpstreamMode = ""
|
||||
|
||||
jsonUpstreamModeLoadBalance jsonUpstreamMode = "load_balance"
|
||||
jsonUpstreamModeParallel jsonUpstreamMode = "parallel"
|
||||
jsonUpstreamModeFastestAddr jsonUpstreamMode = "fastest_addr"
|
||||
)
|
||||
|
||||
func (s *Server) getDNSConfig() (c *jsonDNSConfig) {
|
||||
protectionEnabled, protectionDisabledUntil := s.UpdatedProtectionStatus()
|
||||
|
||||
@@ -145,11 +160,16 @@ func (s *Server) getDNSConfig() (c *jsonDNSConfig) {
|
||||
usePrivateRDNS := s.conf.UsePrivateRDNS
|
||||
localPTRUpstreams := stringutil.CloneSliceOrEmpty(s.conf.LocalPTRResolvers)
|
||||
|
||||
var upstreamMode string
|
||||
if s.conf.FastestAddr {
|
||||
upstreamMode = "fastest_addr"
|
||||
} else if s.conf.AllServers {
|
||||
upstreamMode = "parallel"
|
||||
var upstreamMode jsonUpstreamMode
|
||||
switch s.conf.UpstreamMode {
|
||||
case UpstreamModeLoadBalance:
|
||||
// TODO(d.kolyshev): Support jsonUpstreamModeLoadBalance on frontend instead
|
||||
// of jsonUpstreamModeEmpty.
|
||||
upstreamMode = jsonUpstreamModeEmpty
|
||||
case UpstreamModeParallel:
|
||||
upstreamMode = jsonUpstreamModeParallel
|
||||
case UpstreamModeFastestAddr:
|
||||
upstreamMode = jsonUpstreamModeFastestAddr
|
||||
}
|
||||
|
||||
defPTRUps, err := s.defaultLocalPTRUpstreams()
|
||||
@@ -222,18 +242,22 @@ func (req *jsonDNSConfig) checkBlockingMode() (err error) {
|
||||
return validateBlockingMode(*req.BlockingMode, req.BlockingIPv4, req.BlockingIPv6)
|
||||
}
|
||||
|
||||
// checkUpstreamsMode returns an error if the upstream mode is invalid.
|
||||
func (req *jsonDNSConfig) checkUpstreamsMode() (err error) {
|
||||
// checkUpstreamMode returns an error if the upstream mode is invalid.
|
||||
func (req *jsonDNSConfig) checkUpstreamMode() (err error) {
|
||||
if req.UpstreamMode == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
mode := *req.UpstreamMode
|
||||
if ok := slices.Contains([]string{"", "fastest_addr", "parallel"}, mode); !ok {
|
||||
return fmt.Errorf("upstream_mode: incorrect value %q", mode)
|
||||
switch um := *req.UpstreamMode; um {
|
||||
case
|
||||
jsonUpstreamModeEmpty,
|
||||
jsonUpstreamModeLoadBalance,
|
||||
jsonUpstreamModeParallel,
|
||||
jsonUpstreamModeFastestAddr:
|
||||
return nil
|
||||
default:
|
||||
return fmt.Errorf("upstream_mode: incorrect value %q", um)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// checkBootstrap returns an error if any bootstrap address is invalid.
|
||||
@@ -297,7 +321,7 @@ func (req *jsonDNSConfig) validate(privateNets netutil.SubnetSet) (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
err = req.checkUpstreamsMode()
|
||||
err = req.checkUpstreamMode()
|
||||
if err != nil {
|
||||
// Don't wrap the error since it's informative enough as is.
|
||||
return err
|
||||
@@ -446,8 +470,9 @@ func (s *Server) setConfig(dc *jsonDNSConfig) (shouldRestart bool) {
|
||||
}
|
||||
|
||||
if dc.UpstreamMode != nil {
|
||||
s.conf.AllServers = *dc.UpstreamMode == "parallel"
|
||||
s.conf.FastestAddr = *dc.UpstreamMode == "fastest_addr"
|
||||
s.conf.UpstreamMode = mustParseUpstreamMode(*dc.UpstreamMode)
|
||||
} else {
|
||||
s.conf.UpstreamMode = UpstreamModeLoadBalance
|
||||
}
|
||||
|
||||
if dc.EDNSCSUseCustom != nil && *dc.EDNSCSUseCustom {
|
||||
@@ -460,6 +485,22 @@ func (s *Server) setConfig(dc *jsonDNSConfig) (shouldRestart bool) {
|
||||
return s.setConfigRestartable(dc)
|
||||
}
|
||||
|
||||
// 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 UpstreamModeLoadBalance
|
||||
case jsonUpstreamModeParallel:
|
||||
return UpstreamModeParallel
|
||||
case jsonUpstreamModeFastestAddr:
|
||||
return UpstreamModeFastestAddr
|
||||
default:
|
||||
// Should never happen, since the value should be validated.
|
||||
panic(fmt.Errorf("unexpected upstream mode: %q", mode))
|
||||
}
|
||||
}
|
||||
|
||||
// setIfNotNil sets the value pointed at by currentPtr to the value pointed at
|
||||
// by newPtr if newPtr is not nil. currentPtr must not be nil.
|
||||
func setIfNotNil[T any](currentPtr, newPtr *T) (hasSet bool) {
|
||||
|
||||
Reference in New Issue
Block a user