| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | package dnsserver
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 11:26:00 +01:00
										 |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	"crypto/tls"
 | 
					
						
							| 
									
										
										
										
											2018-02-23 11:54:42 -05:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin"
 | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-19 11:26:00 +01:00
										 |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							|  |  |  | )
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Config configuration for a single server.
 | 
					
						
							|  |  |  | type Config struct {
 | 
					
						
							|  |  |  | 	// The zone of the site.
 | 
					
						
							|  |  |  | 	Zone string
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-14 14:19:32 -05:00
										 |  |  | 	// one or several hostnames to bind the server to.
 | 
					
						
							|  |  |  | 	// defaults to a single empty string that denote the wildcard address
 | 
					
						
							|  |  |  | 	ListenHosts []string
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// The port to listen on.
 | 
					
						
							|  |  |  | 	Port string
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-11 20:42:28 +01:00
										 |  |  | 	// Root points to a base directory we we find user defined "things".
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	// First consumer is the file plugin to looks for zone files in this place.
 | 
					
						
							| 
									
										
										
										
											2016-10-11 20:42:28 +01:00
										 |  |  | 	Root string
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-13 15:47:17 -07:00
										 |  |  | 	// Debug controls the panic/recover mechanism that is enabled by default.
 | 
					
						
							|  |  |  | 	Debug bool
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	// The transport we implement, normally just "dns" over TCP/UDP, but could be
 | 
					
						
							|  |  |  | 	// DNS-over-TLS or DNS-over-gRPC.
 | 
					
						
							|  |  |  | 	Transport string
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-24 10:16:03 +01:00
										 |  |  | 	// If this function is not nil it will be used to further filter access
 | 
					
						
							|  |  |  | 	// to this handler. The primary use is to limit access to a reverse zone
 | 
					
						
							|  |  |  | 	// on a non-octet boundary, i.e. /17
 | 
					
						
							|  |  |  | 	FilterFunc func(string) bool
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	// TLSConfig when listening for encrypted connections (gRPC, DNS-over-TLS).
 | 
					
						
							|  |  |  | 	TLSConfig *tls.Config
 | 
					
						
							| 
									
										
										
										
											2017-01-31 17:21:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	// Plugin stack.
 | 
					
						
							|  |  |  | 	Plugin []plugin.Plugin
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	// Compiled plugin stack.
 | 
					
						
							|  |  |  | 	pluginChain plugin.Handler
 | 
					
						
							| 
									
										
										
										
											2017-08-10 21:31:36 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	// Plugin interested in announcing that they exist, so other plugin can call methods
 | 
					
						
							| 
									
										
										
										
											2017-08-10 21:31:36 +01:00
										 |  |  | 	// on them should register themselves here. The name should be the name as return by the
 | 
					
						
							|  |  |  | 	// Handler's Name method.
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	registry map[string]plugin.Handler
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-02-23 11:54:42 -05:00
										 |  |  | // keyForConfig build a key for identifying the configs during setup time
 | 
					
						
							|  |  |  | func keyForConfig(blocIndex int, blocKeyIndex int) string {
 | 
					
						
							|  |  |  | 	return fmt.Sprintf("%d:%d", blocIndex, blocKeyIndex)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | // GetConfig gets the Config that corresponds to c.
 | 
					
						
							|  |  |  | // If none exist nil is returned.
 | 
					
						
							|  |  |  | func GetConfig(c *caddy.Controller) *Config {
 | 
					
						
							|  |  |  | 	ctx := c.Context().(*dnsContext)
 | 
					
						
							| 
									
										
										
										
											2018-02-23 11:54:42 -05:00
										 |  |  | 	key := keyForConfig(c.ServerBlockIndex, c.ServerBlockKeyIndex)
 | 
					
						
							|  |  |  | 	if cfg, ok := ctx.keysToConfigs[key]; ok {
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 		return cfg
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	// we should only get here during tests because directive
 | 
					
						
							|  |  |  | 	// actions typically skip the server blocks where we make
 | 
					
						
							|  |  |  | 	// the configs.
 | 
					
						
							| 
									
										
										
										
											2018-02-23 11:54:42 -05:00
										 |  |  | 	ctx.saveConfig(key, &Config{ListenHosts: []string{""}})
 | 
					
						
							| 
									
										
										
										
											2016-08-19 17:14:17 -07:00
										 |  |  | 	return GetConfig(c)
 | 
					
						
							|  |  |  | }
 |