2018-03-22 15:24:52 +00:00
|
|
|
package tunneldns
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
|
2018-04-26 14:57:32 +00:00
|
|
|
"github.com/cloudflare/cloudflared/log"
|
2018-03-22 15:24:52 +00:00
|
|
|
"github.com/cloudflare/cloudflared/metrics"
|
2018-04-26 14:57:32 +00:00
|
|
|
|
2018-03-22 15:24:52 +00:00
|
|
|
"github.com/coredns/coredns/core/dnsserver"
|
|
|
|
"github.com/coredns/coredns/plugin"
|
|
|
|
"github.com/coredns/coredns/plugin/cache"
|
|
|
|
"github.com/pkg/errors"
|
2018-04-26 14:57:32 +00:00
|
|
|
"gopkg.in/urfave/cli.v2"
|
2018-03-22 15:24:52 +00:00
|
|
|
)
|
|
|
|
|
2018-04-26 14:57:32 +00:00
|
|
|
var logger = log.CreateLogger()
|
|
|
|
|
2018-03-22 15:24:52 +00:00
|
|
|
// Listener is an adapter between CoreDNS server and Warp runnable
|
|
|
|
type Listener struct {
|
|
|
|
server *dnsserver.Server
|
|
|
|
wg sync.WaitGroup
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run implements a foreground runner
|
|
|
|
func Run(c *cli.Context) error {
|
|
|
|
metricsListener, err := net.Listen("tcp", c.String("metrics"))
|
|
|
|
if err != nil {
|
2018-04-26 14:57:32 +00:00
|
|
|
logger.WithError(err).Fatal("Failed to open the metrics listener")
|
2018-03-22 15:24:52 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 14:57:32 +00:00
|
|
|
go metrics.ServeMetrics(metricsListener, nil, logger)
|
2018-03-22 15:24:52 +00:00
|
|
|
|
|
|
|
listener, err := CreateListener(c.String("address"), uint16(c.Uint("port")), c.StringSlice("upstream"))
|
|
|
|
if err != nil {
|
2018-04-26 14:57:32 +00:00
|
|
|
logger.WithError(err).Errorf("Failed to create the listeners")
|
2018-03-22 15:24:52 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to start the server
|
2018-04-04 20:58:19 +00:00
|
|
|
readySignal := make(chan struct{})
|
|
|
|
err = listener.Start(readySignal)
|
2018-03-22 15:24:52 +00:00
|
|
|
if err != nil {
|
2018-04-26 14:57:32 +00:00
|
|
|
logger.WithError(err).Errorf("Failed to start the listeners")
|
2018-03-22 15:24:52 +00:00
|
|
|
return listener.Stop()
|
|
|
|
}
|
2018-04-04 20:58:19 +00:00
|
|
|
<-readySignal
|
2018-03-22 15:24:52 +00:00
|
|
|
|
|
|
|
// Wait for signal
|
|
|
|
signals := make(chan os.Signal, 10)
|
|
|
|
signal.Notify(signals, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
defer signal.Stop(signals)
|
|
|
|
<-signals
|
|
|
|
|
|
|
|
// Shut down server
|
|
|
|
err = listener.Stop()
|
|
|
|
if err != nil {
|
2018-04-26 14:57:32 +00:00
|
|
|
logger.WithError(err).Errorf("failed to stop")
|
2018-03-22 15:24:52 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a CoreDNS server plugin from configuration
|
|
|
|
func createConfig(address string, port uint16, p plugin.Handler) *dnsserver.Config {
|
|
|
|
c := &dnsserver.Config{
|
|
|
|
Zone: ".",
|
|
|
|
Transport: "dns",
|
|
|
|
ListenHosts: []string{address},
|
|
|
|
Port: strconv.FormatUint(uint64(port), 10),
|
|
|
|
}
|
|
|
|
|
|
|
|
c.AddPlugin(func(next plugin.Handler) plugin.Handler { return p })
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start blocks for serving requests
|
2018-04-04 20:58:19 +00:00
|
|
|
func (l *Listener) Start(readySignal chan struct{}) error {
|
|
|
|
defer close(readySignal)
|
2018-04-26 14:57:32 +00:00
|
|
|
logger.WithField("addr", l.server.Address()).Infof("Starting DNS over HTTPS proxy server")
|
2018-03-22 15:24:52 +00:00
|
|
|
|
|
|
|
// Start UDP listener
|
|
|
|
if udp, err := l.server.ListenPacket(); err == nil {
|
|
|
|
l.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
l.server.ServePacket(udp)
|
|
|
|
l.wg.Done()
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
return errors.Wrap(err, "failed to create a UDP listener")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start TCP listener
|
|
|
|
tcp, err := l.server.Listen()
|
|
|
|
if err == nil {
|
|
|
|
l.wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
l.server.Serve(tcp)
|
|
|
|
l.wg.Done()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.Wrap(err, "failed to create a TCP listener")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop signals server shutdown and blocks until completed
|
|
|
|
func (l *Listener) Stop() error {
|
|
|
|
if err := l.server.Stop(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.wg.Wait()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateListener configures the server and bound sockets
|
|
|
|
func CreateListener(address string, port uint16, upstreams []string) (*Listener, error) {
|
|
|
|
// Build the list of upstreams
|
|
|
|
upstreamList := make([]Upstream, 0)
|
|
|
|
for _, url := range upstreams {
|
2018-04-26 14:57:32 +00:00
|
|
|
logger.WithField("url", url).Infof("Adding DNS upstream")
|
2018-03-22 15:24:52 +00:00
|
|
|
upstream, err := NewUpstreamHTTPS(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create HTTPS upstream")
|
|
|
|
}
|
|
|
|
upstreamList = append(upstreamList, upstream)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a local cache with HTTPS proxy plugin
|
|
|
|
chain := cache.New()
|
|
|
|
chain.Next = ProxyPlugin{
|
|
|
|
Upstreams: upstreamList,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format an endpoint
|
2018-04-02 20:43:11 +00:00
|
|
|
endpoint := "dns://" + net.JoinHostPort(address, strconv.FormatUint(uint64(port), 10))
|
2018-03-22 15:24:52 +00:00
|
|
|
|
|
|
|
// Create the actual middleware server
|
|
|
|
server, err := dnsserver.NewServer(endpoint, []*dnsserver.Config{createConfig(address, port, NewMetricsPlugin(chain))})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Listener{server: server}, nil
|
|
|
|
}
|