| 
									
										
										
										
											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" | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnsutil" | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy" | 
					
						
							|  |  |  | 	"github.com/mholt/caddy/caddyfile" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func init() { | 
					
						
							|  |  |  | 	caddy.RegisterPlugin("dnstap", caddy.Plugin{ | 
					
						
							|  |  |  | 		ServerType: "dns", | 
					
						
							|  |  |  | 		Action:     wrapSetup, | 
					
						
							|  |  |  | 	}) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func parseConfig(d *caddyfile.Dispenser) (c config, err error) { | 
					
						
							|  |  |  | 	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 | 
					
						
							|  |  |  | 		servers, err := dnsutil.ParseHostPortOrFile(c.target[6:]) | 
					
						
							|  |  |  | 		if err != nil { | 
					
						
							|  |  |  | 			return c, d.ArgErr() | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		c.target = servers[0] | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		// default to UNIX socket | 
					
						
							|  |  |  | 		if strings.HasPrefix(c.target, "unix://") { | 
					
						
							|  |  |  | 			c.target = c.target[7:] | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		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 { | 
					
						
							| 
									
										
										
										
											2017-09-01 14:07:21 +02:00
										 |  |  | 	conf, err := parseConfig(&c.Dispenser) | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							| 
									
										
										
										
											2017-11-28 00:36:14 +03:00
										 |  |  | 	dnstap := Dnstap{IO: dio, Pack: 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 | 
					
						
							|  |  |  | } |