Write on the margins of the internet. Powered by the AT Protocol.
margin.at
extension
web
atproto
comments
1package middleware
2
3import (
4 "log"
5 "net/http"
6 "net/url"
7 "strings"
8 "time"
9
10 "github.com/go-chi/chi/v5/middleware"
11)
12
13func PrivacyLogger(next http.Handler) http.Handler {
14 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
15 ww := middleware.NewWrapResponseWriter(w, r.ProtoMajor)
16 t1 := time.Now()
17
18 defer func() {
19 safeURL := redactURL(r.URL)
20
21 log.Printf("[%d] %s %s %s",
22 ww.Status(),
23 r.Method,
24 safeURL,
25 time.Since(t1),
26 )
27 }()
28
29 next.ServeHTTP(ww, r)
30 })
31}
32
33func redactURL(u *url.URL) string {
34 redacted := *u
35 q := redacted.Query()
36
37 sensitiveKeys := []string{"source", "url", "target", "parent", "root", "uri"}
38
39 for _, key := range sensitiveKeys {
40 if q.Has(key) {
41 val := q.Get(key)
42 if strings.Contains(val, "margin.at") {
43 continue
44 }
45 q.Set(key, "[REDACTED]")
46 }
47 }
48
49 redacted.RawQuery = q.Encode()
50 return redacted.String()
51}