| 
									
										
										
										
											2018-05-21 19:40:46 +01:00
										 |  |  | package dnsserver
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"context"
 | 
					
						
							|  |  |  | 	"crypto/tls"
 | 
					
						
							|  |  |  | 	"fmt"
 | 
					
						
							|  |  |  | 	"net"
 | 
					
						
							|  |  |  | 	"net/http"
 | 
					
						
							|  |  |  | 	"strconv"
 | 
					
						
							| 
									
										
										
										
											2018-06-27 21:12:27 +01:00
										 |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/plugin/pkg/dnsutil"
 | 
					
						
							| 
									
										
										
										
											2018-07-07 08:22:07 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/doh"
 | 
					
						
							| 
									
										
										
										
											2018-06-27 21:12:27 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/response"
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	"github.com/coredns/coredns/plugin/pkg/transport"
 | 
					
						
							| 
									
										
										
										
											2018-05-21 19:40:46 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ServerHTTPS represents an instance of a DNS-over-HTTPS server.
 | 
					
						
							|  |  |  | type ServerHTTPS struct {
 | 
					
						
							|  |  |  | 	*Server
 | 
					
						
							|  |  |  | 	httpsServer *http.Server
 | 
					
						
							|  |  |  | 	listenAddr  net.Addr
 | 
					
						
							|  |  |  | 	tlsConfig   *tls.Config
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NewServerHTTPS returns a new CoreDNS GRPC server and compiles all plugins in to it.
 | 
					
						
							|  |  |  | func NewServerHTTPS(addr string, group []*Config) (*ServerHTTPS, error) {
 | 
					
						
							|  |  |  | 	s, err := NewServer(addr, group)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	// The *tls* plugin must make sure that multiple conflicting
 | 
					
						
							|  |  |  | 	// TLS configuration return an error: it can only be specified once.
 | 
					
						
							|  |  |  | 	var tlsConfig *tls.Config
 | 
					
						
							|  |  |  | 	for _, conf := range s.zones {
 | 
					
						
							|  |  |  | 		// Should we error if some configs *don't* have TLS?
 | 
					
						
							|  |  |  | 		tlsConfig = conf.TLSConfig
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sh := &ServerHTTPS{Server: s, tlsConfig: tlsConfig, httpsServer: new(http.Server)}
 | 
					
						
							|  |  |  | 	sh.httpsServer.Handler = sh
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return sh, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Serve implements caddy.TCPServer interface.
 | 
					
						
							|  |  |  | func (s *ServerHTTPS) Serve(l net.Listener) error {
 | 
					
						
							|  |  |  | 	s.m.Lock()
 | 
					
						
							|  |  |  | 	s.listenAddr = l.Addr()
 | 
					
						
							|  |  |  | 	s.m.Unlock()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if s.tlsConfig != nil {
 | 
					
						
							|  |  |  | 		l = tls.NewListener(l, s.tlsConfig)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return s.httpsServer.Serve(l)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ServePacket implements caddy.UDPServer interface.
 | 
					
						
							|  |  |  | func (s *ServerHTTPS) ServePacket(p net.PacketConn) error { return nil }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Listen implements caddy.TCPServer interface.
 | 
					
						
							|  |  |  | func (s *ServerHTTPS) Listen() (net.Listener, error) {
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	l, err := net.Listen("tcp", s.Addr[len(transport.HTTPS+"://"):])
 | 
					
						
							| 
									
										
										
										
											2018-05-21 19:40:46 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return l, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ListenPacket implements caddy.UDPServer interface.
 | 
					
						
							|  |  |  | func (s *ServerHTTPS) ListenPacket() (net.PacketConn, error) { return nil, nil }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // OnStartupComplete lists the sites served by this server
 | 
					
						
							|  |  |  | // and any relevant information, assuming Quiet is false.
 | 
					
						
							|  |  |  | func (s *ServerHTTPS) OnStartupComplete() {
 | 
					
						
							|  |  |  | 	if Quiet {
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-19 07:29:37 +01:00
										 |  |  | 	out := startUpZones(transport.HTTPS+"://", s.Addr, s.zones)
 | 
					
						
							| 
									
										
										
										
											2018-05-21 19:40:46 +01:00
										 |  |  | 	if out != "" {
 | 
					
						
							|  |  |  | 		fmt.Print(out)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Stop stops the server. It blocks until the server is totally stopped.
 | 
					
						
							|  |  |  | func (s *ServerHTTPS) Stop() error {
 | 
					
						
							|  |  |  | 	s.m.Lock()
 | 
					
						
							|  |  |  | 	defer s.m.Unlock()
 | 
					
						
							|  |  |  | 	if s.httpsServer != nil {
 | 
					
						
							|  |  |  | 		s.httpsServer.Shutdown(context.Background())
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // ServeHTTP is the handler that gets the HTTP request and converts to the dns format, calls the plugin
 | 
					
						
							|  |  |  | // chain, converts it back and write it to the client.
 | 
					
						
							|  |  |  | func (s *ServerHTTPS) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 08:22:07 +01:00
										 |  |  | 	if r.URL.Path != doh.Path {
 | 
					
						
							| 
									
										
										
										
											2018-05-21 19:40:46 +01:00
										 |  |  | 		http.Error(w, "", http.StatusNotFound)
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 08:22:07 +01:00
										 |  |  | 	msg, err := doh.RequestToMsg(r)
 | 
					
						
							| 
									
										
										
										
											2018-05-21 19:40:46 +01:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		http.Error(w, err.Error(), http.StatusBadRequest)
 | 
					
						
							|  |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-23 13:50:27 +01:00
										 |  |  | 	// Create a DoHWriter with the correct addresses in it.
 | 
					
						
							| 
									
										
										
										
											2018-05-21 19:40:46 +01:00
										 |  |  | 	h, p, _ := net.SplitHostPort(r.RemoteAddr)
 | 
					
						
							| 
									
										
										
										
											2018-05-23 13:50:27 +01:00
										 |  |  | 	port, _ := strconv.Atoi(p)
 | 
					
						
							|  |  |  | 	dw := &DoHWriter{laddr: s.listenAddr, raddr: &net.TCPAddr{IP: net.ParseIP(h), Port: port}}
 | 
					
						
							| 
									
										
										
										
											2018-05-21 19:40:46 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	// We just call the normal chain handler - all error handling is done there.
 | 
					
						
							|  |  |  | 	// We should expect a packet to be returned that we can send to the client.
 | 
					
						
							|  |  |  | 	s.ServeDNS(context.Background(), dw, msg)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	buf, _ := dw.Msg.Pack()
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-27 21:12:27 +01:00
										 |  |  | 	mt, _ := response.Typify(dw.Msg, time.Now().UTC())
 | 
					
						
							|  |  |  | 	age := dnsutil.MinimalTTL(dw.Msg, mt)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-07 08:22:07 +01:00
										 |  |  | 	w.Header().Set("Content-Type", doh.MimeType)
 | 
					
						
							| 
									
										
										
										
											2018-06-27 21:12:27 +01:00
										 |  |  | 	w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%f", age.Seconds()))
 | 
					
						
							| 
									
										
										
										
											2018-05-21 19:40:46 +01:00
										 |  |  | 	w.Header().Set("Content-Length", strconv.Itoa(len(buf)))
 | 
					
						
							|  |  |  | 	w.WriteHeader(http.StatusOK)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	w.Write(buf)
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Shutdown stops the server (non gracefully).
 | 
					
						
							|  |  |  | func (s *ServerHTTPS) Shutdown() error {
 | 
					
						
							|  |  |  | 	if s.httpsServer != nil {
 | 
					
						
							|  |  |  | 		s.httpsServer.Shutdown(context.Background())
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 |