Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mizzick committed Jul 4, 2024
1 parent 2847564 commit 65b24dc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
1 change: 1 addition & 0 deletions proxy/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
31 changes: 25 additions & 6 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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) {
Expand Down

0 comments on commit 65b24dc

Please sign in to comment.