2020-05-01 15:30:50 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-03-08 16:46:23 +00:00
|
|
|
"github.com/cloudflare/cloudflared/config"
|
2020-05-01 15:30:50 +00:00
|
|
|
"github.com/cloudflare/cloudflared/tunneldns"
|
2020-11-25 06:55:13 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
2020-05-01 15:30:50 +00:00
|
|
|
)
|
|
|
|
|
2020-12-28 18:10:01 +00:00
|
|
|
const (
|
|
|
|
// ResolverServiceType is used to identify what kind of overwatch service this is
|
|
|
|
ResolverServiceType = "resolver"
|
|
|
|
|
2021-02-12 17:32:29 +00:00
|
|
|
LogFieldResolverAddress = "resolverAddress"
|
|
|
|
LogFieldResolverPort = "resolverPort"
|
|
|
|
LogFieldResolverMaxUpstreamConns = "resolverMaxUpstreamConns"
|
2020-12-28 18:10:01 +00:00
|
|
|
)
|
2020-05-01 15:30:50 +00:00
|
|
|
|
|
|
|
// ResolverService is used to wrap the tunneldns package's DNS over HTTP
|
|
|
|
// into a service model for the overwatch package.
|
|
|
|
// it also holds a reference to the config object that represents its state
|
|
|
|
type ResolverService struct {
|
|
|
|
resolver config.DNSResolver
|
|
|
|
shutdown chan struct{}
|
2020-11-25 06:55:13 +00:00
|
|
|
log *zerolog.Logger
|
2020-05-01 15:30:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewResolverService creates a new resolver service
|
2020-11-25 06:55:13 +00:00
|
|
|
func NewResolverService(r config.DNSResolver, log *zerolog.Logger) *ResolverService {
|
2020-05-01 15:30:50 +00:00
|
|
|
return &ResolverService{resolver: r,
|
|
|
|
shutdown: make(chan struct{}),
|
2020-11-25 06:55:13 +00:00
|
|
|
log: log,
|
2020-05-01 15:30:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name is used to figure out this service is related to the others (normally the addr it binds to)
|
|
|
|
// this is just "resolver" since there can only be one DNS resolver running
|
|
|
|
func (s *ResolverService) Name() string {
|
|
|
|
return ResolverServiceType
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type is used to identify what kind of overwatch service this is
|
|
|
|
func (s *ResolverService) Type() string {
|
|
|
|
return ResolverServiceType
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash is used to figure out if this forwarder is the unchanged or not from the config file updates
|
|
|
|
func (s *ResolverService) Hash() string {
|
|
|
|
return s.resolver.Hash()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown stops the tunneldns listener
|
|
|
|
func (s *ResolverService) Shutdown() {
|
|
|
|
s.shutdown <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run is the run loop that is started by the overwatch service
|
|
|
|
func (s *ResolverService) Run() error {
|
|
|
|
// create a listener
|
|
|
|
l, err := tunneldns.CreateListener(s.resolver.AddressOrDefault(), s.resolver.PortOrDefault(),
|
2021-02-12 17:32:29 +00:00
|
|
|
s.resolver.UpstreamsOrDefault(), s.resolver.BootstrapsOrDefault(), s.resolver.MaxUpstreamConnectionsOrDefault(), s.log)
|
2020-05-01 15:30:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// start the listener.
|
|
|
|
readySignal := make(chan struct{})
|
|
|
|
err = l.Start(readySignal)
|
|
|
|
if err != nil {
|
2020-11-25 06:55:13 +00:00
|
|
|
_ = l.Stop()
|
2020-05-01 15:30:50 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
<-readySignal
|
2020-12-28 18:10:01 +00:00
|
|
|
|
|
|
|
resolverLog := s.log.With().
|
|
|
|
Str(LogFieldResolverAddress, s.resolver.AddressOrDefault()).
|
|
|
|
Uint16(LogFieldResolverPort, s.resolver.PortOrDefault()).
|
2021-02-12 17:32:29 +00:00
|
|
|
Int(LogFieldResolverMaxUpstreamConns, s.resolver.MaxUpstreamConnectionsOrDefault()).
|
2020-12-28 18:10:01 +00:00
|
|
|
Logger()
|
|
|
|
|
|
|
|
resolverLog.Info().Msg("Starting resolver")
|
2020-05-01 15:30:50 +00:00
|
|
|
|
|
|
|
// wait for shutdown signal
|
|
|
|
<-s.shutdown
|
2020-12-28 18:10:01 +00:00
|
|
|
resolverLog.Info().Msg("Shutting down resolver")
|
2020-05-01 15:30:50 +00:00
|
|
|
return l.Stop()
|
|
|
|
}
|