From a7e2b29fa27b446e3c073cdf858db92c44daa2df Mon Sep 17 00:00:00 2001 From: Alex Schittko Date: Sat, 13 Jul 2024 01:37:51 -0600 Subject: [PATCH 1/3] Optionally disable tunneldns http response caching to fix #745 --- tunneldns/tunnel.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tunneldns/tunnel.go b/tunneldns/tunnel.go index 996c84c4773..3d7dd4b0e17 100644 --- a/tunneldns/tunnel.go +++ b/tunneldns/tunnel.go @@ -4,6 +4,7 @@ import ( "net" "strconv" "sync" + "os" "github.com/coredns/coredns/core/dnsserver" "github.com/coredns/coredns/plugin" @@ -90,12 +91,19 @@ func CreateListener(address string, port uint16, upstreams []string, bootstraps upstreamList = append(upstreamList, upstream) } + + // Create a local cache with HTTPS proxy plugin chain := cache.New() chain.Next = ProxyPlugin{ Upstreams: upstreamList, - } + } + // Optionally disable http response caching + if os.Getenv("DISABLE_TUNNELDNS_CACHE") == "true" { + chain.Next = ProxyPlugin{} + } + // Format an endpoint endpoint := "dns://" + net.JoinHostPort(address, strconv.FormatUint(uint64(port), 10)) From 46fc5d3601d0043ec55327aeb831d1ef24edbbbc Mon Sep 17 00:00:00 2001 From: Alex Schittko Date: Sat, 13 Jul 2024 07:46:12 +0000 Subject: [PATCH 2/3] Refactor --- tunneldns/tunnel.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tunneldns/tunnel.go b/tunneldns/tunnel.go index 3d7dd4b0e17..05f72ee862b 100644 --- a/tunneldns/tunnel.go +++ b/tunneldns/tunnel.go @@ -94,14 +94,15 @@ func CreateListener(address string, port uint16, upstreams []string, bootstraps // Create a local cache with HTTPS proxy plugin - chain := cache.New() - chain.Next = ProxyPlugin{ + proxyPlugin := ProxyPlugin{ Upstreams: upstreamList, - } + } + chain := cache.New() + chain.Next = proxyPlugin // Optionally disable http response caching if os.Getenv("DISABLE_TUNNELDNS_CACHE") == "true" { - chain.Next = ProxyPlugin{} + chain = proxyPlugin } // Format an endpoint From 650c88db1ba210ec03bb386f83aeaa25dd550468 Mon Sep 17 00:00:00 2001 From: Alex Schittko Date: Sat, 13 Jul 2024 08:00:00 +0000 Subject: [PATCH 3/3] Refactor --- tunneldns/tunnel.go | 39 ++++++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/tunneldns/tunnel.go b/tunneldns/tunnel.go index 05f72ee862b..e991f9e3607 100644 --- a/tunneldns/tunnel.go +++ b/tunneldns/tunnel.go @@ -91,28 +91,37 @@ func CreateListener(address string, port uint16, upstreams []string, bootstraps upstreamList = append(upstreamList, upstream) } - - - // Create a local cache with HTTPS proxy plugin - proxyPlugin := ProxyPlugin{ - Upstreams: upstreamList, - } - chain := cache.New() - chain.Next = proxyPlugin - - // Optionally disable http response caching - if os.Getenv("DISABLE_TUNNELDNS_CACHE") == "true" { - chain = proxyPlugin - } - // Format an endpoint endpoint := "dns://" + net.JoinHostPort(address, strconv.FormatUint(uint64(port), 10)) + // get middlewares for DNS Server + middlewares := getMiddlewares(upstreamList) + chain := NewMetricsPlugin(middlewares) + // Create the actual middleware server - server, err := dnsserver.NewServer(endpoint, []*dnsserver.Config{createConfig(address, port, NewMetricsPlugin(chain))}) + server, err := dnsserver.NewServer(endpoint, []*dnsserver.Config{createConfig(address, port, chain)}) if err != nil { return nil, err } + return &Listener{server: server, log: log}, nil } + +// getMiddlewares() returns the middleware chain for DNS Server +// +// Middleware includes features like... +// * Response Cache +// * HTTP Proxy Settings +func getMiddlewares(upstreamList []Upstream) plugin.Handler { + proxyPlugin := ProxyPlugin{ + Upstreams: upstreamList, + } + cachePlugin := cache.New() + cachePlugin.Next = proxyPlugin + + if os.Getenv("DISABLE_TUNNELDNS_CACHE") == "true" { + return cachePlugin + } + return proxyPlugin +} \ No newline at end of file