| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | package dnsserver
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							| 
									
										
										
										
											2018-04-20 11:01:06 +01:00
										 |  |  | 	"context"
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	"crypto/tls"
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"net"
 | 
					
						
							| 
									
										
										
										
											2022-12-28 11:14:16 +00:00
										 |  |  | 	"time"
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy"
 | 
					
						
							| 
									
										
										
										
											2019-11-17 02:02:46 +00:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/reuseport"
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/transport"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	"github.com/miekg/dns"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 09:46:40 -07:00
										 |  |  | // ServerTLS represents an instance of a TLS-over-DNS-server.
 | 
					
						
							|  |  |  | type ServerTLS struct {
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	*Server
 | 
					
						
							| 
									
										
										
										
											2018-01-14 03:09:14 -05:00
										 |  |  | 	tlsConfig *tls.Config
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // NewServerTLS returns a new CoreDNS TLS server and compiles all plugin in to it.
 | 
					
						
							| 
									
										
										
										
											2017-08-04 09:46:40 -07:00
										 |  |  | func NewServerTLS(addr string, group []*Config) (*ServerTLS, error) {
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	s, err := NewServer(addr, group)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2018-01-14 03:09:14 -05:00
										 |  |  | 	// The *tls* plugin must make sure that multiple conflicting
 | 
					
						
							| 
									
										
										
										
											2020-09-01 15:10:45 +08:00
										 |  |  | 	// TLS configuration returns an error: it can only be specified once.
 | 
					
						
							| 
									
										
										
										
											2018-01-14 03:09:14 -05:00
										 |  |  | 	var tlsConfig *tls.Config
 | 
					
						
							| 
									
										
										
										
											2022-09-08 14:56:27 -04:00
										 |  |  | 	for _, z := range s.zones {
 | 
					
						
							|  |  |  | 		for _, conf := range z {
 | 
					
						
							|  |  |  | 			// Should we error if some configs *don't* have TLS?
 | 
					
						
							|  |  |  | 			tlsConfig = conf.TLSConfig
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2018-01-14 03:09:14 -05:00
										 |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-14 03:09:14 -05:00
										 |  |  | 	return &ServerTLS{Server: s, tlsConfig: tlsConfig}, nil
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-06 16:25:07 +08:00
										 |  |  | // Compile-time check to ensure Server implements the caddy.GracefulServer interface
 | 
					
						
							|  |  |  | var _ caddy.GracefulServer = &Server{}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | // Serve implements caddy.TCPServer interface.
 | 
					
						
							| 
									
										
										
										
											2017-08-04 09:46:40 -07:00
										 |  |  | func (s *ServerTLS) Serve(l net.Listener) error {
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	s.m.Lock()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-14 03:09:14 -05:00
										 |  |  | 	if s.tlsConfig != nil {
 | 
					
						
							|  |  |  | 		l = tls.NewListener(l, s.tlsConfig)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	// Only fill out the TCP server for this one.
 | 
					
						
							| 
									
										
										
										
											2022-12-28 11:14:16 +00:00
										 |  |  | 	s.server[tcp] = &dns.Server{Listener: l,
 | 
					
						
							|  |  |  | 		Net:           "tcp-tls",
 | 
					
						
							|  |  |  | 		MaxTCPQueries: tlsMaxQueries,
 | 
					
						
							|  |  |  | 		ReadTimeout:   s.readTimeout,
 | 
					
						
							|  |  |  | 		WriteTimeout:  s.writeTimeout,
 | 
					
						
							|  |  |  | 		IdleTimeout: func() time.Duration {
 | 
					
						
							|  |  |  | 			return s.idleTimeout
 | 
					
						
							|  |  |  | 		},
 | 
					
						
							|  |  |  | 		Handler: dns.HandlerFunc(func(w dns.ResponseWriter, r *dns.Msg) {
 | 
					
						
							|  |  |  | 			ctx := context.WithValue(context.Background(), Key{}, s.Server)
 | 
					
						
							|  |  |  | 			ctx = context.WithValue(ctx, LoopKey{}, 0)
 | 
					
						
							|  |  |  | 			s.ServeDNS(ctx, w, r)
 | 
					
						
							|  |  |  | 		})}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	s.m.Unlock()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return s.server[tcp].ActivateAndServe()
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ServePacket implements caddy.UDPServer interface.
 | 
					
						
							| 
									
										
										
										
											2017-08-04 09:46:40 -07:00
										 |  |  | func (s *ServerTLS) ServePacket(p net.PacketConn) error { return nil }
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Listen implements caddy.TCPServer interface.
 | 
					
						
							| 
									
										
										
										
											2017-08-04 09:46:40 -07:00
										 |  |  | func (s *ServerTLS) Listen() (net.Listener, error) {
 | 
					
						
							| 
									
										
										
										
											2019-11-17 02:02:46 +00:00
										 |  |  | 	l, err := reuseport.Listen("tcp", s.Addr[len(transport.TLS+"://"):])
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return l, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ListenPacket implements caddy.UDPServer interface.
 | 
					
						
							| 
									
										
										
										
											2017-08-04 09:46:40 -07:00
										 |  |  | func (s *ServerTLS) ListenPacket() (net.PacketConn, error) { return nil, nil }
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | // OnStartupComplete lists the sites served by this server
 | 
					
						
							|  |  |  | // and any relevant information, assuming Quiet is false.
 | 
					
						
							| 
									
										
										
										
											2017-08-04 09:46:40 -07:00
										 |  |  | func (s *ServerTLS) OnStartupComplete() {
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	if Quiet {
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	out := startUpZones(transport.TLS+"://", s.Addr, s.zones)
 | 
					
						
							| 
									
										
										
										
											2018-03-17 19:04:01 +00:00
										 |  |  | 	if out != "" {
 | 
					
						
							|  |  |  | 		fmt.Print(out)
 | 
					
						
							| 
									
										
										
										
											2017-03-13 20:24:37 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2022-12-28 11:14:16 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	tlsMaxQueries = -1
 | 
					
						
							|  |  |  | )
 |