Skip to content

Commit

Permalink
Improove filter logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rekby committed Dec 8, 2020
1 parent a297165 commit eb54944
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
10 changes: 9 additions & 1 deletion internal/domain_checker/chains.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//nolint:golint
package domain_checker

import "context"
import (
"context"

"github.com/rekby/lets-proxy2/internal/log"
)

type Any []DomainChecker

Expand All @@ -12,9 +16,11 @@ func (any Any) IsDomainAllowed(ctx context.Context, domain string) (bool, error)
return false, err
}
if res {
log.DebugCtx(ctx, "Allowed by 'any' rule chain: some of subrules allow domain (details in debug log)")
return true, nil
}
}
log.InfoCtx(ctx, "Deny by 'any' rule chain: nothing of subrules allow domain.")
return false, nil
}

Expand All @@ -31,9 +37,11 @@ func (all All) IsDomainAllowed(ctx context.Context, domain string) (bool, error)
return false, err
}
if !res {
log.InfoCtx(ctx, "Deny by 'all' chain: any of subrules denied domain.")
return false, nil
}
}
log.DebugCtx(ctx, "Allow by 'all' chain: all of subrules allow domain.")
return true, nil
}

Expand Down
3 changes: 2 additions & 1 deletion internal/domain_checker/ip_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ hostIP:
}
}
ip := ip
logger.Debug("Domain has not allowed IP address", zap.Stringer("checked_ip", &ip))
logger.Info("Domain has not allowed IP address", zap.Stringer("checked_ip", &ip))
return false, nil
}
logger.Debug("Domain allowed IP address filter")
return true, nil
}

Expand Down
25 changes: 22 additions & 3 deletions internal/domain_checker/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,24 @@ package domain_checker
import (
"context"
"regexp"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/rekby/lets-proxy2/internal/log"
)

type True struct{}

func (True) IsDomainAllowed(ctx context.Context, domain string) (bool, error) {
log.DebugCtx(ctx, "Allow by 'true' rule")
return true, nil
}

type False struct{}

func (False) IsDomainAllowed(ctx context.Context, domain string) (bool, error) {
log.InfoCtx(ctx, "Deny by 'false' rule")
return false, nil
}

Expand All @@ -23,9 +30,15 @@ type Not struct {
}

func (n Not) IsDomainAllowed(ctx context.Context, domain string) (bool, error) {
res, err := n.origin.IsDomainAllowed(ctx, domain)
subRuleRes, err := n.origin.IsDomainAllowed(ctx, domain)
res := !subRuleRes
if err == nil {
return !res, nil
logLevel := zapcore.DebugLevel
if !res {
logLevel = zapcore.InfoLevel
}
log.LevelParamCtx(ctx, logLevel, "'Not' filter (details in debug log)", zap.Bool("result", res))
return res, nil
}
return false, err
}
Expand All @@ -34,7 +47,13 @@ type Regexp regexp.Regexp

func (r *Regexp) IsDomainAllowed(ctx context.Context, domain string) (bool, error) {
reg := (*regexp.Regexp)(r)
return reg.MatchString(domain), nil
result := reg.MatchString(domain)
logLevel := zapcore.DebugLevel
if !result {
logLevel = zapcore.InfoLevel
}
log.LevelParamCtx(ctx, logLevel, "Check if domain allowed by regexp", zap.String("regexp", reg.String()), zap.Bool("result", result))
return result, nil
}

func NewRegexp(r *regexp.Regexp) *Regexp {
Expand Down
5 changes: 5 additions & 0 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ func debugDpanic(logger *zap.Logger, err error, mess string, fields ...zap.Field
}
}

func DebugCtx(ctx context.Context, mess string, fields ...zap.Field) {
logger := zc.L(ctx)
logger.WithOptions(zap.AddCallerSkip(1)).Debug(mess, fields...)
}

func DebugFatal(logger *zap.Logger, err error, mess string, fields ...zap.Field) {
debugFatal(logger, err, mess, fields...)
}
Expand Down

0 comments on commit eb54944

Please sign in to comment.