From 73e5d6ab88f270aac2a34d10c4fb4e8fde7bfeab Mon Sep 17 00:00:00 2001 From: ThinkChaos Date: Sat, 27 Jan 2024 15:57:57 -0500 Subject: [PATCH] refactor: remove `model.Request.Log` in favor of use `Context` --- model/models.go | 2 -- resolver/blocking_resolver.go | 1 - resolver/bootstrap_test.go | 3 --- resolver/caching_resolver.go | 2 +- resolver/client_names_resolver.go | 3 +-- resolver/ecs_resolver.go | 14 +++++++++----- resolver/resolver.go | 12 +----------- resolver/upstream_tree_resolver_test.go | 5 ++--- server/server.go | 1 - 9 files changed, 14 insertions(+), 29 deletions(-) diff --git a/model/models.go b/model/models.go index 43b0292bb..0e86f42ad 100644 --- a/model/models.go +++ b/model/models.go @@ -6,7 +6,6 @@ import ( "time" "github.com/miekg/dns" - "github.com/sirupsen/logrus" ) // ResponseType represents the type of the response ENUM( @@ -67,6 +66,5 @@ type Request struct { Protocol RequestProtocol ClientNames []string Req *dns.Msg - Log *logrus.Entry RequestTS time.Time } diff --git a/resolver/blocking_resolver.go b/resolver/blocking_resolver.go index 7e250efe2..dc3cdab64 100644 --- a/resolver/blocking_resolver.go +++ b/resolver/blocking_resolver.go @@ -588,7 +588,6 @@ func (r *BlockingResolver) queryForFQIdentifierIPs(ctx context.Context, identifi for _, qType := range []uint16{dns.TypeA, dns.TypeAAAA} { resp, err := r.next.Resolve(ctx, &model.Request{ Req: util.NewMsgWithQuestion(identifier, dns.Type(qType)), - Log: logger, }) if err == nil && resp.Res.Rcode == dns.RcodeSuccess { diff --git a/resolver/bootstrap_test.go b/resolver/bootstrap_test.go index d0d4df00a..9c2339e54 100644 --- a/resolver/bootstrap_test.go +++ b/resolver/bootstrap_test.go @@ -10,10 +10,8 @@ import ( "sync/atomic" "github.com/0xERR0R/blocky/config" - "github.com/0xERR0R/blocky/log" "github.com/0xERR0R/blocky/model" "github.com/0xERR0R/blocky/util" - "github.com/sirupsen/logrus" "github.com/stretchr/testify/mock" . "github.com/0xERR0R/blocky/helpertest" @@ -310,7 +308,6 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() { It("uses the bootstrap upstream", func() { mainReq := &model.Request{ Req: util.NewMsgWithQuestion("example.com.", A), - Log: logrus.NewEntry(log.Log()), } mockUpstreamServer := NewMockUDPUpstreamServer().WithAnswerRR("example.com 123 IN A 123.124.122.122") diff --git a/resolver/caching_resolver.go b/resolver/caching_resolver.go index ecc62eef9..e2a052f01 100644 --- a/resolver/caching_resolver.go +++ b/resolver/caching_resolver.go @@ -110,7 +110,7 @@ func (r *CachingResolver) reloadCacheEntry(ctx context.Context, cacheKey string) logger.Debugf("prefetching '%s' (%s)", util.Obfuscate(domainName), qType) - req := newRequest(dns.Fqdn(domainName), qType, logger) + req := newRequest(dns.Fqdn(domainName), qType) response, err := r.next.Resolve(ctx, req) if err == nil { diff --git a/resolver/client_names_resolver.go b/resolver/client_names_resolver.go index 60aecd686..d3df5b543 100644 --- a/resolver/client_names_resolver.go +++ b/resolver/client_names_resolver.go @@ -63,7 +63,7 @@ func (r *ClientNamesResolver) Resolve(ctx context.Context, request *model.Reques clientNames := r.getClientNames(ctx, request) request.ClientNames = clientNames - ctx, request.Log = log.CtxWithFields(ctx, logrus.Fields{"client_names": strings.Join(clientNames, "; ")}) + ctx, _ = log.CtxWithFields(ctx, logrus.Fields{"client_names": strings.Join(clientNames, "; ")}) return r.next.Resolve(ctx, request) } @@ -128,7 +128,6 @@ func (r *ClientNamesResolver) resolveClientNames(ctx context.Context, ip net.IP) resp, err := r.externalResolver.Resolve(ctx, &model.Request{ Req: util.NewMsgWithQuestion(reverse, dns.Type(dns.TypePTR)), - Log: logger, }) if err != nil { logger.Error("can't resolve client name: ", err) diff --git a/resolver/ecs_resolver.go b/resolver/ecs_resolver.go index 1b97eab69..9578e80ce 100644 --- a/resolver/ecs_resolver.go +++ b/resolver/ecs_resolver.go @@ -9,6 +9,7 @@ import ( "github.com/0xERR0R/blocky/model" "github.com/0xERR0R/blocky/util" "github.com/miekg/dns" + "github.com/sirupsen/logrus" ) // https://www.rfc-editor.org/rfc/rfc7871.html#section-6 @@ -49,23 +50,26 @@ func NewECSResolver(cfg config.ECS) ChainedResolver { // and sets the client IP from the EDNS0 option to the request if this option is enabled func (r *ECSResolver) Resolve(ctx context.Context, request *model.Request) (*model.Response, error) { if r.cfg.IsEnabled() { + ctx, logger := r.log(ctx) + _ = ctx + so := util.GetEdns0Option[*dns.EDNS0_SUBNET](request.Req) // Set the client IP from the Edns0 subnet option if the option is enabled and the correct subnet mask is set if r.cfg.UseAsClient && so != nil && ((so.Family == ecsFamilyIPv4 && so.SourceNetmask == ecsMaskIPv4) || (so.Family == ecsFamilyIPv6 && so.SourceNetmask == ecsMaskIPv6)) { - request.Log.Debugf("using request's edns0 address as internal client IP: %s", so.Address) + logger.Debugf("using request's edns0 address as internal client IP: %s", so.Address) request.ClientIP = so.Address } // Set the Edns0 subnet option if the client IP is IPv4 or IPv6 and the masks are set in the configuration if r.cfg.IPv4Mask > 0 || r.cfg.IPv6Mask > 0 { - r.setSubnet(so, request) + r.setSubnet(so, request, logger) } // Remove the Edns0 subnet option if the client IP is IPv4 or IPv6 and the corresponding mask is not set // and the forwardEcs option is not enabled if r.cfg.IPv4Mask == 0 && r.cfg.IPv6Mask == 0 && so != nil && !r.cfg.Forward { - request.Log.Debug("remove edns0 subnet option") + logger.Debug("remove edns0 subnet option") util.RemoveEdns0Option[*dns.EDNS0_SUBNET](request.Req) } } @@ -75,7 +79,7 @@ func (r *ECSResolver) Resolve(ctx context.Context, request *model.Request) (*mod // setSubnet appends the subnet information to the request as EDNS0 option // if the client IP is IPv4 or IPv6 and the corresponding mask is set in the configuration -func (r *ECSResolver) setSubnet(so *dns.EDNS0_SUBNET, request *model.Request) { +func (r *ECSResolver) setSubnet(so *dns.EDNS0_SUBNET, request *model.Request, logger *logrus.Entry) { var subIP net.IP if so != nil && r.cfg.Forward && so.Address != nil { subIP = so.Address @@ -96,7 +100,7 @@ func (r *ECSResolver) setSubnet(so *dns.EDNS0_SUBNET, request *model.Request) { } if edsOption != nil { - request.Log.Debugf("set edns0 subnet option address: %s", edsOption.Address) + logger.Debugf("set edns0 subnet option address: %s", edsOption.Address) util.SetEdns0Option(request.Req, edsOption) } } diff --git a/resolver/resolver.go b/resolver/resolver.go index 1fe5f8c90..dad100dd0 100644 --- a/resolver/resolver.go +++ b/resolver/resolver.go @@ -15,17 +15,9 @@ import ( "github.com/sirupsen/logrus" ) -func newRequest(question string, rType dns.Type, logger ...*logrus.Entry) *model.Request { - var loggerEntry *logrus.Entry - if len(logger) == 1 { - loggerEntry = logger[0] - } else { - loggerEntry = logrus.NewEntry(log.Log()) - } - +func newRequest(question string, rType dns.Type) *model.Request { return &model.Request{ Req: util.NewMsgWithQuestion(question, rType), - Log: loggerEntry, Protocol: model.RequestProtocolUDP, } } @@ -35,7 +27,6 @@ func newRequestWithClient(question string, rType dns.Type, ip string, clientName ClientIP: net.ParseIP(ip), ClientNames: clientNames, Req: util.NewMsgWithQuestion(question, rType), - Log: logrus.NewEntry(log.Log()), RequestTS: time.Time{}, Protocol: model.RequestProtocolUDP, } @@ -59,7 +50,6 @@ func newRequestWithClientID(question string, rType dns.Type, ip, requestClientID ClientIP: net.ParseIP(ip), RequestClientID: requestClientID, Req: util.NewMsgWithQuestion(question, rType), - Log: logrus.NewEntry(log.Log()), RequestTS: time.Time{}, Protocol: model.RequestProtocolUDP, } diff --git a/resolver/upstream_tree_resolver_test.go b/resolver/upstream_tree_resolver_test.go index f2cd07ab7..628182809 100644 --- a/resolver/upstream_tree_resolver_test.go +++ b/resolver/upstream_tree_resolver_test.go @@ -269,10 +269,9 @@ var _ = Describe("UpstreamTreeResolver", Label("upstreamTreeResolver"), func() { It("Should use one of the matching resolvers & log warning", func() { logger, hook := log.NewMockEntry() - request := newRequestWithClient("example.com.", A, "0.0.0.0", "name-matches1") - request.Log = logger + ctx, _ = log.NewCtx(ctx, logger) - Expect(sut.Resolve(ctx, request)). + Expect(sut.Resolve(ctx, newRequestWithClient("example.com.", A, "0.0.0.0", "name-matches1"))). Should( SatisfyAll( SatisfyAny( diff --git a/server/server.go b/server/server.go index 35c1f641e..d3f4d4c88 100644 --- a/server/server.go +++ b/server/server.go @@ -583,7 +583,6 @@ func newRequest( RequestClientID: clientID, Protocol: protocol, Req: request, - Log: logger, RequestTS: time.Now(), }