diff --git a/cmd/rio/main.go b/cmd/rio/main.go index 5956544..8635acc 100644 --- a/cmd/rio/main.go +++ b/cmd/rio/main.go @@ -71,6 +71,8 @@ func handleSubdomain(ctx *context.GlobalContext, domain, cname, path string, req Path: path, Referrer: req.Header.Get("Referer"), UserAgent: req.Header.Get("User-Agent"), + DNT: req.Header.Get("DNT"), + GPC: req.Header.Get("Sec-GPC"), Writer: w, Global: ctx, } diff --git a/internal/context/context.go b/internal/context/context.go index d5b0720..b7c3dfd 100644 --- a/internal/context/context.go +++ b/internal/context/context.go @@ -36,6 +36,8 @@ type Context struct { // HTTP Stuff Referrer string UserAgent string + DNT string + GPC string Writer http.ResponseWriter // Pointer to the global context diff --git a/internal/metrics/metrics.go b/internal/metrics/metrics.go index c3aa1c3..b5403fc 100644 --- a/internal/metrics/metrics.go +++ b/internal/metrics/metrics.go @@ -16,12 +16,18 @@ type MetricConfig struct { Enabled bool } -// Checks if we should send a metric ping to Loki based on the served path. -func (c *MetricConfig) ShouldSendMetrics(path, userAgent string) bool { +// Checks if we should send a metric ping to page-metrics based on the served path. +func (c *MetricConfig) ShouldSendMetrics(path, userAgent, dnt, gpc string) bool { if !strings.HasSuffix(path, ".html") || !c.Enabled { return false } + // Ignore requests where the user have set "Do-Not-Track" or "Do-Not-Sell-My-Data", even though + // there is no user data and we're not selling it. + if dnt == "1" || gpc == "1" { + return false + } + // Filter out bots for _, pattern := range *c.BotUserAgents { if pattern.MatchString(userAgent) { diff --git a/internal/metrics/metrics_test.go b/internal/metrics/metrics_test.go index 0af990a..a15b5ee 100644 --- a/internal/metrics/metrics_test.go +++ b/internal/metrics/metrics_test.go @@ -14,11 +14,19 @@ func TestShouldPing(t *testing.T) { }, } - if cfg.ShouldSendMetrics("/index.html", "random-bot/v23.5") { + if cfg.ShouldSendMetrics("/index.html", "random-bot/v23.5", "", "") { t.Fatalf("Accepted bot user-agent") } - if !cfg.ShouldSendMetrics("/index.html", "Firefox/...") { + if !cfg.ShouldSendMetrics("/index.html", "Firefox/...", "", "") { t.Fatalf("Rejected real user-agent") } + + if cfg.ShouldSendMetrics("/index.html", "Firefox/...", "1", "") { + t.Fatalf("Ignored DNT") + } + + if cfg.ShouldSendMetrics("/index.html", "Firefox/...", "", "1") { + t.Fatalf("Ignored GPC") + } } diff --git a/internal/pages/pages.go b/internal/pages/pages.go index 0ba28ee..9972cdc 100644 --- a/internal/pages/pages.go +++ b/internal/pages/pages.go @@ -124,7 +124,7 @@ func ServeFile(context *context.Context) { context.Writer.Write(content) // Tell Loki about if, if desired - if context.Global.MetricConfig.ShouldSendMetrics(path, context.UserAgent) { + if context.Global.MetricConfig.ShouldSendMetrics(path, context.UserAgent, context.DNT, context.GPC) { context.Global.MetricConfig.SendMetricPing(context.Domain, path, context.Referrer) } }