cloudflared-mirror/cmd/cloudflared/app_service.go

93 lines
2.5 KiB
Go
Raw Normal View History

package main
import (
"github.com/rs/zerolog"
"github.com/cloudflare/cloudflared/config"
2020-05-01 15:30:50 +00:00
"github.com/cloudflare/cloudflared/overwatch"
)
// AppService is the main service that runs when no command lines flags are passed to cloudflared
// it manages all the running services such as tunnels, forwarders, DNS resolver, etc
type AppService struct {
configManager config.Manager
2020-05-01 15:30:50 +00:00
serviceManager overwatch.Manager
shutdownC chan struct{}
configUpdateChan chan config.Root
log *zerolog.Logger
}
// NewAppService creates a new AppService with needed supporting services
func NewAppService(configManager config.Manager, serviceManager overwatch.Manager, shutdownC chan struct{}, log *zerolog.Logger) *AppService {
return &AppService{
configManager: configManager,
2020-05-01 15:30:50 +00:00
serviceManager: serviceManager,
shutdownC: shutdownC,
configUpdateChan: make(chan config.Root),
log: log,
}
}
// Run starts the run loop to handle config updates and run forwarders, tunnels, etc
func (s *AppService) Run() error {
go s.actionLoop()
return s.configManager.Start(s)
}
// Shutdown kills all the running services
func (s *AppService) Shutdown() error {
s.configManager.Shutdown()
2020-05-01 15:30:50 +00:00
s.shutdownC <- struct{}{}
2020-11-15 01:49:44 +00:00
return nil
}
// ConfigDidUpdate is a delegate notification from the config manager
// it is trigger when the config file has been updated and now the service needs
// to update its services accordingly
func (s *AppService) ConfigDidUpdate(c config.Root) {
s.configUpdateChan <- c
}
// actionLoop handles the actions from running processes
func (s *AppService) actionLoop() {
for {
select {
case c := <-s.configUpdateChan:
s.handleConfigUpdate(c)
case <-s.shutdownC:
2020-05-01 15:30:50 +00:00
for _, service := range s.serviceManager.Services() {
service.Shutdown()
}
return
}
}
}
func (s *AppService) handleConfigUpdate(c config.Root) {
// handle the client forward listeners
2020-05-01 15:30:50 +00:00
activeServices := map[string]struct{}{}
for _, f := range c.Forwarders {
service := NewForwardService(f, s.log)
2020-05-01 15:30:50 +00:00
s.serviceManager.Add(service)
activeServices[service.Name()] = struct{}{}
}
2020-05-01 15:30:50 +00:00
// handle resolver changes
if c.Resolver.Enabled {
service := NewResolverService(c.Resolver, s.log)
2020-05-01 15:30:50 +00:00
s.serviceManager.Add(service)
activeServices[service.Name()] = struct{}{}
}
2020-05-01 15:30:50 +00:00
// TODO: TUN-1451 - tunnels
2020-05-01 15:30:50 +00:00
// remove any services that are no longer active
for _, service := range s.serviceManager.Services() {
if _, ok := activeServices[service.Name()]; !ok {
s.serviceManager.Remove(service.Name())
}
}
}