Skip to content

Commit

Permalink
all: imp code
Browse files Browse the repository at this point in the history
  • Loading branch information
schzhn committed Aug 27, 2024
1 parent 0b1f022 commit 6b3fda0
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 56 deletions.
4 changes: 2 additions & 2 deletions internal/dnsforward/clientid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,8 @@ func TestServer_clientIDFromDNSContext(t *testing.T) {
}

srv := &Server{
conf: ServerConfig{TLSConfig: tlsConf},
logger: slogutil.NewDiscardLogger(),
conf: ServerConfig{TLSConfig: tlsConf},
baseLogger: slogutil.NewDiscardLogger(),
}

var (
Expand Down
5 changes: 1 addition & 4 deletions internal/dnsforward/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ func (s *Server) newProxyConfig() (conf *proxy.Config, err error) {
trustedPrefixes := netutil.UnembedPrefixes(srvConf.TrustedProxies)

conf = &proxy.Config{
Logger: s.baseLogger.With(slogutil.KeyPrefix, "dnsproxy"),
HTTP3: srvConf.ServeHTTP3,
Ratelimit: int(srvConf.Ratelimit),
RatelimitSubnetLenIPv4: srvConf.RatelimitSubnetLenIPv4,
Expand All @@ -342,10 +343,6 @@ func (s *Server) newProxyConfig() (conf *proxy.Config, err error) {
MessageConstructor: s,
}

if s.logger != nil {
conf.Logger = s.logger.With(slogutil.KeyPrefix, "dnsproxy")
}

if srvConf.EDNSClientSubnet.UseCustom {
// TODO(s.chzhen): Use netip.Addr instead of net.IP inside dnsproxy.
conf.EDNSAddr = net.IP(srvConf.EDNSClientSubnet.CustomIP.AsSlice())
Expand Down
19 changes: 7 additions & 12 deletions internal/dnsforward/dnsforward.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,9 @@ type Server struct {
// access drops disallowed clients.
access *accessManager

// logger is used for logging during server routines.
//
// TODO(d.kolyshev): Make it never nil.
// TODO(d.kolyshev): Use this logger.
logger *slog.Logger
// baseLogger is used to create loggers for other entities. It should not
// have a prefix and must not be nil.
baseLogger *slog.Logger

// localDomainSuffix is the suffix used to detect internal hosts. It
// must be a valid domain name plus dots on each side.
Expand Down Expand Up @@ -246,7 +244,7 @@ func NewServer(p DNSCreateParams) (s *Server, err error) {
stats: p.Stats,
queryLog: p.QueryLog,
privateNets: p.PrivateNets,
logger: p.Logger.With(slogutil.KeyPrefix, "dnsforward"),
baseLogger: p.Logger,
// TODO(e.burkov): Use some case-insensitive string comparison.
localDomainSuffix: strings.ToLower(localDomainSuffix),
etcHosts: etcHosts,
Expand Down Expand Up @@ -615,7 +613,7 @@ func (s *Server) prepareInternalDNS() (err error) {
return fmt.Errorf("preparing ipset settings: %w", err)
}

ipsetLogger := s.logger.With(slogutil.KeyPrefix, "ipset")
ipsetLogger := s.baseLogger.With(slogutil.KeyPrefix, "ipset")
s.ipset, err = newIpsetHandler(context.TODO(), ipsetLogger, ipsetList)
if err != nil {
// Don't wrap the error, because it's informative enough as is.
Expand Down Expand Up @@ -685,7 +683,7 @@ func (s *Server) setupAddrProc() {
s.addrProc = client.EmptyAddrProc{}
} else {
c := s.conf.AddrProcConf
c.BaseLogger = s.logger
c.BaseLogger = s.baseLogger
c.DialContext = s.DialContext
c.PrivateSubnets = s.privateNets
c.UsePrivateRDNS = s.conf.UsePrivateRDNS
Expand Down Expand Up @@ -729,6 +727,7 @@ func validateBlockingMode(
func (s *Server) prepareInternalProxy() (err error) {
srvConf := s.conf
conf := &proxy.Config{
Logger: s.baseLogger.With(slogutil.KeyPrefix, "dnsproxy"),
CacheEnabled: true,
CacheSizeBytes: 4096,
PrivateRDNSUpstreamConfig: srvConf.PrivateRDNSUpstreamConfig,
Expand All @@ -741,10 +740,6 @@ func (s *Server) prepareInternalProxy() (err error) {
MessageConstructor: s,
}

if s.logger != nil {
conf.Logger = s.logger.With(slogutil.KeyPrefix, "dnsproxy")
}

err = setProxyUpstreamMode(conf, srvConf.UpstreamMode, srvConf.FastestTimeout.Duration)
if err != nil {
return fmt.Errorf("invalid upstream mode: %w", err)
Expand Down
4 changes: 2 additions & 2 deletions internal/dnsforward/process_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func TestServer_ProcessDHCPHosts_localRestriction(t *testing.T) {
dnsFilter: createTestDNSFilter(t),
dhcpServer: dhcp,
localDomainSuffix: localDomainSuffix,
logger: slogutil.NewDiscardLogger(),
baseLogger: slogutil.NewDiscardLogger(),
}

req := &dns.Msg{
Expand Down Expand Up @@ -567,7 +567,7 @@ func TestServer_ProcessDHCPHosts(t *testing.T) {
dnsFilter: createTestDNSFilter(t),
dhcpServer: testDHCP,
localDomainSuffix: tc.suffix,
logger: slogutil.NewDiscardLogger(),
baseLogger: slogutil.NewDiscardLogger(),
}

req := &dns.Msg{
Expand Down
2 changes: 1 addition & 1 deletion internal/dnsforward/stats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func TestServer_ProcessQueryLogsAndStats(t *testing.T) {
ql := &testQueryLog{}
st := &testStats{}
srv := &Server{
logger: slogutil.NewDiscardLogger(),
baseLogger: slogutil.NewDiscardLogger(),
queryLog: ql,
stats: st,
anonymizer: aghnet.NewIPMut(nil),
Expand Down
19 changes: 4 additions & 15 deletions internal/rdns/rdns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package rdns

import (
"context"
"fmt"
"log/slog"
"net/netip"
"time"
Expand Down Expand Up @@ -96,7 +95,7 @@ func (r *Default) Process(ctx context.Context, ip netip.Addr) (host string, chan

host, ttl, err := r.exchanger.Exchange(ip)
if err != nil {
r.logger.DebugContext(ctx, "resolving ip", "ip", ip, slogutil.KeyError, err)
r.logger.DebugContext(ctx, "resolving", "ip", ip, slogutil.KeyError, err)
}

ttl = max(ttl, r.cacheTTL)
Expand All @@ -108,7 +107,7 @@ func (r *Default) Process(ctx context.Context, ip netip.Addr) (host string, chan

err = r.cache.Set(ip, item)
if err != nil {
r.logger.DebugContext(ctx, "adding item to cache", "item", ip, slogutil.KeyError, err)
r.logger.DebugContext(ctx, "adding item to cache", "key", ip, slogutil.KeyError, err)
}

// TODO(e.burkov): The name doesn't change if it's neither stored in cache
Expand All @@ -125,25 +124,15 @@ func (r *Default) findInCache(ctx context.Context, ip netip.Addr) (host string,
r.logger.DebugContext(
ctx,
"retrieving item from cache",
"item", ip,
"key", ip,
slogutil.KeyError, err,
)
}

return "", true
}

item, ok := val.(*cacheItem)
if !ok {
r.logger.DebugContext(
ctx,
"bad type of cache item",
"item", ip,
"type", fmt.Sprintf("%T", val),
)

return "", true
}
item := val.(*cacheItem)

return item.host, time.Now().After(item.expiry)
}
Expand Down
6 changes: 3 additions & 3 deletions internal/rdns/rdns_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package rdns_test

import (
"context"
"net/netip"
"testing"
"time"
Expand Down Expand Up @@ -114,14 +113,15 @@ func TestDefault_Process(t *testing.T) {
return revAddr2, time.Hour, nil
}

ctx := testutil.ContextWithTimeout(t, testTimeout)
require.EventuallyWithT(t, func(t *assert.CollectT) {
got, changed = r.Process(context.TODO(), ip1)
got, changed = r.Process(ctx, ip1)
assert.True(t, changed)
assert.Equal(t, revAddr2, got)
}, 2*cacheTTL, time.Millisecond*100)

assert.Never(t, func() (changed bool) {
_, changed = r.Process(context.TODO(), ip1)
_, changed = r.Process(testutil.ContextWithTimeout(t, testTimeout), ip1)

return changed
}, 2*cacheTTL, time.Millisecond*100)
Expand Down
23 changes: 6 additions & 17 deletions internal/whois/whois.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/AdguardTeam/golibs/logutil/slogutil"
"github.com/AdguardTeam/golibs/netutil"
"github.com/bluele/gcache"
"github.com/c2h5oh/datasize"
)

const (
Expand Down Expand Up @@ -240,9 +241,9 @@ func (w *Default) query(ctx context.Context, target, serverAddr string) (data []
// queryAll queries WHOIS server and handles redirects.
func (w *Default) queryAll(ctx context.Context, target string) (info map[string]string, err error) {
server := net.JoinHostPort(w.serverAddr, w.portStr)
var data []byte

for range w.maxRedirects {
var data []byte
data, err = w.query(ctx, target, server)
if err != nil {
// Don't wrap the error since it's informative enough as is.
Expand All @@ -252,7 +253,7 @@ func (w *Default) queryAll(ctx context.Context, target string) (info map[string]
w.logger.DebugContext(
ctx,
"received response",
"size", len(data),
"size", datasize.ByteSize(len(data)),
"source", server,
"target", target,
)
Expand Down Expand Up @@ -315,7 +316,7 @@ func (w *Default) requestInfo(
item := toCacheItem(info, w.cacheTTL)
err := w.cache.Set(ip, item)
if err != nil {
w.logger.DebugContext(ctx, "adding item to cache", "item", ip, slogutil.KeyError, err)
w.logger.DebugContext(ctx, "adding item to cache", "key", ip, slogutil.KeyError, err)
}
}()

Expand Down Expand Up @@ -350,27 +351,15 @@ func (w *Default) findInCache(ctx context.Context, ip netip.Addr) (wi *Info, exp
w.logger.DebugContext(
ctx,
"retrieving item from cache",
"item", ip,
"key", ip,
slogutil.KeyError, err,
)
}

return nil, false
}

item, ok := val.(*cacheItem)
if !ok {
w.logger.DebugContext(
ctx,
"bad type of cache item",
"item", ip,
"type", fmt.Sprintf("%T", val),
)

return nil, false
}

return fromCacheItem(item)
return fromCacheItem(val.(*cacheItem))
}

// Info is the filtered WHOIS data for a runtime client.
Expand Down

0 comments on commit 6b3fda0

Please sign in to comment.