| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | package dnstap
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2022-05-13 02:13:26 +08:00
										 |  |  | 	"net/url"
 | 
					
						
							| 
									
										
										
										
											2022-09-07 09:22:38 -04:00
										 |  |  | 	"os"
 | 
					
						
							| 
									
										
										
										
											2017-09-01 14:07:21 +02:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy"
 | 
					
						
							| 
									
										
										
										
											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"
 | 
					
						
							| 
									
										
										
										
											2020-11-05 14:37:16 +01:00
										 |  |  | 	clog "github.com/coredns/coredns/plugin/pkg/log"
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 14:37:16 +01:00
										 |  |  | var log = clog.NewWithPlugin("dnstap")
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-05 14:37:16 +01:00
										 |  |  | func init() { plugin.Register("dnstap", setup) }
 | 
					
						
							| 
									
										
										
										
											2017-09-01 14:07:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | func parseConfig(c *caddy.Controller) ([]*Dnstap, error) {
 | 
					
						
							|  |  |  | 	dnstaps := []*Dnstap{}
 | 
					
						
							| 
									
										
										
										
											2017-09-01 14:07:21 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 	for c.Next() { // directive name
 | 
					
						
							|  |  |  | 		d := Dnstap{}
 | 
					
						
							|  |  |  | 		endpoint := ""
 | 
					
						
							| 
									
										
										
										
											2022-09-07 09:22:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 		args := c.RemainingArgs()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if len(args) == 0 {
 | 
					
						
							|  |  |  | 			return nil, c.ArgErr()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 		endpoint = args[0]
 | 
					
						
							| 
									
										
										
										
											2022-09-07 09:22:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-21 00:34:48 +01:00
										 |  |  | 		var dio *dio
 | 
					
						
							|  |  |  | 		if strings.HasPrefix(endpoint, "tls://") {
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 			// remote network endpoint
 | 
					
						
							|  |  |  | 			endpointURL, err := url.Parse(endpoint)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				return nil, c.ArgErr()
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2023-02-21 00:34:48 +01:00
										 |  |  | 			dio = newIO("tls", endpointURL.Host)
 | 
					
						
							|  |  |  | 			d = Dnstap{io: dio}
 | 
					
						
							|  |  |  | 		} else if strings.HasPrefix(endpoint, "tcp://") {
 | 
					
						
							|  |  |  | 			// remote network endpoint
 | 
					
						
							|  |  |  | 			endpointURL, err := url.Parse(endpoint)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				return nil, c.ArgErr()
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			dio = newIO("tcp", endpointURL.Host)
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 			d = Dnstap{io: dio}
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			endpoint = strings.TrimPrefix(endpoint, "unix://")
 | 
					
						
							| 
									
										
										
										
											2023-02-21 00:34:48 +01:00
										 |  |  | 			dio = newIO("unix", endpoint)
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 			d = Dnstap{io: dio}
 | 
					
						
							| 
									
										
										
										
											2017-09-01 14:07:21 +02:00
										 |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 		d.IncludeRawMessage = len(args) == 2 && args[1] == "full"
 | 
					
						
							| 
									
										
										
										
											2022-09-07 09:22:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 		hostname, _ := os.Hostname()
 | 
					
						
							|  |  |  | 		d.Identity = []byte(hostname)
 | 
					
						
							|  |  |  | 		d.Version = []byte(caddy.AppName + "-" + caddy.AppVersion)
 | 
					
						
							| 
									
										
										
										
											2022-09-07 09:22:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 		for c.NextBlock() {
 | 
					
						
							|  |  |  | 			switch c.Val() {
 | 
					
						
							| 
									
										
										
										
											2023-02-21 00:34:48 +01:00
										 |  |  | 			case "skipverify":
 | 
					
						
							|  |  |  | 				{
 | 
					
						
							|  |  |  | 					dio.skipVerify = true
 | 
					
						
							|  |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 			case "identity":
 | 
					
						
							|  |  |  | 				{
 | 
					
						
							|  |  |  | 					if !c.NextArg() {
 | 
					
						
							|  |  |  | 						return nil, c.ArgErr()
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							|  |  |  | 					d.Identity = []byte(c.Val())
 | 
					
						
							| 
									
										
										
										
											2022-09-07 09:22:38 -04:00
										 |  |  | 				}
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 			case "version":
 | 
					
						
							|  |  |  | 				{
 | 
					
						
							|  |  |  | 					if !c.NextArg() {
 | 
					
						
							|  |  |  | 						return nil, c.ArgErr()
 | 
					
						
							|  |  |  | 					}
 | 
					
						
							|  |  |  | 					d.Version = []byte(c.Val())
 | 
					
						
							| 
									
										
										
										
											2022-09-07 09:22:38 -04:00
										 |  |  | 				}
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 		dnstaps = append(dnstaps, &d)
 | 
					
						
							| 
									
										
										
										
											2022-09-07 09:22:38 -04:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 	return dnstaps, nil
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func setup(c *caddy.Controller) error {
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 	dnstaps, err := parseConfig(c)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2020-10-12 19:10:35 +02:00
										 |  |  | 		return plugin.Error("dnstap", err)
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 	for i := range dnstaps {
 | 
					
						
							|  |  |  | 		dnstap := dnstaps[i]
 | 
					
						
							|  |  |  | 		c.OnStartup(func() error {
 | 
					
						
							|  |  |  | 			if err := dnstap.io.(*dio).connect(); err != nil {
 | 
					
						
							|  |  |  | 				log.Errorf("No connection to dnstap endpoint: %s", err)
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 			return nil
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		c.OnRestart(func() error {
 | 
					
						
							|  |  |  | 			dnstap.io.(*dio).close()
 | 
					
						
							|  |  |  | 			return nil
 | 
					
						
							|  |  |  | 		})
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		c.OnFinalShutdown(func() error {
 | 
					
						
							|  |  |  | 			dnstap.io.(*dio).close()
 | 
					
						
							|  |  |  | 			return nil
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 		})
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-28 10:33:31 -05:00
										 |  |  | 		if i == len(dnstaps)-1 {
 | 
					
						
							|  |  |  | 			// last dnstap plugin in block: point next to next plugin
 | 
					
						
							|  |  |  | 			dnsserver.GetConfig(c).AddPlugin(func(next plugin.Handler) plugin.Handler {
 | 
					
						
							|  |  |  | 				dnstap.Next = next
 | 
					
						
							|  |  |  | 				return dnstap
 | 
					
						
							|  |  |  | 			})
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			// not last dnstap plugin in block: point next to next dnstap
 | 
					
						
							|  |  |  | 			nextDnstap := dnstaps[i+1]
 | 
					
						
							|  |  |  | 			dnsserver.GetConfig(c).AddPlugin(func(plugin.Handler) plugin.Handler {
 | 
					
						
							|  |  |  | 				dnstap.Next = nextDnstap
 | 
					
						
							|  |  |  | 				return dnstap
 | 
					
						
							|  |  |  | 			})
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-07-24 23:12:50 +02:00
										 |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 |