Skip to content

Commit

Permalink
Pull request 342: Fix config validation
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit f4dc4f3
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Apr 10 18:47:46 2024 +0300

    proxy: imp docs

commit c9ee715
Author: Eugene Burkov <E.Burkov@AdGuard.COM>
Date:   Wed Apr 10 18:39:45 2024 +0300

    proxy: fix default values
  • Loading branch information
EugeneOne1 committed Apr 10, 2024
1 parent 9b75951 commit 78378a1
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package proxy

import (
"cmp"
"context"
"fmt"
"io"
Expand Down Expand Up @@ -200,15 +201,23 @@ type Proxy struct {
}

// New creates a new Proxy with the specified configuration. c must not be nil.
//
// TODO(e.burkov): Cover with tests.
func New(c *Config) (p *Proxy, err error) {
p = &Proxy{
Config: *c,
privateNets: netutil.SubnetSetFunc(netutil.IsLocallyServed),
beforeRequestHandler: noopRequestHandler{},
upstreamRTTStats: map[string]upstreamRTTStats{},
rttLock: sync.Mutex{},
ratelimitLock: sync.Mutex{},
RWMutex: sync.RWMutex{},
Config: *c,
privateNets: cmp.Or[netutil.SubnetSet](
c.PrivateSubnets,
netutil.SubnetSetFunc(netutil.IsLocallyServed),
),
beforeRequestHandler: cmp.Or[BeforeRequestHandler](
c.BeforeRequestHandler,
noopRequestHandler{},
),
upstreamRTTStats: map[string]upstreamRTTStats{},
rttLock: sync.Mutex{},
ratelimitLock: sync.Mutex{},
RWMutex: sync.RWMutex{},
bytesPool: &sync.Pool{
New: func() any {
// 2 bytes may be used to store packet length (see TCP/TLS).
Expand All @@ -217,9 +226,12 @@ func New(c *Config) (p *Proxy, err error) {
return &b
},
},
udpOOBSize: proxynetutil.UDPGetOOBSize(),
time: realClock{},
messages: defaultMessageConstructor{},
udpOOBSize: proxynetutil.UDPGetOOBSize(),
time: realClock{},
messages: cmp.Or[MessageConstructor](
c.MessageConstructor,
defaultMessageConstructor{},
),
recDetector: newRecursionDetector(recursionTTL, cachedRecurrentReqNum),
}

Expand Down Expand Up @@ -260,16 +272,6 @@ func New(c *Config) (p *Proxy, err error) {
return nil, fmt.Errorf("setting up DNS64: %w", err)
}

if c.MessageConstructor != nil {
p.messages = c.MessageConstructor
}
if c.PrivateSubnets != nil {
p.privateNets = c.PrivateSubnets
}
if c.BeforeRequestHandler != nil {
p.beforeRequestHandler = c.BeforeRequestHandler
}

p.RatelimitWhitelist = slices.Clone(p.RatelimitWhitelist)
slices.SortFunc(p.RatelimitWhitelist, netip.Addr.Compare)

Expand Down

0 comments on commit 78378a1

Please sign in to comment.