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 069ee22 commit 228c61a
Showing 1 changed file with 33 additions and 11 deletions.
44 changes: 33 additions & 11 deletions internal/dnsforward/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 *UpstreamMode `json:"upstream_mode"`

// BlockedResponseTTL is the TTL for blocked responses.
BlockedResponseTTL *uint32 `json:"blocked_response_ttl"`
Expand Down Expand Up @@ -114,6 +114,22 @@ type jsonDNSConfig struct {
DefaultLocalPTRUpstreams []string `json:"default_local_ptr_upstreams,omitempty"`
}

// UpstreamMode is a enumeration of upstream modes.
type UpstreamMode string

const (
// UpstreamModeEmpty is the default value on frontend, it is used as
// UpstreamModeLoadBalance mode.
//
// Deprecated
// TODO(d.kolyshev): Use UpstreamModeLoadBalance instead.
UpstreamModeEmpty UpstreamMode = ""

UpstreamModeLoadBalance UpstreamMode = "load_balance"
UpstreamModeParallel UpstreamMode = "parallel"
UpstreamModeFastestAddr UpstreamMode = "fastest_addr"
)

func (s *Server) getDNSConfig() (c *jsonDNSConfig) {
protectionEnabled, protectionDisabledUntil := s.UpdatedProtectionStatus()

Expand Down Expand Up @@ -145,14 +161,16 @@ func (s *Server) getDNSConfig() (c *jsonDNSConfig) {
usePrivateRDNS := s.conf.UsePrivateRDNS
localPTRUpstreams := stringutil.CloneSliceOrEmpty(s.conf.LocalPTRResolvers)

var upstreamMode string
// TODO(d.kolyshev): Use 'load_balance' on frontend instead of nil as a
// default value.
var upstreamMode UpstreamMode
switch s.conf.UpstreamMode {
case UpstreamModeTypeLoadBalance:
// TODO(d.kolyshev): Support UpstreamModeLoadBalance on frontend instead
// of UpstreamModeEmpty.
upstreamMode = UpstreamModeEmpty
case UpstreamModeTypeParallel:
upstreamMode = "parallel"
upstreamMode = UpstreamModeParallel
case UpstreamModeTypeFastestAddr:
upstreamMode = "fastest_addr"
upstreamMode = UpstreamModeFastestAddr
}

defPTRUps, err := s.defaultLocalPTRUpstreams()
Expand Down Expand Up @@ -232,7 +250,11 @@ func (req *jsonDNSConfig) checkUpstreamMode() (err error) {
}

switch um := *req.UpstreamMode; um {
case "", "parallel", "fastest_addr", "load_balance":
case
UpstreamModeEmpty,
UpstreamModeLoadBalance,
UpstreamModeParallel,
UpstreamModeFastestAddr:
return nil
default:
return fmt.Errorf("upstream_mode: incorrect value %q", um)
Expand Down Expand Up @@ -466,13 +488,13 @@ func (s *Server) setConfig(dc *jsonDNSConfig) (shouldRestart bool) {

// mustParseUpstreamMode returns an upstream mode parsed from string. Panics in
// case of invalid value.
func mustParseUpstreamMode(mode string) (um string) {
func mustParseUpstreamMode(mode UpstreamMode) (um string) {
switch mode {
case "", "load_balance":
case UpstreamModeEmpty, UpstreamModeLoadBalance:
return UpstreamModeTypeLoadBalance
case "parallel":
case UpstreamModeParallel:
return UpstreamModeTypeParallel
case "fastest_addr":
case UpstreamModeFastestAddr:
return UpstreamModeTypeFastestAddr
default:
// Should never happen, since the value should be validated.
Expand Down

0 comments on commit 228c61a

Please sign in to comment.