From a9ee9ba9167b1b12939373b856b1dda3c3211d35 Mon Sep 17 00:00:00 2001 From: Quentin McGaw Date: Mon, 7 Aug 2023 14:18:03 +0000 Subject: [PATCH] chore(all): prefer local over imported interfaces --- cmd/dns/main.go | 2 +- internal/dns/loop.go | 2 +- internal/health/client.go | 6 --- internal/metrics/setup.go | 8 +--- internal/models/server.go | 7 ---- internal/server/exchange.go | 3 +- internal/server/handler.go | 12 ++---- internal/server/interfaces.go | 29 ++++++++++++++ internal/setup/cache.go | 7 ++-- internal/setup/filter.go | 5 +-- internal/setup/interfaces.go | 27 ++++++++++--- internal/setup/metrics.go | 10 ++++- pkg/blockbuilder/builder.go | 7 ---- pkg/cache/cache.go | 11 ------ pkg/cache/lru/interfaces.go | 15 ++++++++ pkg/cache/lru/lru.go | 3 +- pkg/cache/lru/settings.go | 3 +- pkg/cache/metrics/interface.go | 49 ------------------------ pkg/cache/noop/interfaces.go | 5 +++ pkg/cache/noop/noop.go | 3 +- pkg/cache/noop/settings.go | 3 +- pkg/doh/interfaces.go | 30 +++++++++++++++ pkg/doh/metrics/interface.go | 23 ----------- pkg/doh/metrics/noop/metrics.go | 26 +++---------- pkg/doh/metrics/prometheus/interfaces.go | 6 +++ pkg/doh/metrics/prometheus/metrics.go | 6 +-- pkg/doh/metrics/prometheus/settings.go | 3 +- pkg/doh/server.go | 3 +- pkg/doh/settings.go | 14 +++---- pkg/dot/dial.go | 6 +-- pkg/dot/interfaces.go | 29 ++++++++++++++ pkg/dot/metrics/interface.go | 22 ----------- pkg/dot/metrics/noop/metrics.go | 12 +----- pkg/dot/server.go | 3 +- pkg/dot/settings.go | 14 +++---- pkg/filter/interface.go | 19 --------- pkg/filter/mapfilter/filter.go | 4 +- pkg/filter/mapfilter/interfaces.go | 9 +++++ pkg/filter/mapfilter/settings.go | 3 +- pkg/filter/metrics/interface.go | 20 ---------- pkg/log/interface.go | 19 --------- pkg/middlewares/metrics/interface.go | 21 ---------- pkg/middlewares/metrics/interfaces.go | 11 ++++++ pkg/middlewares/metrics/middleware.go | 4 +- pkg/middlewares/metrics/settings.go | 2 +- 45 files changed, 212 insertions(+), 314 deletions(-) delete mode 100644 internal/models/server.go create mode 100644 internal/server/interfaces.go delete mode 100644 pkg/cache/cache.go create mode 100644 pkg/cache/lru/interfaces.go delete mode 100644 pkg/cache/metrics/interface.go create mode 100644 pkg/cache/noop/interfaces.go delete mode 100644 pkg/doh/metrics/interface.go create mode 100644 pkg/doh/metrics/prometheus/interfaces.go delete mode 100644 pkg/dot/metrics/interface.go delete mode 100644 pkg/filter/interface.go create mode 100644 pkg/filter/mapfilter/interfaces.go delete mode 100644 pkg/filter/metrics/interface.go delete mode 100644 pkg/log/interface.go delete mode 100644 pkg/middlewares/metrics/interface.go create mode 100644 pkg/middlewares/metrics/interfaces.go diff --git a/cmd/dns/main.go b/cmd/dns/main.go index 8297c86c..31ae7945 100644 --- a/cmd/dns/main.go +++ b/cmd/dns/main.go @@ -136,7 +136,7 @@ func _main(ctx context.Context, buildInfo models.BuildInformation, //nolint:cycl client := &http.Client{Timeout: clientTimeout} blockBuilder := setup.BuildBlockBuilder(settings.Block, client) prometheusRegistry := prometheus.NewRegistry() - cacheMetrics, err := setup.CacheMetrics(settings.Metrics, prometheusRegistry) + cacheMetrics, err := setup.BuildCacheMetrics(settings.Metrics, prometheusRegistry) if err != nil { return fmt.Errorf("cache metrics: %w", err) } diff --git a/internal/dns/loop.go b/internal/dns/loop.go index c8d1ecb1..186aa499 100644 --- a/internal/dns/loop.go +++ b/internal/dns/loop.go @@ -140,7 +140,7 @@ func (l *loop) runSubsequent(ctx context.Context, ready chan<- struct{}) (err er func (l *loop) setupAll(ctx context.Context, downloadBlockFiles bool) ( //nolint:ireturn dnsServer Service, err error) { - filterMetrics, err := setup.FilterMetrics(l.settings.Metrics, l.prometheusRegistry) + filterMetrics, err := setup.BuildFilterMetrics(l.settings.Metrics, l.prometheusRegistry) if err != nil { return nil, fmt.Errorf("setting up filter metrics: %w", err) } diff --git a/internal/health/client.go b/internal/health/client.go index 23b60824..16a6ec0c 100644 --- a/internal/health/client.go +++ b/internal/health/client.go @@ -13,12 +13,6 @@ func IsClientMode(args []string) bool { return len(args) > 1 && args[1] == "healthcheck" } -var _ Querier = (*Client)(nil) - -type Querier interface { - Query(ctx context.Context) error -} - type Client struct { *http.Client } diff --git a/internal/metrics/setup.go b/internal/metrics/setup.go index 88585015..74d46c6c 100644 --- a/internal/metrics/setup.go +++ b/internal/metrics/setup.go @@ -11,12 +11,8 @@ import ( "github.com/qdm12/log" ) -type Logger interface { +type ParentLogger interface { New(options ...log.Option) *log.Logger - Debug(s string) - Info(s string) - Warn(s string) - Error(s string) } type PrometheusGatherer interface { @@ -30,7 +26,7 @@ type Service interface { } func New(settings settings.Metrics, //nolint:ireturn - parentLogger Logger, prometheusGatherer PrometheusGatherer) ( + parentLogger ParentLogger, prometheusGatherer PrometheusGatherer) ( service Service, err error) { switch settings.Type { case "noop": diff --git a/internal/models/server.go b/internal/models/server.go deleted file mode 100644 index 2f71be29..00000000 --- a/internal/models/server.go +++ /dev/null @@ -1,7 +0,0 @@ -package models - -import "context" - -type Server interface { - Run(ctx context.Context, crashed chan<- error) -} diff --git a/internal/server/exchange.go b/internal/server/exchange.go index f18dfa9c..ed3f69c9 100644 --- a/internal/server/exchange.go +++ b/internal/server/exchange.go @@ -6,7 +6,6 @@ import ( "net" "github.com/miekg/dns" - "github.com/qdm12/dns/v2/pkg/log" ) type Exchange = func(ctx context.Context, request *dns.Msg) ( @@ -15,7 +14,7 @@ type Exchange = func(ctx context.Context, request *dns.Msg) ( type Dial = func(ctx context.Context, _, _ string) (net.Conn, error) -func NewExchange(name string, dial Dial, warner log.Warner) Exchange { +func NewExchange(name string, dial Dial, warner Warner) Exchange { client := &dns.Client{} return func(ctx context.Context, request *dns.Msg) (response *dns.Msg, err error) { netConn, err := dial(ctx, "", "") diff --git a/internal/server/handler.go b/internal/server/handler.go index 8cf1938f..4c3f218e 100644 --- a/internal/server/handler.go +++ b/internal/server/handler.go @@ -4,9 +4,6 @@ import ( "context" "github.com/miekg/dns" - "github.com/qdm12/dns/v2/pkg/cache" - "github.com/qdm12/dns/v2/pkg/filter" - "github.com/qdm12/dns/v2/pkg/log" ) var _ dns.Handler = (*Handler)(nil) @@ -14,14 +11,13 @@ var _ dns.Handler = (*Handler)(nil) type Handler struct { ctx context.Context //nolint:containedctx exchange Exchange - filter filter.Interface - cache cache.Interface - logger log.Logger + filter Filter + cache Cache + logger Logger } func New(ctx context.Context, exchange Exchange, - filter filter.Interface, cache cache.Interface, - logger log.Logger) *Handler { + filter Filter, cache Cache, logger Logger) *Handler { return &Handler{ ctx: ctx, exchange: exchange, diff --git a/internal/server/interfaces.go b/internal/server/interfaces.go new file mode 100644 index 00000000..b13cd7c5 --- /dev/null +++ b/internal/server/interfaces.go @@ -0,0 +1,29 @@ +package server + +import ( + "github.com/miekg/dns" + "github.com/qdm12/dns/v2/pkg/filter/update" +) + +type Filter interface { + FilterRequest(request *dns.Msg) (blocked bool) + FilterResponse(response *dns.Msg) (blocked bool) + Update(settings update.Settings) +} + +type Cache interface { + Add(request, response *dns.Msg) + Get(request *dns.Msg) (response *dns.Msg) + Remove(request *dns.Msg) +} + +type Logger interface { + Debug(s string) + Info(s string) + Warner + Error(s string) +} + +type Warner interface { + Warn(s string) +} diff --git a/internal/setup/cache.go b/internal/setup/cache.go index 15513472..147a2cce 100644 --- a/internal/setup/cache.go +++ b/internal/setup/cache.go @@ -7,7 +7,6 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/qdm12/dns/v2/internal/config/settings" "github.com/qdm12/dns/v2/pkg/cache/lru" - "github.com/qdm12/dns/v2/pkg/cache/metrics" noopmetrics "github.com/qdm12/dns/v2/pkg/cache/metrics/noop" prommetrics "github.com/qdm12/dns/v2/pkg/cache/metrics/prometheus" "github.com/qdm12/dns/v2/pkg/cache/noop" @@ -21,7 +20,7 @@ type Cache interface { } func BuildCache(userSettings settings.Cache, //nolint:ireturn - metrics metrics.Interface) ( + metrics CacheMetrics) ( cache Cache) { switch userSettings.Type { case noop.CacheType: @@ -36,9 +35,9 @@ func BuildCache(userSettings settings.Cache, //nolint:ireturn } } -func CacheMetrics(userSettings settings.Metrics, //nolint:ireturn +func BuildCacheMetrics(userSettings settings.Metrics, //nolint:ireturn registry prometheus.Registerer) ( - metrics metrics.Interface, err error) { + metrics CacheMetrics, err error) { switch userSettings.Type { case noopString: return noopmetrics.New(), nil diff --git a/internal/setup/filter.go b/internal/setup/filter.go index 0c979564..5a984f5c 100644 --- a/internal/setup/filter.go +++ b/internal/setup/filter.go @@ -5,15 +5,14 @@ import ( "github.com/prometheus/client_golang/prometheus" "github.com/qdm12/dns/v2/internal/config/settings" - "github.com/qdm12/dns/v2/pkg/filter/metrics" noopmetrics "github.com/qdm12/dns/v2/pkg/filter/metrics/noop" prommetrics "github.com/qdm12/dns/v2/pkg/filter/metrics/prometheus" promcommon "github.com/qdm12/dns/v2/pkg/metrics/prometheus" ) -func FilterMetrics(userSettings settings.Metrics, //nolint:ireturn +func BuildFilterMetrics(userSettings settings.Metrics, //nolint:ireturn registry prometheus.Registerer) ( - metrics metrics.Interface, err error) { + metrics FilterMetrics, err error) { switch userSettings.Type { case noopString: return noopmetrics.New(), nil diff --git a/internal/setup/interfaces.go b/internal/setup/interfaces.go index 747d4e37..671ece9e 100644 --- a/internal/setup/interfaces.go +++ b/internal/setup/interfaces.go @@ -19,17 +19,20 @@ type Filter interface { Update(settings update.Settings) } +type FilterMetrics interface { + SetBlockedHostnames(n int) + SetBlockedIPs(n int) + SetBlockedIPPrefixes(n int) + HostnamesFilteredInc(qClass, qType string) + IPsFilteredInc(rrtype string) +} + type Middleware interface { Wrap(next dns.Handler) dns.Handler } type PrometheusRegisterer prometheus.Registerer -type Metrics interface { - DoTMetrics - DoHMetrics -} - type DoTMetrics interface { DoTDialInc(provider, address, outcome string) DNSDialInc(address, outcome string) @@ -40,3 +43,17 @@ type DoHMetrics interface { DoTDialInc(provider, address, outcome string) DNSDialInc(address, outcome string) } + +type CacheMetrics interface { //nolint:interfacebloat + SetCacheType(cacheType string) + CacheInsertInc() + CacheRemoveInc() + CacheMoveInc() + CacheGetEmptyInc() + CacheInsertEmptyInc() + CacheRemoveEmptyInc() + CacheHitInc() + CacheMissInc() + CacheExpiredInc() + CacheMaxEntriesSet(maxEntries uint) +} diff --git a/internal/setup/metrics.go b/internal/setup/metrics.go index 74bf7d18..f3e5c09d 100644 --- a/internal/setup/metrics.go +++ b/internal/setup/metrics.go @@ -12,7 +12,15 @@ import ( func middlewareMetrics(metricsType string, commonPrometheus prometheus.Settings) ( middleware *metricsmiddleware.Middleware, err error) { - var metrics metricsmiddleware.Interface + var metrics interface { + RequestsInc() + QuestionsInc(class, qType string) + RcodeInc(rcode string) + AnswersInc(class, qType string) + ResponsesInc() + InFlightRequestsInc() + InFlightRequestsDec() + } switch metricsType { case noopString: metrics = noopmetrics.New() diff --git a/pkg/blockbuilder/builder.go b/pkg/blockbuilder/builder.go index e504b60b..9958eaac 100644 --- a/pkg/blockbuilder/builder.go +++ b/pkg/blockbuilder/builder.go @@ -1,17 +1,10 @@ package blockbuilder import ( - "context" "net/http" "net/netip" ) -var _ Interface = (*Builder)(nil) - -type Interface interface { - BuildAll(ctx context.Context) Result -} - func New(settings Settings) *Builder { settings.SetDefaults() diff --git a/pkg/cache/cache.go b/pkg/cache/cache.go deleted file mode 100644 index b22ec703..00000000 --- a/pkg/cache/cache.go +++ /dev/null @@ -1,11 +0,0 @@ -package cache - -import ( - "github.com/miekg/dns" -) - -type Interface interface { - Add(request, response *dns.Msg) - Get(request *dns.Msg) (response *dns.Msg) - Remove(request *dns.Msg) -} diff --git a/pkg/cache/lru/interfaces.go b/pkg/cache/lru/interfaces.go new file mode 100644 index 00000000..5db271f2 --- /dev/null +++ b/pkg/cache/lru/interfaces.go @@ -0,0 +1,15 @@ +package lru + +type Metrics interface { //nolint:interfacebloat + SetCacheType(cacheType string) + CacheInsertInc() + CacheRemoveInc() + CacheMoveInc() + CacheGetEmptyInc() + CacheInsertEmptyInc() + CacheRemoveEmptyInc() + CacheHitInc() + CacheMissInc() + CacheExpiredInc() + CacheMaxEntriesSet(maxEntries uint) +} diff --git a/pkg/cache/lru/lru.go b/pkg/cache/lru/lru.go index 13ff84c1..a71dbeab 100644 --- a/pkg/cache/lru/lru.go +++ b/pkg/cache/lru/lru.go @@ -6,7 +6,6 @@ import ( "time" "github.com/miekg/dns" - "github.com/qdm12/dns/v2/pkg/cache/metrics" ) type LRU struct { @@ -19,7 +18,7 @@ type LRU struct { mutex sync.Mutex // External objects - metrics metrics.Interface + metrics Metrics // Mock fields timeNow func() time.Time diff --git a/pkg/cache/lru/settings.go b/pkg/cache/lru/settings.go index a70faf42..5779beac 100644 --- a/pkg/cache/lru/settings.go +++ b/pkg/cache/lru/settings.go @@ -1,7 +1,6 @@ package lru import ( - "github.com/qdm12/dns/v2/pkg/cache/metrics" "github.com/qdm12/dns/v2/pkg/cache/metrics/noop" "github.com/qdm12/gosettings" "github.com/qdm12/gotree" @@ -13,7 +12,7 @@ type Settings struct { MaxEntries uint // Metrics is the metrics interface to record metric information // for the cache. It defaults to a No-Op metric implementation. - Metrics metrics.Interface + Metrics Metrics } func (s *Settings) SetDefaults() { diff --git a/pkg/cache/metrics/interface.go b/pkg/cache/metrics/interface.go deleted file mode 100644 index c92f3134..00000000 --- a/pkg/cache/metrics/interface.go +++ /dev/null @@ -1,49 +0,0 @@ -// Package metrics defines an interface valid for all caches. -package metrics - -import ( - "fmt" - - "github.com/qdm12/dns/v2/pkg/cache/metrics/noop" - "github.com/qdm12/dns/v2/pkg/cache/metrics/prometheus" -) - -var ( - _ Interface = (*prometheus.Metrics)(nil) - _ Interface = (*noop.Metrics)(nil) -) - -type Interface interface { //nolint:interfacebloat - SetCacheType(cacheType string) - CacheInsertInc() - CacheRemoveInc() - CacheMoveInc() - CacheGetEmptyInc() - CacheInsertEmptyInc() - CacheRemoveEmptyInc() - CacheHitInc() - CacheMissInc() - CacheExpiredInc() - CacheMaxEntriesSet(maxEntries uint) -} - -type Settings struct { - Type string - Prometheus prometheus.Settings -} - -func Metrics(settings Settings) (metrics Interface, err error) { //nolint:ireturn - switch settings.Type { - case "noop": - return noop.New(), nil - case "prometheus": - metrics, err = prometheus.New(settings.Prometheus) - if err != nil { - return nil, fmt.Errorf("setting up Prometheus metrics: %w", err) - } - default: - panic(fmt.Sprintf("unknown metrics type: %s", settings.Type)) - } - - return metrics, nil -} diff --git a/pkg/cache/noop/interfaces.go b/pkg/cache/noop/interfaces.go new file mode 100644 index 00000000..d37f4389 --- /dev/null +++ b/pkg/cache/noop/interfaces.go @@ -0,0 +1,5 @@ +package noop + +type Metrics interface { + SetCacheType(cacheType string) +} diff --git a/pkg/cache/noop/noop.go b/pkg/cache/noop/noop.go index 97e90550..5bc965f9 100644 --- a/pkg/cache/noop/noop.go +++ b/pkg/cache/noop/noop.go @@ -2,11 +2,10 @@ package noop import ( "github.com/miekg/dns" - "github.com/qdm12/dns/v2/pkg/cache/metrics" ) type NoOp struct { - metrics metrics.Interface + metrics Metrics } func New(settings Settings) *NoOp { diff --git a/pkg/cache/noop/settings.go b/pkg/cache/noop/settings.go index 26c82dd3..7970a0ea 100644 --- a/pkg/cache/noop/settings.go +++ b/pkg/cache/noop/settings.go @@ -1,7 +1,6 @@ package noop import ( - "github.com/qdm12/dns/v2/pkg/cache/metrics" "github.com/qdm12/dns/v2/pkg/cache/metrics/noop" "github.com/qdm12/gosettings" "github.com/qdm12/gotree" @@ -10,7 +9,7 @@ import ( type Settings struct { // Metrics is the metrics interface to record the cache type. // It defaults to a No-Op metric implementation. - Metrics metrics.Interface + Metrics Metrics } func (s *Settings) SetDefaults() { diff --git a/pkg/doh/interfaces.go b/pkg/doh/interfaces.go index 41365ad9..7f583abd 100644 --- a/pkg/doh/interfaces.go +++ b/pkg/doh/interfaces.go @@ -2,6 +2,7 @@ package doh import ( "github.com/miekg/dns" + "github.com/qdm12/dns/v2/pkg/filter/update" "github.com/qdm12/dns/v2/pkg/provider" ) @@ -9,6 +10,35 @@ type Middleware interface { Wrap(next dns.Handler) dns.Handler } +type Filter interface { + FilterRequest(request *dns.Msg) (blocked bool) + FilterResponse(response *dns.Msg) (blocked bool) + Update(settings update.Settings) +} + +type Cache interface { + Add(request, response *dns.Msg) + Get(request *dns.Msg) (response *dns.Msg) + Remove(request *dns.Msg) +} + +type Metrics interface { + DoHDialInc(url string) + DoTDialInc(provider, address, outcome string) + DNSDialInc(address, outcome string) +} + +type Logger interface { + Debug(s string) + Info(s string) + Warner + Error(s string) +} + +type Warner interface { + Warn(s string) +} + type Picker interface { DoHServer(servers []provider.DoHServer) provider.DoHServer } diff --git a/pkg/doh/metrics/interface.go b/pkg/doh/metrics/interface.go deleted file mode 100644 index 5cef0db6..00000000 --- a/pkg/doh/metrics/interface.go +++ /dev/null @@ -1,23 +0,0 @@ -// Package metrics defines metric interfaces for the -// DoH server and resolver. -package metrics - -import ( - "github.com/qdm12/dns/v2/pkg/doh/metrics/noop" - "github.com/qdm12/dns/v2/pkg/doh/metrics/prometheus" - dotmetrics "github.com/qdm12/dns/v2/pkg/dot/metrics" -) - -var ( - _ DialMetrics = (*prometheus.Metrics)(nil) - _ DialMetrics = (*noop.Metrics)(nil) -) - -type DialMetrics interface { - DoHDialMetrics - dotmetrics.DialMetrics -} - -type DoHDialMetrics interface { - DoHDialInc(url string) -} diff --git a/pkg/doh/metrics/noop/metrics.go b/pkg/doh/metrics/noop/metrics.go index c20ec23d..b5139368 100644 --- a/pkg/doh/metrics/noop/metrics.go +++ b/pkg/doh/metrics/noop/metrics.go @@ -1,28 +1,12 @@ // Package noop defines a No-op metric implementation for DoH. package noop -import ( - dotmetrics "github.com/qdm12/dns/v2/pkg/dot/metrics" - dotnoop "github.com/qdm12/dns/v2/pkg/dot/metrics/noop" - middleware "github.com/qdm12/dns/v2/pkg/middlewares/metrics" - middlewarenoop "github.com/qdm12/dns/v2/pkg/middlewares/metrics/noop" -) - -type ( - dotDialMetrics = dotmetrics.DialMetrics - middlewareInterface = middleware.Interface -) - -type Metrics struct { - dotDialMetrics - middlewareInterface -} +type Metrics struct{} func New() (metrics *Metrics) { - return &Metrics{ - dotDialMetrics: dotnoop.New(), - middlewareInterface: middlewarenoop.New(), - } + return &Metrics{} } -func (m *Metrics) DoHDialInc(string) {} +func (m *Metrics) DNSDialInc(_, _ string) {} +func (m *Metrics) DoTDialInc(_, _, _ string) {} +func (m *Metrics) DoHDialInc(string) {} diff --git a/pkg/doh/metrics/prometheus/interfaces.go b/pkg/doh/metrics/prometheus/interfaces.go new file mode 100644 index 00000000..8712d1e0 --- /dev/null +++ b/pkg/doh/metrics/prometheus/interfaces.go @@ -0,0 +1,6 @@ +package prometheus + +type DialMetrics interface { + DoTDialInc(provider, address, outcome string) + DNSDialInc(address, outcome string) +} diff --git a/pkg/doh/metrics/prometheus/metrics.go b/pkg/doh/metrics/prometheus/metrics.go index 0b35ed65..69aa8d90 100644 --- a/pkg/doh/metrics/prometheus/metrics.go +++ b/pkg/doh/metrics/prometheus/metrics.go @@ -3,12 +3,12 @@ package prometheus import ( "fmt" - - dotmetrics "github.com/qdm12/dns/v2/pkg/dot/metrics" ) type ( - dotDialMetrics = dotmetrics.DialMetrics + // unexported alias so it is not exposed through + // the Metrics struct. + dotDialMetrics = DialMetrics ) type Metrics struct { diff --git a/pkg/doh/metrics/prometheus/settings.go b/pkg/doh/metrics/prometheus/settings.go index af034be3..7a6232ef 100644 --- a/pkg/doh/metrics/prometheus/settings.go +++ b/pkg/doh/metrics/prometheus/settings.go @@ -1,7 +1,6 @@ package prometheus import ( - dot "github.com/qdm12/dns/v2/pkg/dot/metrics" dotnoop "github.com/qdm12/dns/v2/pkg/dot/metrics/noop" prom "github.com/qdm12/dns/v2/pkg/metrics/prometheus" "github.com/qdm12/gosettings" @@ -12,7 +11,7 @@ type Settings struct { Prometheus prom.Settings // DoTDialMetrics is the metrics interface for the // DoT dialer. It defaults to a No-op implementation. - DoTDialMetrics dot.DialMetrics + DoTDialMetrics DialMetrics } func (s *Settings) SetDefaults() { diff --git a/pkg/doh/server.go b/pkg/doh/server.go index 4f9b371c..008ca5cc 100644 --- a/pkg/doh/server.go +++ b/pkg/doh/server.go @@ -6,13 +6,12 @@ import ( "sync" "github.com/miekg/dns" - "github.com/qdm12/dns/v2/pkg/log" ) type Server struct { // Dependencies injected settings ServerSettings - logger log.Logger + logger Logger // Internal state running bool diff --git a/pkg/doh/settings.go b/pkg/doh/settings.go index b07144e2..34a46b4f 100644 --- a/pkg/doh/settings.go +++ b/pkg/doh/settings.go @@ -7,13 +7,9 @@ import ( "time" "github.com/qdm12/dns/v2/internal/picker" - "github.com/qdm12/dns/v2/pkg/cache" cachenoop "github.com/qdm12/dns/v2/pkg/cache/noop" - "github.com/qdm12/dns/v2/pkg/doh/metrics" metricsnoop "github.com/qdm12/dns/v2/pkg/doh/metrics/noop" - "github.com/qdm12/dns/v2/pkg/filter" filternoop "github.com/qdm12/dns/v2/pkg/filter/noop" - "github.com/qdm12/dns/v2/pkg/log" lognoop "github.com/qdm12/dns/v2/pkg/log/noop" "github.com/qdm12/dns/v2/pkg/provider" "github.com/qdm12/gosettings" @@ -33,13 +29,13 @@ type ServerSettings struct { // Cache is the cache to use in the server. // It defaults to a No-Op cache implementation with // a No-Op cache metrics implementation. - Cache cache.Interface + Cache Cache // Filter is the filter for DNS requests and responses. // It defaults to a No-Op filter implementation. - Filter filter.Interface + Filter Filter // Logger is the logger to log information. // It defaults to a No-Op logger implementation. - Logger log.Logger + Logger Logger } type ResolverSettings struct { @@ -48,10 +44,10 @@ type ResolverSettings struct { Timeout time.Duration // Warner is the warning logger to log dial errors. // It defaults to a No-Op warner implementation. - Warner log.Warner + Warner Warner // Metrics is the metrics interface to record metric data. // It defaults to a No-Op metrics implementation. - Metrics metrics.DialMetrics + Metrics Metrics // Picker is the picker to use for each upstream call to pick // a server from a pool of servers. It must be thread safe. // It defaults to a fast thread safe pseudo random picker diff --git a/pkg/dot/dial.go b/pkg/dot/dial.go index e80d6c9e..8b9847b1 100644 --- a/pkg/dot/dial.go +++ b/pkg/dot/dial.go @@ -7,8 +7,6 @@ import ( "net" "github.com/qdm12/dns/v2/internal/server" - "github.com/qdm12/dns/v2/pkg/dot/metrics" - "github.com/qdm12/dns/v2/pkg/log" "github.com/qdm12/dns/v2/pkg/provider" ) @@ -85,7 +83,7 @@ func pickNameAddress(picker Picker, servers []provider.DoTServer, func onDialError(ctx context.Context, dialErr error, dotName, dotAddress string, dialer *net.Dialer, picker Picker, ipv6 bool, dnsServers []provider.DNSServer, - warner log.Warner, metrics metrics.DialMetrics) ( + warner Warner, metrics Metrics) ( conn net.Conn, err error) { warner.Warn(dialErr.Error()) metrics.DoTDialInc(dotName, dotAddress, "error") @@ -101,7 +99,7 @@ func onDialError(ctx context.Context, dialErr error, func dialPlaintext(ctx context.Context, dialer *net.Dialer, picker Picker, ipv6 bool, dnsServers []provider.DNSServer, - warner log.Warner, metrics metrics.DialDNSMetrics) ( + warner Warner, metrics Metrics) ( conn net.Conn, err error) { dnsServer := picker.DNSServer(dnsServers) ip := picker.DNSIP(dnsServer, ipv6) diff --git a/pkg/dot/interfaces.go b/pkg/dot/interfaces.go index a9eed392..65e3b664 100644 --- a/pkg/dot/interfaces.go +++ b/pkg/dot/interfaces.go @@ -4,6 +4,7 @@ import ( "net/netip" "github.com/miekg/dns" + "github.com/qdm12/dns/v2/pkg/filter/update" "github.com/qdm12/dns/v2/pkg/provider" ) @@ -11,6 +12,34 @@ type Middleware interface { Wrap(next dns.Handler) dns.Handler } +type Filter interface { + FilterRequest(request *dns.Msg) (blocked bool) + FilterResponse(response *dns.Msg) (blocked bool) + Update(settings update.Settings) +} + +type Cache interface { + Add(request, response *dns.Msg) + Get(request *dns.Msg) (response *dns.Msg) + Remove(request *dns.Msg) +} + +type Metrics interface { + DoTDialInc(provider, address, outcome string) + DNSDialInc(address, outcome string) +} + +type Logger interface { + Debug(s string) + Info(s string) + Warner + Error(s string) +} + +type Warner interface { + Warn(s string) +} + type Picker interface { IP(ips []netip.Addr) netip.Addr DNSServer(servers []provider.DNSServer) provider.DNSServer diff --git a/pkg/dot/metrics/interface.go b/pkg/dot/metrics/interface.go deleted file mode 100644 index 61a21a0b..00000000 --- a/pkg/dot/metrics/interface.go +++ /dev/null @@ -1,22 +0,0 @@ -// Package metrics defines metric interfaces for the -// DoT server and resolver. -package metrics - -import ( - "github.com/qdm12/dns/v2/pkg/dot/metrics/noop" - "github.com/qdm12/dns/v2/pkg/dot/metrics/prometheus" -) - -var ( - _ DialMetrics = (*prometheus.Metrics)(nil) - _ DialMetrics = (*noop.Metrics)(nil) -) - -type DialMetrics interface { - DoTDialInc(provider, address, outcome string) - DialDNSMetrics -} - -type DialDNSMetrics interface { - DNSDialInc(address, outcome string) -} diff --git a/pkg/dot/metrics/noop/metrics.go b/pkg/dot/metrics/noop/metrics.go index a4f7e93c..a237fb40 100644 --- a/pkg/dot/metrics/noop/metrics.go +++ b/pkg/dot/metrics/noop/metrics.go @@ -1,21 +1,11 @@ // Package noop defines a No-Op metric implementation for DoT. package noop -import ( - middleware "github.com/qdm12/dns/v2/pkg/middlewares/metrics" - middlewarenoop "github.com/qdm12/dns/v2/pkg/middlewares/metrics/noop" -) - -type middlewareInterface = middleware.Interface - type Metrics struct { - middlewareInterface } func New() *Metrics { - return &Metrics{ - middlewareInterface: middlewarenoop.New(), - } + return &Metrics{} } func (m *Metrics) DoTDialInc(string, string, string) {} diff --git a/pkg/dot/server.go b/pkg/dot/server.go index 924ca64d..694ea5ee 100644 --- a/pkg/dot/server.go +++ b/pkg/dot/server.go @@ -6,13 +6,12 @@ import ( "sync" "github.com/miekg/dns" - "github.com/qdm12/dns/v2/pkg/log" ) type Server struct { // Dependencies injected settings ServerSettings - logger log.Logger + logger Logger // Internal state running bool diff --git a/pkg/dot/settings.go b/pkg/dot/settings.go index 939bec52..2bd3f727 100644 --- a/pkg/dot/settings.go +++ b/pkg/dot/settings.go @@ -7,13 +7,9 @@ import ( "time" "github.com/qdm12/dns/v2/internal/picker" - "github.com/qdm12/dns/v2/pkg/cache" cachenoop "github.com/qdm12/dns/v2/pkg/cache/noop" - "github.com/qdm12/dns/v2/pkg/dot/metrics" metricsnoop "github.com/qdm12/dns/v2/pkg/dot/metrics/noop" - "github.com/qdm12/dns/v2/pkg/filter" filternoop "github.com/qdm12/dns/v2/pkg/filter/noop" - "github.com/qdm12/dns/v2/pkg/log" lognoop "github.com/qdm12/dns/v2/pkg/log/noop" "github.com/qdm12/dns/v2/pkg/provider" "github.com/qdm12/gosettings" @@ -33,13 +29,13 @@ type ServerSettings struct { // Cache is the cache to use in the server. // It defaults to a No-Op cache implementation with // a No-Op cache metrics implementation. - Cache cache.Interface + Cache Cache // Filter is the filter for DNS requests and responses. // It defaults to a No-Op filter implementation. - Filter filter.Interface + Filter Filter // Logger is the logger to log information. // It defaults to a No-Op logger implementation. - Logger log.Logger + Logger Logger // Metrics is the metrics interface to record metric data. // It defaults to a No-Op metrics implementation. // Metrics metrics.DialMetrics @@ -54,10 +50,10 @@ type ResolverSettings struct { IPv6 bool // Warner is the warning logger to log dial errors. // It defaults to a No-Op warner implementation. - Warner log.Warner + Warner Warner // Metrics is the metrics interface to record metric data. // It defaults to a No-Op metrics implementation. - Metrics metrics.DialMetrics + Metrics Metrics // Picker is the picker to use for each upstream call to pick // a server and/or IP address. It must be thread safe. // It defaults to a fast thread safe pseudo random picker diff --git a/pkg/filter/interface.go b/pkg/filter/interface.go deleted file mode 100644 index 3104f832..00000000 --- a/pkg/filter/interface.go +++ /dev/null @@ -1,19 +0,0 @@ -package filter - -import ( - "github.com/miekg/dns" - "github.com/qdm12/dns/v2/pkg/filter/mapfilter" - "github.com/qdm12/dns/v2/pkg/filter/noop" - "github.com/qdm12/dns/v2/pkg/filter/update" -) - -var ( - _ Interface = (*mapfilter.Filter)(nil) - _ Interface = (*noop.Filter)(nil) -) - -type Interface interface { - FilterRequest(request *dns.Msg) (blocked bool) - FilterResponse(response *dns.Msg) (blocked bool) - Update(settings update.Settings) -} diff --git a/pkg/filter/mapfilter/filter.go b/pkg/filter/mapfilter/filter.go index c86ae40c..36673184 100644 --- a/pkg/filter/mapfilter/filter.go +++ b/pkg/filter/mapfilter/filter.go @@ -3,15 +3,13 @@ package mapfilter import ( "net/netip" "sync" - - "github.com/qdm12/dns/v2/pkg/filter/metrics" ) type Filter struct { fqdnHostnames map[string]struct{} ips map[netip.Addr]struct{} ipPrefixes []netip.Prefix - metrics metrics.Interface + metrics Metrics updateLock sync.RWMutex } diff --git a/pkg/filter/mapfilter/interfaces.go b/pkg/filter/mapfilter/interfaces.go new file mode 100644 index 00000000..964931e7 --- /dev/null +++ b/pkg/filter/mapfilter/interfaces.go @@ -0,0 +1,9 @@ +package mapfilter + +type Metrics interface { + SetBlockedHostnames(n int) + SetBlockedIPs(n int) + SetBlockedIPPrefixes(n int) + HostnamesFilteredInc(qClass, qType string) + IPsFilteredInc(rrtype string) +} diff --git a/pkg/filter/mapfilter/settings.go b/pkg/filter/mapfilter/settings.go index 9c420cbf..4077941e 100644 --- a/pkg/filter/mapfilter/settings.go +++ b/pkg/filter/mapfilter/settings.go @@ -5,7 +5,6 @@ import ( "reflect" "strings" - "github.com/qdm12/dns/v2/pkg/filter/metrics" "github.com/qdm12/dns/v2/pkg/filter/metrics/noop" "github.com/qdm12/dns/v2/pkg/filter/metrics/prometheus" "github.com/qdm12/dns/v2/pkg/filter/update" @@ -18,7 +17,7 @@ type Settings struct { Update update.Settings // Metrics is the metric interface and defaults // to a no-op implementation if left unset. - Metrics metrics.Interface + Metrics Metrics } func (s *Settings) SetDefaults() { diff --git a/pkg/filter/metrics/interface.go b/pkg/filter/metrics/interface.go deleted file mode 100644 index e842f5a6..00000000 --- a/pkg/filter/metrics/interface.go +++ /dev/null @@ -1,20 +0,0 @@ -// Package metrics defines a metrics interface for the filter. -package metrics - -import ( - "github.com/qdm12/dns/v2/pkg/filter/metrics/noop" - "github.com/qdm12/dns/v2/pkg/filter/metrics/prometheus" -) - -var ( - _ Interface = (*prometheus.Metrics)(nil) - _ Interface = (*noop.Metrics)(nil) -) - -type Interface interface { - SetBlockedHostnames(n int) - SetBlockedIPs(n int) - SetBlockedIPPrefixes(n int) - HostnamesFilteredInc(qClass, qType string) - IPsFilteredInc(rrtype string) -} diff --git a/pkg/log/interface.go b/pkg/log/interface.go deleted file mode 100644 index a5d23b49..00000000 --- a/pkg/log/interface.go +++ /dev/null @@ -1,19 +0,0 @@ -package log - -import "github.com/qdm12/dns/v2/pkg/log/noop" - -var ( - _ Logger = (*noop.Logger)(nil) - _ Warner = (*noop.Logger)(nil) -) - -type Logger interface { - Debug(s string) - Info(s string) - Warner - Error(s string) -} - -type Warner interface { - Warn(s string) -} diff --git a/pkg/middlewares/metrics/interface.go b/pkg/middlewares/metrics/interface.go deleted file mode 100644 index b7436b0f..00000000 --- a/pkg/middlewares/metrics/interface.go +++ /dev/null @@ -1,21 +0,0 @@ -package metrics - -import ( - "github.com/qdm12/dns/v2/pkg/middlewares/metrics/noop" - "github.com/qdm12/dns/v2/pkg/middlewares/metrics/prometheus" -) - -var ( - _ Interface = (*prometheus.Metrics)(nil) - _ Interface = (*noop.Metrics)(nil) -) - -type Interface interface { - RequestsInc() - QuestionsInc(class, qType string) - RcodeInc(rcode string) - AnswersInc(class, qType string) - ResponsesInc() - InFlightRequestsInc() - InFlightRequestsDec() -} diff --git a/pkg/middlewares/metrics/interfaces.go b/pkg/middlewares/metrics/interfaces.go new file mode 100644 index 00000000..3a526d7b --- /dev/null +++ b/pkg/middlewares/metrics/interfaces.go @@ -0,0 +1,11 @@ +package metrics + +type Metrics interface { + RequestsInc() + QuestionsInc(class, qType string) + RcodeInc(rcode string) + AnswersInc(class, qType string) + ResponsesInc() + InFlightRequestsInc() + InFlightRequestsDec() +} diff --git a/pkg/middlewares/metrics/middleware.go b/pkg/middlewares/metrics/middleware.go index 2565bb41..7fd1eef1 100644 --- a/pkg/middlewares/metrics/middleware.go +++ b/pkg/middlewares/metrics/middleware.go @@ -10,7 +10,7 @@ import ( ) type Middleware struct { - metrics Interface + metrics Metrics } func New(settings Settings) *Middleware { @@ -30,7 +30,7 @@ func (m *Middleware) Wrap(next dns.Handler) dns.Handler { //nolint:ireturn type handler struct { next dns.Handler - metrics Interface + metrics Metrics } func (h *handler) ServeDNS(w dns.ResponseWriter, response *dns.Msg) { diff --git a/pkg/middlewares/metrics/settings.go b/pkg/middlewares/metrics/settings.go index d15eab41..659d92ac 100644 --- a/pkg/middlewares/metrics/settings.go +++ b/pkg/middlewares/metrics/settings.go @@ -8,7 +8,7 @@ import ( type Settings struct { // Metrics is the interface to interact with metrics in the // DNS middleware. It defaults to a No-Op implementation. - Metrics Interface + Metrics Metrics } func (s *Settings) SetDefaults() {