| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | package dnstap
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-09-01 14:07:21 +02:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2017-09-26 17:45:33 +02:00
										 |  |  | 	"github.com/coredns/coredns/plugin/dnstap/dnstapio"
 | 
					
						
							| 
									
										
										
										
											2018-09-19 08:16:04 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/parse"
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy"
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-20 08:02:30 +01:00
										 |  |  | func init() { plugin.Register("dnstap", wrapSetup) }
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | func wrapSetup(c *caddy.Controller) error {
 | 
					
						
							|  |  |  | 	if err := setup(c); err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 		return plugin.Error("dnstap", err)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 14:07:21 +02:00
										 |  |  | type config struct {
 | 
					
						
							|  |  |  | 	target string
 | 
					
						
							|  |  |  | 	socket bool
 | 
					
						
							|  |  |  | 	full   bool
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-28 10:41:12 +01:00
										 |  |  | func parseConfig(d *caddy.Controller) (c config, err error) {
 | 
					
						
							| 
									
										
										
										
											2017-09-01 14:07:21 +02:00
										 |  |  | 	d.Next() // directive name
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !d.Args(&c.target) {
 | 
					
						
							|  |  |  | 		return c, d.ArgErr()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 14:07:21 +02:00
										 |  |  | 	if strings.HasPrefix(c.target, "tcp://") {
 | 
					
						
							|  |  |  | 		// remote IP endpoint
 | 
					
						
							| 
									
										
										
										
											2018-09-19 08:16:04 +01:00
										 |  |  | 		servers, err := parse.HostPortOrFile(c.target[6:])
 | 
					
						
							| 
									
										
										
										
											2017-09-01 14:07:21 +02:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			return c, d.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		c.target = servers[0]
 | 
					
						
							|  |  |  | 	} else {
 | 
					
						
							|  |  |  | 		// default to UNIX socket
 | 
					
						
							| 
									
										
										
										
											2019-09-25 20:23:43 +08:00
										 |  |  | 		c.target = strings.TrimPrefix(c.target, "unix://")
 | 
					
						
							| 
									
										
										
										
											2017-09-01 14:07:21 +02:00
										 |  |  | 		c.socket = true
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-01 14:07:21 +02:00
										 |  |  | 	c.full = d.NextArg() && d.Val() == "full"
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2019-09-28 10:41:12 +01:00
										 |  |  | 	conf, err := parseConfig(c)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-01 13:16:14 +02:00
										 |  |  | 	dio := dnstapio.New(conf.target, conf.socket)
 | 
					
						
							| 
									
										
										
										
											2018-03-01 03:19:01 +01:00
										 |  |  | 	dnstap := Dnstap{IO: dio, JoinRawMessage: conf.full}
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 00:36:14 +03:00
										 |  |  | 	c.OnStartup(func() error {
 | 
					
						
							| 
									
										
										
										
											2017-12-01 13:16:14 +02:00
										 |  |  | 		dio.Connect()
 | 
					
						
							| 
									
										
										
										
											2017-11-28 00:36:14 +03:00
										 |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-11-28 00:36:14 +03:00
										 |  |  | 	c.OnRestart(func() error {
 | 
					
						
							|  |  |  | 		dio.Close()
 | 
					
						
							|  |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	c.OnFinalShutdown(func() error {
 | 
					
						
							|  |  |  | 		dio.Close()
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 		return nil
 | 
					
						
							|  |  |  | 	})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	dnsserver.GetConfig(c).AddPlugin(
 | 
					
						
							|  |  |  | 		func(next plugin.Handler) plugin.Handler {
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 			dnstap.Next = next
 | 
					
						
							|  |  |  | 			return dnstap
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 |