Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improove filter domain logging #146

Merged
merged 2 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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("Non self or private ip", 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