Skip to content

Commit 8ebddb0

Browse files
committed
feat: migrate to zap for logging
Resolves #19, #7
1 parent cbaf2e8 commit 8ebddb0

25 files changed

+506
-492
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
void
2+
config.yaml
23

34
dist/
45

.pre-commit-config.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ repos:
88
# entry: detect-secrets-hook
99
# args: ['--baseline', '.secrets.baseline']
1010
- repo: https://github.com/golangci/golangci-lint
11-
rev: v1.49.0
11+
rev: v1.52.2
1212
hooks:
1313
- id: golangci-lint
1414
- repo: https://github.com/Bahjat/pre-commit-golang
1515
rev: v1.0.3 # pragma: allowlist secret
1616
hooks:
1717
- id: go-unit-tests
1818
- repo: https://github.com/pre-commit/pre-commit-hooks
19-
rev: v4.3.0
19+
rev: v4.4.0
2020
hooks:
2121
- id: check-json
2222
- id: check-added-large-files
2323
- id: pretty-format-json
2424
- id: check-merge-conflict
2525
- id: check-yaml
2626
- repo: https://github.com/igorshubovych/markdownlint-cli
27-
rev: v0.32.2
27+
rev: v0.34.0
2828
hooks:
2929
- id: markdownlint-fix
3030
- repo: https://github.com/koalaman/shellcheck-precommit
31-
rev: v0.8.0
31+
rev: v0.9.0
3232
hooks:
3333
- id: shellcheck

allow.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,36 @@ import (
44
"context"
55

66
"github.com/miekg/dns"
7-
"go.devnw.com/event"
87
)
98

109
func AllowResolver(
1110
ctx context.Context,
12-
pub *event.Publisher,
11+
logger Logger,
1312
upstream chan<- *Request,
1413
records ...*Record,
1514
) (*Allow, error) {
16-
err := checkNil(ctx, pub)
15+
err := checkNil(ctx, logger)
1716
if err != nil {
1817
return nil, err
1918
}
2019

21-
m, err := NewMatcher(ctx, pub, records...)
20+
m, err := NewMatcher(ctx, logger, records...)
2221
if err != nil {
2322
return nil, err
2423
}
2524

2625
return &Allow{
2726
Matcher: m,
2827
ctx: ctx,
29-
pub: pub,
28+
logger: logger,
3029
upstream: upstream,
3130
}, nil
3231
}
3332

3433
type Allow struct {
3534
*Matcher
3635
ctx context.Context
37-
pub *event.Publisher
36+
logger Logger
3837
upstream chan<- *Request
3938
}
4039

@@ -54,15 +53,13 @@ func (a *Allow) Intercept(
5453
case <-a.ctx.Done():
5554
case <-ctx.Done():
5655
case a.upstream <- req:
57-
a.pub.EventFunc(ctx, func() event.Event {
58-
return &Event{
59-
Msg: "query found in allow list",
60-
Name: req.r.Question[0].Name,
61-
Type: dns.Type(req.r.Question[0].Qtype),
62-
Category: ALLOW,
63-
Record: record,
64-
}
65-
})
56+
a.logger.Debugw(
57+
"matched",
58+
"category", ALLOW,
59+
"name", req.r.Question[0].Name,
60+
"type", dns.Type(req.r.Question[0].Qtype),
61+
"record", record,
62+
)
6663
}
6764

6865
return nil, false

block.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,34 @@ import (
44
"context"
55

66
"github.com/miekg/dns"
7-
"go.devnw.com/event"
87
)
98

109
func BlockResolver(
1110
ctx context.Context,
12-
pub *event.Publisher,
11+
logger Logger,
1312
records ...*Record,
1413
) (*Block, error) {
15-
err := checkNil(ctx, pub)
14+
err := checkNil(ctx, logger)
1615
if err != nil {
1716
return nil, err
1817
}
1918

20-
m, err := NewMatcher(ctx, pub, records...)
19+
m, err := NewMatcher(ctx, logger, records...)
2120
if err != nil {
2221
return nil, err
2322
}
2423

2524
return &Block{
2625
Matcher: m,
2726
ctx: ctx,
28-
pub: pub,
27+
logger: logger,
2928
}, nil
3029
}
3130

3231
type Block struct {
3332
*Matcher
34-
ctx context.Context
35-
pub *event.Publisher
33+
ctx context.Context
34+
logger Logger
3635
}
3736

3837
func (b *Block) Intercept(
@@ -49,22 +48,26 @@ func (b *Block) Intercept(
4948
// Matched a blocked record
5049
err := req.Block()
5150
if err != nil {
52-
b.pub.ErrorFunc(ctx, func() error {
53-
return NewErr(req, err, "failed to block request")
54-
})
51+
b.logger.Errorw(
52+
"failed to block",
53+
"category", BLOCK,
54+
"name", req.r.Question[0].Name,
55+
"type", dns.Type(req.r.Question[0].Qtype),
56+
"record", record,
57+
"client", req.client,
58+
"server", req.server,
59+
)
5560
}
5661

57-
b.pub.EventFunc(ctx, func() event.Event {
58-
return &Event{
59-
Msg: "query found in block list",
60-
Name: req.r.Question[0].Name,
61-
Type: dns.Type(req.r.Question[0].Qtype),
62-
Category: BLOCK,
63-
Record: record,
64-
Server: req.server,
65-
Client: req.client,
66-
}
67-
})
62+
b.logger.Infow(
63+
"matched",
64+
"category", BLOCK,
65+
"name", req.r.Question[0].Name,
66+
"type", dns.Type(req.r.Question[0].Qtype),
67+
"record", record,
68+
"client", req.client,
69+
"server", req.server,
70+
)
6871

6972
return nil, false
7073
}

cache.go

Lines changed: 32 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@ import (
99
"time"
1010

1111
"github.com/miekg/dns"
12-
"go.devnw.com/event"
1312
"go.devnw.com/ttl"
1413
)
1514

1615
type Cache struct {
17-
ctx context.Context
18-
pub *event.Publisher
19-
cache *ttl.Cache[string, *dns.Msg]
16+
ctx context.Context
17+
logger Logger
18+
cache *ttl.Cache[string, *dns.Msg]
2019
}
2120

2221
// Intercept is the cache intercept func which attempts to first pull
@@ -31,43 +30,37 @@ func (c *Cache) Intercept(
3130
if len(req.r.Question) == 0 {
3231
err := req.Block()
3332
if err != nil {
34-
c.pub.ErrorFunc(ctx, func() error {
35-
return Error{
36-
Category: CACHE,
37-
Server: "cache",
38-
Msg: "failed to answer request",
39-
Inner: err,
40-
Record: req.String(),
41-
}
42-
})
33+
c.logger.Errorw(
34+
"invalid question",
35+
"category", CACHE,
36+
"request", req.String(),
37+
"error", err,
38+
)
4339
}
4440
}
4541

4642
r, ok := c.cache.Get(c.ctx, req.Key())
4743
if !ok || r == nil {
4844
// Add hook for final response to cache
4945
req.w = &interceptor{
50-
ctx: c.ctx,
51-
cache: c.cache,
52-
pub: c.pub,
53-
req: req,
54-
next: req.w.WriteMsg, // TODO: Determine if this is the correct pattern
46+
ctx: c.ctx,
47+
cache: c.cache,
48+
logger: c.logger,
49+
req: req,
50+
next: req.w.WriteMsg, // TODO: Determine if this is the correct pattern
5551
}
5652

5753
return req, true
5854
}
5955

6056
err := req.Answer(r.SetReply(req.r))
6157
if err != nil {
62-
c.pub.ErrorFunc(ctx, func() error {
63-
return Error{
64-
Category: CACHE,
65-
Server: "cache",
66-
Msg: "failed to answer request",
67-
Inner: err,
68-
Record: req.String(),
69-
}
70-
})
58+
c.logger.Errorw(
59+
"failed to set reply",
60+
"category", CACHE,
61+
"request", req.String(),
62+
"error", err,
63+
)
7164
}
7265

7366
return req, false
@@ -77,12 +70,12 @@ func (c *Cache) Intercept(
7770
// for future queries so that they are not re-requesting an updated
7871
// IP for an address that has already been queried.
7972
type interceptor struct {
80-
ctx context.Context
81-
cache *ttl.Cache[string, *dns.Msg]
82-
pub *event.Publisher
83-
req *Request
84-
next func(*dns.Msg) error
85-
once sync.Once
73+
ctx context.Context
74+
cache *ttl.Cache[string, *dns.Msg]
75+
logger Logger
76+
req *Request
77+
next func(*dns.Msg) error
78+
once sync.Once
8679
}
8780

8881
func (i *interceptor) WriteMsg(res *dns.Msg) (err error) {
@@ -99,13 +92,12 @@ func (i *interceptor) WriteMsg(res *dns.Msg) (err error) {
9992
return
10093
}
10194

102-
i.pub.EventFunc(i.ctx, func() event.Event {
103-
return &CacheEvent{
104-
Method: WRITE,
105-
Record: i.req.r.Question[0].Name,
106-
TTL: ttl,
107-
}
108-
})
95+
i.logger.Debugw(
96+
"cache",
97+
"method", WRITE,
98+
"record", i.req.r.Question[0].Name,
99+
"ttl", ttl,
100+
)
109101
})
110102

111103
if err != nil {

config.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package main
22

3-
import (
4-
gen "go.structs.dev/gen"
5-
)
6-
7-
// Config defines the configuration options available for void.
8-
type Config struct {
9-
Port int `json:"listen_port"`
10-
TTL int `json:"ttl"`
11-
Local gen.Map[string, Record] `json:"local_records"`
12-
Allow gen.Map[string, Record] `json:"allow_records"`
13-
Deny gen.Map[string, Record] `json:"deny_records"`
14-
CacheDir string `json:"cache_dir"`
15-
}
16-
173
// Type indicates the type of a record to ensure proper analysis.
184
type Type string
195

deployment/config/config.yaml

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -70,17 +70,20 @@
7070
# Logger configures the log location and log rotation settings.
7171
#
7272
# Uses configuration from https://github.com/natefinch/lumberjack/tree/v2.0
73-
logger:
74-
filename: "/var/log/void/void.log"
75-
maxage: 30
76-
# maxsize: 100 # MB
77-
# maxbackups: 10
78-
# localtime: false
79-
compress: true
73+
logger:
74+
# Path to log file or :stdout: for stdout
75+
# Leave empty to log to stderr
76+
filename: "/var/log/void/void.log"
77+
level: "error" # debug, info, warn, error, fatal
78+
format: "json" # or "console"
79+
maxage: 30
80+
maxsize: 100 # MB
81+
maxbackups: 10
82+
localtime: true
83+
compress: true
8084

8185
verbose: false
8286

83-
8487
dns:
8588
#port: 53 # default
8689
#upstream: [ # default

0 commit comments

Comments
 (0)