Skip to content

Commit 070615c

Browse files
committed
better logging and HTTP/DNS
1 parent 1dfff1c commit 070615c

6 files changed

Lines changed: 204 additions & 70 deletions

File tree

Corefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ open-blocklist.internal {
33
forward . 172.30.0.10:5353
44

55
cache 10
6-
log
6+
# log
77
errors
88
}
99

@@ -14,14 +14,14 @@ open-blocklist-etcd.internal {
1414
}
1515

1616
cache 10
17-
log
17+
# log
1818
errors
1919
}
2020

2121

2222
in-addr.arpa {
2323
forward . 172.30.0.10:5353
2424
cache 30
25-
log
25+
# log
2626
errors
2727
}

docker-compose.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ services:
3131

3232

3333
open-blocklist:
34-
image: ${OPEN_BLOCKLIST_IMAGE:-ghcr.io/nashcom/open-blocklist:latest}
34+
image: ${OPENBL_IMAGE:-ghcr.io/nashcom/open-blocklist:latest}
3535
container_name: open-blocklist-main
3636
hostname: open-blocklist
3737

@@ -43,8 +43,8 @@ services:
4343
ipv4_address: 172.30.0.10
4444

4545
environment:
46-
- OPENBL_ETCD_ENDPOINT=http://172.30.0.20:2379
47-
- OPENBL_LOGLEVEL=DEBUG
46+
OPENBL_ETCD_ENDPOINT: http://172.30.0.20:2379
47+
OPENBL_LOGLEVEL: ${OPENBL_LOGLEVEL:-INFO}
4848

4949
ports:
5050
- "${LOOKUP_HOST_PORT:-8080}:8080"

src/dns.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main
66
import (
77
"fmt"
88
"strings"
9+
"time"
910
"github.com/miekg/dns"
1011
)
1112

@@ -53,6 +54,9 @@ func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
5354
msg.SetReply(r)
5455
msg.Authoritative = true
5556

57+
start := time.Now()
58+
status := ""
59+
5660
if len(r.Question) == 0 {
5761
msg.Rcode = dns.RcodeFormatError
5862
_ = w.WriteMsg(msg)
@@ -66,9 +70,6 @@ func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
6670
}
6771

6872
q := r.Question[0]
69-
queryType := dns.TypeToString[q.Qtype]
70-
71-
logMsg(LOG_DEBUG, "[DNS Requery]: Type=%s Name=%s", queryType, q.Name)
7273

7374
switch q.Qtype {
7475

@@ -78,7 +79,7 @@ func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
7879

7980
msg.Rcode = dns.RcodeRefused
8081

81-
logMsg(LOG_VERBOSE, "[DNS Result/Refused]: Type=%s Name=%s", queryType, q.Name)
82+
status = "Refused"
8283
stats.ReqDnsWrongZone.Add(1)
8384
break // fall through to w.WriteMsg
8485
}
@@ -88,7 +89,7 @@ func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
8889
// Malformed label count — not a valid RBL query
8990
msg.Rcode = dns.RcodeFormatError
9091

91-
logMsg(LOG_VERBOSE, "[DNS Result/Malformed]: Type=%s Name=%s", queryType, q.Name)
92+
status = "Malformed"
9293
stats.ReqDnsInvalidQuery.Add(1)
9394
break
9495
}
@@ -100,25 +101,23 @@ func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
100101
msg.Answer = append(msg.Answer, rr)
101102
msg.Rcode = dns.RcodeSuccess
102103

103-
logMsg(LOG_VERBOSE, "[DNS Result/Found]: Type=%s Name=%s", queryType, q.Name)
104+
status = "Found"
104105
stats.ReqDnsBlocked.Add(1)
105106

106107
} else {
107108
// Known zone, not listed -> NXDOMAIN (RBL semantics)
108109
msg.Rcode = dns.RcodeNameError
109110

110-
logMsg(LOG_VERBOSE, "[DNS Result/NotFound]: Type=%s Name=%s", queryType, q.Name)
111+
status = "NotFound"
111112
stats.ReqDnsNotListed.Add(1)
112113
}
113114

114115
case dns.TypePTR:
115116

116-
logMsg(LOG_VERBOSE, "[DNS Query/PTR]: name=%s type=%s", q.Name, queryType)
117-
118117
if !dns.IsSubDomain("in-addr.arpa.", q.Name) {
119118
msg.Rcode = dns.RcodeRefused
120119

121-
logMsg(LOG_VERBOSE, "[DNS Result/NO-IN-ARPA]: Type=%s Name=%s", queryType, q.Name)
120+
status = "WrongZone"
122121
stats.ReqDnsWrongZone.Add(1)
123122
break
124123
}
@@ -127,7 +126,7 @@ func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
127126
if !ok {
128127
msg.Rcode = dns.RcodeFormatError
129128

130-
logMsg(LOG_VERBOSE, "[DNS Result/Invalid Query]: name=%s type=%s", q.Name, queryType)
129+
status = "Invalid IP"
131130
stats.ReqDnsInvalidQuery.Add(1)
132131
break
133132
}
@@ -136,30 +135,31 @@ func handleDNSRequest(w dns.ResponseWriter, r *dns.Msg) {
136135
_ , blocked := ipTableLookup(ip)
137136

138137
if blocked {
139-
rr, _ := dns.NewRR(q.Name + " 10 IN PTR " + reverseHost + ".")
138+
rr, _ := dns.NewRR(q.Name + " 10 IN PTR " + gReverseHost + ".")
140139
msg.Answer = append(msg.Answer, rr)
141140
msg.Rcode = dns.RcodeSuccess
142141

143-
logMsg(LOG_VERBOSE, "[DNS Result/Blocked]: name=%s type=%s", q.Name, queryType)
142+
status = "Blocked"
144143
stats.ReqDnsBlocked.Add(1)
145144

146145
} else {
147146
// Not listed -> NXDOMAIN (RBL semantics)
148147
msg.Rcode = dns.RcodeNameError
149148

150-
logMsg(LOG_VERBOSE, "[DNS Result/NotFound]: name=%s type=%s", q.Name, queryType)
149+
status = "NotFound"
151150
stats.ReqDnsNotListed.Add(1)
152151
}
153152

154153
default:
155154
msg.Rcode = dns.RcodeRefused
156-
157-
logMsg(LOG_VERBOSE, "[DNS Result/Unhandled]: name=%s type=%s", q.Name, queryType)
155+
status = "Unhandled"
158156
stats.ReqDnsOtherQueryType.Add(1)
159157
break
160158
}
161159

162160
_ = w.WriteMsg(msg)
161+
162+
logDnsReq(r, start, status)
163163
}
164164

165165
// rblQueryToIP extracts and reverses the IP address encoded in an RBL query name.

src/helpers.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,14 +299,14 @@ func reverseKey(ip net.IP) string {
299299
p := ipv4Parts(ip)
300300

301301
return fmt.Sprintf("%s/arpa/in-addr/%s/%s/%s/%s",
302-
skyPrefix,
302+
SKY_DNS_PREFIX,
303303
p[0], p[1], p[2], p[3])
304304
}
305305

306306
p := ipv6Nibbles(ip)
307307

308308
return fmt.Sprintf("%s/arpa/ip6/%s",
309-
skyPrefix,
309+
SKY_DNS_PREFIX,
310310
strings.Join(p, "/"))
311311
}
312312

src/log.go

Lines changed: 76 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@ package main
66
import (
77
"log"
88
"fmt"
9+
"net/http"
910
"os"
1011
"time"
12+
"github.com/miekg/dns"
1113
)
1214

15+
1316
func showCfg(description, variableName, defaultValue, currentValue any) {
1417
logMsg(LOG_INFO, "%-34s %-40s %-40v %v", variableName, description, defaultValue, currentValue)
1518
}
@@ -42,16 +45,16 @@ func (l LogLevel) LowerCaseStr() string {
4245
}
4346
}
4447

45-
func logLine(msg string) {
48+
func logLine(level LogLevel, msg string) {
4649

4750
ts := time.Now().UTC().Format(time.RFC3339)
4851

4952
if gLogJSON {
50-
log.Printf(`{"ts":"%s","type":"%s","msg":%q}`, ts, "event", msg)
53+
log.Printf(`{"ts":"%s","type":"event","level":"%s","msg":%q}`, ts, level.LowerCaseStr(), msg)
5154
return
5255
}
5356

54-
log.Println(ts + " " + msg)
57+
log.Println(ts + " " + level.String() + ": " + msg)
5558
}
5659

5760
func logSpace() {
@@ -60,7 +63,12 @@ func logSpace() {
6063
return
6164
}
6265

63-
logLine("")
66+
if gLogLevel < LOG_INFO {
67+
return
68+
}
69+
70+
ts := time.Now().UTC().Format(time.RFC3339)
71+
log.Println(ts)
6472
}
6573

6674
func logMsg(level LogLevel, format string, args ...any) {
@@ -69,12 +77,12 @@ func logMsg(level LogLevel, format string, args ...any) {
6977
return
7078
}
7179

72-
logLine(fmt.Sprintf(format, args ...))
80+
logLine(level, fmt.Sprintf(format, args ...))
7381
}
7482

7583
func logFatal(format string, args ...any) {
7684

77-
logLine(fmt.Sprintf(format, args ...))
85+
logLine(LOG_ERROR, fmt.Sprintf(format, args ...))
7886
os.Exit(1)
7987
}
8088

@@ -86,4 +94,65 @@ func logListerner(info string, addr string) {
8694
} else {
8795
logMsg (LOG_INFO, "Listening [%-10s] on %s", info, addr)
8896
}
89-
}
97+
}
98+
99+
func logHttpReq(r *http.Request, start time.Time, requestName string, status string) {
100+
101+
duration := time.Since(start)
102+
ts := time.Now().UTC().Format(time.RFC3339)
103+
104+
if gLogJSON {
105+
log.Printf(`{"ts":"%s","type":"http","request":%q,"method":%q,"uri":%q,"duration_seconds":%f,"status":%q}`,
106+
ts,
107+
requestName,
108+
r.Method,
109+
r.RequestURI,
110+
duration.Seconds(),
111+
status,
112+
)
113+
return
114+
}
115+
116+
if gLogLevel < LOG_DEBUG {
117+
return
118+
}
119+
120+
logMsg(LOG_DEBUG, "[HTTP-REQ: %s] %s %s %v (%.6f seconds) -> %s",
121+
requestName,
122+
r.Method,
123+
r.RequestURI,
124+
duration,
125+
duration.Seconds(),
126+
status,
127+
)
128+
}
129+
130+
func logDnsReq(r *dns.Msg, start time.Time, status string) {
131+
132+
duration := time.Since(start)
133+
ts := time.Now().UTC().Format(time.RFC3339)
134+
q := r.Question[0]
135+
136+
if gLogJSON {
137+
log.Printf(`{"ts":"%s","type":"dns","request":%q,"qtype":%s,"duration_seconds":%f,"status":%s}`,
138+
ts,
139+
q.Name,
140+
dns.TypeToString[q.Qtype],
141+
duration.Seconds(),
142+
status,
143+
)
144+
return
145+
}
146+
147+
if gLogLevel < LOG_DEBUG {
148+
return
149+
}
150+
151+
logMsg(LOG_DEBUG, "[DNS Requery: %s] %s %v (%.6f seconds) -> %s",
152+
dns.TypeToString[q.Qtype],
153+
q.Name,
154+
duration,
155+
duration.Seconds(),
156+
status,
157+
)
158+
}

0 commit comments

Comments
 (0)