pancheri/blackhole.go

43 lines
1.0 KiB
Go

package pancheri
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"slices"
)
var (
blockedDomains = promauto.NewGauge(prometheus.GaugeOpts{
Name: "pancheri_blackhole_blocked_domains_total",
Help: "Number of blocked domains on the blackhole blocklist",
})
blockHits = promauto.NewCounter(prometheus.CounterOpts{
Name: "pancheri_blackhole_block_hits_total",
Help: "Number of requests that have hit the blocklist",
})
blockMisses = promauto.NewCounter(prometheus.CounterOpts{
Name: "pancheri_blackhole_misses_total",
Help: "Number of requests that have missed the blocklist",
})
)
type BlackholeFile struct {
DenyDomains []string `yaml:"deny_domains"`
}
type Blackholer struct {
DenyDomains []string
}
func (b *Blackholer) ShouldBlock(domain string) bool {
blockedDomains.Set(float64(len(b.DenyDomains)))
blocked := slices.Contains(b.DenyDomains, domain)
if blocked {
blockHits.Inc()
} else {
blockMisses.Inc()
}
return blocked
}