2018-10-08 19:20:28 +00:00
|
|
|
package tunnel
|
2018-05-01 23:45:06 +00:00
|
|
|
|
|
|
|
import (
|
2021-02-12 17:32:29 +00:00
|
|
|
"fmt"
|
|
|
|
|
2018-05-01 23:45:06 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunneldns"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2020-11-25 06:55:13 +00:00
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"github.com/urfave/cli/v2"
|
2018-05-01 23:45:06 +00:00
|
|
|
)
|
|
|
|
|
2021-01-25 21:51:58 +00:00
|
|
|
func runDNSProxyServer(c *cli.Context, dnsReadySignal chan struct{}, shutdownC <-chan struct{}, log *zerolog.Logger) error {
|
2018-05-01 23:45:06 +00:00
|
|
|
port := c.Int("proxy-dns-port")
|
|
|
|
if port <= 0 || port > 65535 {
|
|
|
|
return errors.New("The 'proxy-dns-port' must be a valid port number in <1, 65535> range.")
|
|
|
|
}
|
2021-02-12 17:32:29 +00:00
|
|
|
maxUpstreamConnections := c.Int("proxy-dns-max-upstream-conns")
|
|
|
|
if maxUpstreamConnections < 0 {
|
|
|
|
return fmt.Errorf("'%s' must be 0 or higher", "proxy-dns-max-upstream-conns")
|
|
|
|
}
|
|
|
|
listener, err := tunneldns.CreateListener(c.String("proxy-dns-address"), uint16(port), c.StringSlice("proxy-dns-upstream"), c.StringSlice("proxy-dns-bootstrap"), maxUpstreamConnections, log)
|
2018-05-01 23:45:06 +00:00
|
|
|
if err != nil {
|
|
|
|
close(dnsReadySignal)
|
|
|
|
listener.Stop()
|
|
|
|
return errors.Wrap(err, "Cannot create the DNS over HTTPS proxy server")
|
|
|
|
}
|
|
|
|
|
|
|
|
err = listener.Start(dnsReadySignal)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "Cannot start the DNS over HTTPS proxy server")
|
|
|
|
}
|
|
|
|
<-shutdownC
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = listener.Stop()
|
2021-01-25 21:51:58 +00:00
|
|
|
log.Info().Msg("DNS server stopped")
|
2018-05-01 23:45:06 +00:00
|
|
|
return nil
|
|
|
|
}
|