2020-04-13 17:22:00 +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/overwatch"
|
2020-11-25 06:55:13 +00:00
|
|
|
|
|
|
|
"github.com/rs/zerolog"
|
2020-04-13 17:22:00 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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
|
2020-04-13 17:22:00 +00:00
|
|
|
shutdownC chan struct{}
|
|
|
|
configUpdateChan chan config.Root
|
2020-11-25 06:55:13 +00:00
|
|
|
log *zerolog.Logger
|
2020-04-13 17:22:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAppService creates a new AppService with needed supporting services
|
2020-11-25 06:55:13 +00:00
|
|
|
func NewAppService(configManager config.Manager, serviceManager overwatch.Manager, shutdownC chan struct{}, log *zerolog.Logger) *AppService {
|
2020-04-13 17:22:00 +00:00
|
|
|
return &AppService{
|
|
|
|
configManager: configManager,
|
2020-05-01 15:30:50 +00:00
|
|
|
serviceManager: serviceManager,
|
2020-04-13 17:22:00 +00:00
|
|
|
shutdownC: shutdownC,
|
|
|
|
configUpdateChan: make(chan config.Root),
|
2020-11-25 06:55:13 +00:00
|
|
|
log: log,
|
2020-04-13 17:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2020-04-13 17:22:00 +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()
|
2020-04-13 17:22:00 +00:00
|
|
|
}
|
|
|
|
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{}{}
|
2020-04-13 17:22:00 +00:00
|
|
|
for _, f := range c.Forwarders {
|
2020-11-25 06:55:13 +00:00
|
|
|
service := NewForwardService(f, s.log)
|
2020-05-01 15:30:50 +00:00
|
|
|
s.serviceManager.Add(service)
|
|
|
|
activeServices[service.Name()] = struct{}{}
|
2020-04-13 17:22:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 15:30:50 +00:00
|
|
|
// handle resolver changes
|
|
|
|
if c.Resolver.Enabled {
|
2020-11-25 06:55:13 +00:00
|
|
|
service := NewResolverService(c.Resolver, s.log)
|
2020-05-01 15:30:50 +00:00
|
|
|
s.serviceManager.Add(service)
|
|
|
|
activeServices[service.Name()] = struct{}{}
|
|
|
|
|
2020-04-13 17:22:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 15:30:50 +00:00
|
|
|
// TODO: TUN-1451 - tunnels
|
2020-04-13 17:22:00 +00:00
|
|
|
|
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())
|
2020-04-13 17:22:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|