Skip to content

Commit

Permalink
refactor: remove model.Request.Log in favor of use Context
Browse files Browse the repository at this point in the history
  • Loading branch information
ThinkChaos committed Mar 19, 2024
1 parent 0a47eaa commit 73e5d6a
Show file tree
Hide file tree
Showing 9 changed files with 14 additions and 29 deletions.
2 changes: 0 additions & 2 deletions model/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/miekg/dns"
"github.com/sirupsen/logrus"
)

// ResponseType represents the type of the response ENUM(
Expand Down Expand Up @@ -67,6 +66,5 @@ type Request struct {
Protocol RequestProtocol
ClientNames []string
Req *dns.Msg
Log *logrus.Entry
RequestTS time.Time
}
1 change: 0 additions & 1 deletion resolver/blocking_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions resolver/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion resolver/caching_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 1 addition & 2 deletions resolver/client_names_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions resolver/ecs_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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
Expand All @@ -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)
}
}
Expand Down
12 changes: 1 addition & 11 deletions resolver/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand All @@ -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,
}
Expand All @@ -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,
}
Expand Down
5 changes: 2 additions & 3 deletions resolver/upstream_tree_resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
1 change: 0 additions & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,6 @@ func newRequest(
RequestClientID: clientID,
Protocol: protocol,
Req: request,
Log: logger,
RequestTS: time.Now(),
}

Expand Down

0 comments on commit 73e5d6a

Please sign in to comment.