diff --git a/main.go b/main.go index 460e434ea..7aaa43d86 100644 --- a/main.go +++ b/main.go @@ -67,7 +67,8 @@ type Options struct { EDNSAddr string `yaml:"edns-addr" long:"edns-addr" description:"Send EDNS Client Address"` // UpstreamMode determines the logic through which upstreams will be used. - UpstreamMode string `yaml:"upstream-mode" long:"upstream-mode" description:"" optional:"yes" optional-value:"load_balance"` + // If not specified the [proxy.UpstreamModeLoadBalance] is used. + UpstreamMode string `yaml:"upstream-mode" long:"upstream-mode" description:"Upstreams logic mode" optional:"yes" optional-value:"load_balance"` // ListenAddrs is the list of server's listen addresses. ListenAddrs []string `yaml:"listen-addrs" short:"l" long:"listen" description:"Listening addresses"` diff --git a/proxy/config.go b/proxy/config.go index 6f5768332..b15b6a7f2 100644 --- a/proxy/config.go +++ b/proxy/config.go @@ -106,6 +106,7 @@ type Config struct { HTTPSServerName string // UpstreamMode determines the logic through which upstreams will be used. + // If not specified the [proxy.UpstreamModeLoadBalance] is used. UpstreamMode UpstreamMode // UDPListenAddr is the set of UDP addresses to listen for plain diff --git a/proxy/proxy.go b/proxy/proxy.go index d5195972c..bfba4074a 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -262,12 +262,9 @@ func New(c *Config) (p *Proxy, err error) { p.requestsSema = syncutil.EmptySemaphore{} } - p.logger.Info("upstream mode is set", "mode", p.UpstreamMode) - if p.UpstreamMode == UpstreamModeFastestAddr { - p.fastestAddr = fastip.New(&fastip.Config{ - Logger: p.Logger, - PingWaitTimeout: p.FastestPingTimeout, - }) + err = p.setupUpstreamMode() + if err != nil { + return nil, fmt.Errorf("setting up upstream mode: %w", err) } err = p.setupDNS64() @@ -281,6 +278,28 @@ func New(c *Config) (p *Proxy, err error) { return p, nil } +// setupUpstreamMode validates and sets up upstream mode for proxy. +func (p *Proxy) setupUpstreamMode() (err error) { + switch p.UpstreamMode { + case "": + p.UpstreamMode = UpstreamModeLoadBalance + case UpstreamModeFastestAddr, UpstreamModeLoadBalance, UpstreamModeParallel: + // Go on. + default: + return fmt.Errorf("bad upstream mode: %q", p.UpstreamMode) + } + + p.logger.Info("upstream mode is set", "mode", p.UpstreamMode) + if p.UpstreamMode == UpstreamModeFastestAddr { + p.fastestAddr = fastip.New(&fastip.Config{ + Logger: p.Logger, + PingWaitTimeout: p.FastestPingTimeout, + }) + } + + return nil +} + // validateBasicAuth validates the basic-auth mode settings if p.Config.Userinfo // is set. func (p *Proxy) validateBasicAuth() (err error) {