* plugin/dnstap: various cleanups A recent issue made me look into this plugin, I suspect various other cleanups (hopefully deletion of code) can be made as well Remove identical functions ToClientQuery etc, and just use tap.Message as the base type in plugin. Keep msg/ for a few helper functions that may proof useful. This remove the whole test directory as we will just check the things we are interested in which gives much better feedback and keeps that code closer together. tapwr dir is also not needed, writer_test.go was just duplicating the tests already done. This moves writer.go to the top directory. Make the only user of dnstap, the forward plugin, use the newer code also remove the test, a better test there would be a full e2e test to see the correct thing happens. Cleanup the Tapper interface and move it to dnstapio where it belongs, remove higher level interfaces that are not used. This remove dnstap.Tapper and dnstap.IORoutines. Use the standard mechanism for getting access to a plugin and remove shuffling the plugin into the context. Signed-off-by: Miek Gieben <miek@miek.nl> * use opts to get the correct proto Signed-off-by: Miek Gieben <miek@miek.nl> * Various fixes Signed-off-by: Miek Gieben <miek@miek.nl> * Remove bad addr test, as dnstap is only called from within coredns where these fields have been preparsed Signed-off-by: Miek Gieben <miek@miek.nl> * dnstap: remove saving the error all these fields have been preparsed, no need for dnstap to be pedantic and check (and save!) this error again. Simplifies it a bit more. Signed-off-by: Miek Gieben <miek@miek.nl> * Update plugin/forward/dnstap.go Co-authored-by: Ruslan Drozhdzh <30860269+rdrozhdzh@users.noreply.github.com> * Code review Signed-off-by: Miek Gieben <miek@miek.nl> * add back in preferUDP Signed-off-by: Miek Gieben <miek@miek.nl> * nit Signed-off-by: Miek Gieben <miek@miek.nl> Co-authored-by: Ruslan Drozhdzh <30860269+rdrozhdzh@users.noreply.github.com>
264 lines
5.4 KiB
Go
264 lines
5.4 KiB
Go
package forward
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"errors"
|
|
"fmt"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/coredns/caddy"
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
"github.com/coredns/coredns/plugin"
|
|
"github.com/coredns/coredns/plugin/dnstap"
|
|
"github.com/coredns/coredns/plugin/pkg/parse"
|
|
pkgtls "github.com/coredns/coredns/plugin/pkg/tls"
|
|
"github.com/coredns/coredns/plugin/pkg/transport"
|
|
)
|
|
|
|
func init() { plugin.Register("forward", setup) }
|
|
|
|
func setup(c *caddy.Controller) error {
|
|
f, err := parseForward(c)
|
|
if err != nil {
|
|
return plugin.Error("forward", err)
|
|
}
|
|
if f.Len() > max {
|
|
return plugin.Error("forward", fmt.Errorf("more than %d TOs configured: %d", max, f.Len()))
|
|
}
|
|
|
|
dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
|
|
f.Next = next
|
|
return f
|
|
})
|
|
|
|
c.OnStartup(func() error {
|
|
return f.OnStartup()
|
|
})
|
|
c.OnStartup(func() error {
|
|
if taph := dnsserver.GetConfig(c).Handler("dnstap"); taph != nil {
|
|
if tapPlugin, ok := taph.(dnstap.Dnstap); ok {
|
|
f.tapPlugin = &tapPlugin
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
|
|
c.OnShutdown(func() error {
|
|
return f.OnShutdown()
|
|
})
|
|
|
|
return nil
|
|
}
|
|
|
|
// OnStartup starts a goroutines for all proxies.
|
|
func (f *Forward) OnStartup() (err error) {
|
|
for _, p := range f.proxies {
|
|
p.start(f.hcInterval)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// OnShutdown stops all configured proxies.
|
|
func (f *Forward) OnShutdown() error {
|
|
for _, p := range f.proxies {
|
|
p.stop()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func parseForward(c *caddy.Controller) (*Forward, error) {
|
|
var (
|
|
f *Forward
|
|
err error
|
|
i int
|
|
)
|
|
for c.Next() {
|
|
if i > 0 {
|
|
return nil, plugin.ErrOnce
|
|
}
|
|
i++
|
|
f, err = parseStanza(c)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
return f, nil
|
|
}
|
|
|
|
func parseStanza(c *caddy.Controller) (*Forward, error) {
|
|
f := New()
|
|
|
|
if !c.Args(&f.from) {
|
|
return f, c.ArgErr()
|
|
}
|
|
f.from = plugin.Host(f.from).Normalize()
|
|
|
|
to := c.RemainingArgs()
|
|
if len(to) == 0 {
|
|
return f, c.ArgErr()
|
|
}
|
|
|
|
toHosts, err := parse.HostPortOrFile(to...)
|
|
if err != nil {
|
|
return f, err
|
|
}
|
|
|
|
transports := make([]string, len(toHosts))
|
|
allowedTrans := map[string]bool{"dns": true, "tls": true}
|
|
for i, host := range toHosts {
|
|
trans, h := parse.Transport(host)
|
|
|
|
if !allowedTrans[trans] {
|
|
return f, fmt.Errorf("'%s' is not supported as a destination protocol in forward: %s", trans, host)
|
|
}
|
|
p := NewProxy(h, trans)
|
|
f.proxies = append(f.proxies, p)
|
|
transports[i] = trans
|
|
}
|
|
|
|
for c.NextBlock() {
|
|
if err := parseBlock(c, f); err != nil {
|
|
return f, err
|
|
}
|
|
}
|
|
|
|
if f.tlsServerName != "" {
|
|
f.tlsConfig.ServerName = f.tlsServerName
|
|
}
|
|
|
|
// Initialize ClientSessionCache in tls.Config. This may speed up a TLS handshake
|
|
// in upcoming connections to the same TLS server.
|
|
f.tlsConfig.ClientSessionCache = tls.NewLRUClientSessionCache(len(f.proxies))
|
|
|
|
for i := range f.proxies {
|
|
// Only set this for proxies that need it.
|
|
if transports[i] == transport.TLS {
|
|
f.proxies[i].SetTLSConfig(f.tlsConfig)
|
|
}
|
|
f.proxies[i].SetExpire(f.expire)
|
|
f.proxies[i].health.SetRecursionDesired(f.opts.hcRecursionDesired)
|
|
}
|
|
|
|
return f, nil
|
|
}
|
|
|
|
func parseBlock(c *caddy.Controller, f *Forward) error {
|
|
switch c.Val() {
|
|
case "except":
|
|
ignore := c.RemainingArgs()
|
|
if len(ignore) == 0 {
|
|
return c.ArgErr()
|
|
}
|
|
for i := 0; i < len(ignore); i++ {
|
|
ignore[i] = plugin.Host(ignore[i]).Normalize()
|
|
}
|
|
f.ignored = ignore
|
|
case "max_fails":
|
|
if !c.NextArg() {
|
|
return c.ArgErr()
|
|
}
|
|
n, err := strconv.Atoi(c.Val())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n < 0 {
|
|
return fmt.Errorf("max_fails can't be negative: %d", n)
|
|
}
|
|
f.maxfails = uint32(n)
|
|
case "health_check":
|
|
if !c.NextArg() {
|
|
return c.ArgErr()
|
|
}
|
|
dur, err := time.ParseDuration(c.Val())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if dur < 0 {
|
|
return fmt.Errorf("health_check can't be negative: %d", dur)
|
|
}
|
|
f.hcInterval = dur
|
|
|
|
for c.NextArg() {
|
|
switch hcOpts := c.Val(); hcOpts {
|
|
case "no_rec":
|
|
f.opts.hcRecursionDesired = false
|
|
default:
|
|
return fmt.Errorf("health_check: unknown option %s", hcOpts)
|
|
}
|
|
}
|
|
|
|
case "force_tcp":
|
|
if c.NextArg() {
|
|
return c.ArgErr()
|
|
}
|
|
f.opts.forceTCP = true
|
|
case "prefer_udp":
|
|
if c.NextArg() {
|
|
return c.ArgErr()
|
|
}
|
|
f.opts.preferUDP = true
|
|
case "tls":
|
|
args := c.RemainingArgs()
|
|
if len(args) > 3 {
|
|
return c.ArgErr()
|
|
}
|
|
|
|
tlsConfig, err := pkgtls.NewTLSConfigFromArgs(args...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
f.tlsConfig = tlsConfig
|
|
case "tls_servername":
|
|
if !c.NextArg() {
|
|
return c.ArgErr()
|
|
}
|
|
f.tlsServerName = c.Val()
|
|
case "expire":
|
|
if !c.NextArg() {
|
|
return c.ArgErr()
|
|
}
|
|
dur, err := time.ParseDuration(c.Val())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if dur < 0 {
|
|
return fmt.Errorf("expire can't be negative: %s", dur)
|
|
}
|
|
f.expire = dur
|
|
case "policy":
|
|
if !c.NextArg() {
|
|
return c.ArgErr()
|
|
}
|
|
switch x := c.Val(); x {
|
|
case "random":
|
|
f.p = &random{}
|
|
case "round_robin":
|
|
f.p = &roundRobin{}
|
|
case "sequential":
|
|
f.p = &sequential{}
|
|
default:
|
|
return c.Errf("unknown policy '%s'", x)
|
|
}
|
|
case "max_concurrent":
|
|
if !c.NextArg() {
|
|
return c.ArgErr()
|
|
}
|
|
n, err := strconv.Atoi(c.Val())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if n < 0 {
|
|
return fmt.Errorf("max_concurrent can't be negative: %d", n)
|
|
}
|
|
f.ErrLimitExceeded = errors.New("concurrent queries exceeded maximum " + c.Val())
|
|
f.maxConcurrent = int64(n)
|
|
|
|
default:
|
|
return c.Errf("unknown property '%s'", c.Val())
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
const max = 15 // Maximum number of upstreams.
|