refactor: remove `model.Request.Log` in favor of use `Context`

This commit is contained in:
ThinkChaos 2024-01-27 15:57:57 -05:00
parent 0a47eaad09
commit 73e5d6ab88
9 changed files with 14 additions and 29 deletions

View File

@ -6,7 +6,6 @@ import (
"time" "time"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/sirupsen/logrus"
) )
// ResponseType represents the type of the response ENUM( // ResponseType represents the type of the response ENUM(
@ -67,6 +66,5 @@ type Request struct {
Protocol RequestProtocol Protocol RequestProtocol
ClientNames []string ClientNames []string
Req *dns.Msg Req *dns.Msg
Log *logrus.Entry
RequestTS time.Time RequestTS time.Time
} }

View File

@ -588,7 +588,6 @@ func (r *BlockingResolver) queryForFQIdentifierIPs(ctx context.Context, identifi
for _, qType := range []uint16{dns.TypeA, dns.TypeAAAA} { for _, qType := range []uint16{dns.TypeA, dns.TypeAAAA} {
resp, err := r.next.Resolve(ctx, &model.Request{ resp, err := r.next.Resolve(ctx, &model.Request{
Req: util.NewMsgWithQuestion(identifier, dns.Type(qType)), Req: util.NewMsgWithQuestion(identifier, dns.Type(qType)),
Log: logger,
}) })
if err == nil && resp.Res.Rcode == dns.RcodeSuccess { if err == nil && resp.Res.Rcode == dns.RcodeSuccess {

View File

@ -10,10 +10,8 @@ import (
"sync/atomic" "sync/atomic"
"github.com/0xERR0R/blocky/config" "github.com/0xERR0R/blocky/config"
"github.com/0xERR0R/blocky/log"
"github.com/0xERR0R/blocky/model" "github.com/0xERR0R/blocky/model"
"github.com/0xERR0R/blocky/util" "github.com/0xERR0R/blocky/util"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/mock" "github.com/stretchr/testify/mock"
. "github.com/0xERR0R/blocky/helpertest" . "github.com/0xERR0R/blocky/helpertest"
@ -310,7 +308,6 @@ var _ = Describe("Bootstrap", Label("bootstrap"), func() {
It("uses the bootstrap upstream", func() { It("uses the bootstrap upstream", func() {
mainReq := &model.Request{ mainReq := &model.Request{
Req: util.NewMsgWithQuestion("example.com.", A), Req: util.NewMsgWithQuestion("example.com.", A),
Log: logrus.NewEntry(log.Log()),
} }
mockUpstreamServer := NewMockUDPUpstreamServer().WithAnswerRR("example.com 123 IN A 123.124.122.122") mockUpstreamServer := NewMockUDPUpstreamServer().WithAnswerRR("example.com 123 IN A 123.124.122.122")

View File

@ -110,7 +110,7 @@ func (r *CachingResolver) reloadCacheEntry(ctx context.Context, cacheKey string)
logger.Debugf("prefetching '%s' (%s)", util.Obfuscate(domainName), qType) 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) response, err := r.next.Resolve(ctx, req)
if err == nil { if err == nil {

View File

@ -63,7 +63,7 @@ func (r *ClientNamesResolver) Resolve(ctx context.Context, request *model.Reques
clientNames := r.getClientNames(ctx, request) clientNames := r.getClientNames(ctx, request)
request.ClientNames = clientNames 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) 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{ resp, err := r.externalResolver.Resolve(ctx, &model.Request{
Req: util.NewMsgWithQuestion(reverse, dns.Type(dns.TypePTR)), Req: util.NewMsgWithQuestion(reverse, dns.Type(dns.TypePTR)),
Log: logger,
}) })
if err != nil { if err != nil {
logger.Error("can't resolve client name: ", err) logger.Error("can't resolve client name: ", err)

View File

@ -9,6 +9,7 @@ import (
"github.com/0xERR0R/blocky/model" "github.com/0xERR0R/blocky/model"
"github.com/0xERR0R/blocky/util" "github.com/0xERR0R/blocky/util"
"github.com/miekg/dns" "github.com/miekg/dns"
"github.com/sirupsen/logrus"
) )
// https://www.rfc-editor.org/rfc/rfc7871.html#section-6 // 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 // 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) { func (r *ECSResolver) Resolve(ctx context.Context, request *model.Request) (*model.Response, error) {
if r.cfg.IsEnabled() { if r.cfg.IsEnabled() {
ctx, logger := r.log(ctx)
_ = ctx
so := util.GetEdns0Option[*dns.EDNS0_SUBNET](request.Req) 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 // 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) || if r.cfg.UseAsClient && so != nil && ((so.Family == ecsFamilyIPv4 && so.SourceNetmask == ecsMaskIPv4) ||
(so.Family == ecsFamilyIPv6 && so.SourceNetmask == ecsMaskIPv6)) { (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 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 // 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 { 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 // 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 // and the forwardEcs option is not enabled
if r.cfg.IPv4Mask == 0 && r.cfg.IPv6Mask == 0 && so != nil && !r.cfg.Forward { 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) 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 // 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 // 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 var subIP net.IP
if so != nil && r.cfg.Forward && so.Address != nil { if so != nil && r.cfg.Forward && so.Address != nil {
subIP = so.Address subIP = so.Address
@ -96,7 +100,7 @@ func (r *ECSResolver) setSubnet(so *dns.EDNS0_SUBNET, request *model.Request) {
} }
if edsOption != nil { 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) util.SetEdns0Option(request.Req, edsOption)
} }
} }

View File

@ -15,17 +15,9 @@ import (
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
func newRequest(question string, rType dns.Type, logger ...*logrus.Entry) *model.Request { func newRequest(question string, rType dns.Type) *model.Request {
var loggerEntry *logrus.Entry
if len(logger) == 1 {
loggerEntry = logger[0]
} else {
loggerEntry = logrus.NewEntry(log.Log())
}
return &model.Request{ return &model.Request{
Req: util.NewMsgWithQuestion(question, rType), Req: util.NewMsgWithQuestion(question, rType),
Log: loggerEntry,
Protocol: model.RequestProtocolUDP, Protocol: model.RequestProtocolUDP,
} }
} }
@ -35,7 +27,6 @@ func newRequestWithClient(question string, rType dns.Type, ip string, clientName
ClientIP: net.ParseIP(ip), ClientIP: net.ParseIP(ip),
ClientNames: clientNames, ClientNames: clientNames,
Req: util.NewMsgWithQuestion(question, rType), Req: util.NewMsgWithQuestion(question, rType),
Log: logrus.NewEntry(log.Log()),
RequestTS: time.Time{}, RequestTS: time.Time{},
Protocol: model.RequestProtocolUDP, Protocol: model.RequestProtocolUDP,
} }
@ -59,7 +50,6 @@ func newRequestWithClientID(question string, rType dns.Type, ip, requestClientID
ClientIP: net.ParseIP(ip), ClientIP: net.ParseIP(ip),
RequestClientID: requestClientID, RequestClientID: requestClientID,
Req: util.NewMsgWithQuestion(question, rType), Req: util.NewMsgWithQuestion(question, rType),
Log: logrus.NewEntry(log.Log()),
RequestTS: time.Time{}, RequestTS: time.Time{},
Protocol: model.RequestProtocolUDP, Protocol: model.RequestProtocolUDP,
} }

View File

@ -269,10 +269,9 @@ var _ = Describe("UpstreamTreeResolver", Label("upstreamTreeResolver"), func() {
It("Should use one of the matching resolvers & log warning", func() { It("Should use one of the matching resolvers & log warning", func() {
logger, hook := log.NewMockEntry() logger, hook := log.NewMockEntry()
request := newRequestWithClient("example.com.", A, "0.0.0.0", "name-matches1") ctx, _ = log.NewCtx(ctx, logger)
request.Log = logger
Expect(sut.Resolve(ctx, request)). Expect(sut.Resolve(ctx, newRequestWithClient("example.com.", A, "0.0.0.0", "name-matches1"))).
Should( Should(
SatisfyAll( SatisfyAll(
SatisfyAny( SatisfyAny(

View File

@ -583,7 +583,6 @@ func newRequest(
RequestClientID: clientID, RequestClientID: clientID,
Protocol: protocol, Protocol: protocol,
Req: request, Req: request,
Log: logger,
RequestTS: time.Now(), RequestTS: time.Now(),
} }