cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
cmd/cloudflared/app_service.go
92lines · modecode
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "github.com/rs/zerolog" |
| 5 | |
| 6 | "github.com/cloudflare/cloudflared/config" |
| 7 | "github.com/cloudflare/cloudflared/overwatch" |
| 8 | ) |
| 9 | |
| 10 | // AppService is the main service that runs when no command lines flags are passed to cloudflared |
| 11 | // it manages all the running services such as tunnels, forwarders, DNS resolver, etc |
| 12 | type AppService struct { |
| 13 | configManager config.Manager |
| 14 | serviceManager overwatch.Manager |
| 15 | shutdownC chan struct{} |
| 16 | configUpdateChan chan config.Root |
| 17 | log *zerolog.Logger |
| 18 | } |
| 19 | |
| 20 | // NewAppService creates a new AppService with needed supporting services |
| 21 | func NewAppService(configManager config.Manager, serviceManager overwatch.Manager, shutdownC chan struct{}, log *zerolog.Logger) *AppService { |
| 22 | return &AppService{ |
| 23 | configManager: configManager, |
| 24 | serviceManager: serviceManager, |
| 25 | shutdownC: shutdownC, |
| 26 | configUpdateChan: make(chan config.Root), |
| 27 | log: log, |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | // Run starts the run loop to handle config updates and run forwarders, tunnels, etc |
| 32 | func (s *AppService) Run() error { |
| 33 | go s.actionLoop() |
| 34 | return s.configManager.Start(s) |
| 35 | } |
| 36 | |
| 37 | // Shutdown kills all the running services |
| 38 | func (s *AppService) Shutdown() error { |
| 39 | s.configManager.Shutdown() |
| 40 | s.shutdownC <- struct{}{} |
| 41 | |
| 42 | return nil |
| 43 | } |
| 44 | |
| 45 | // ConfigDidUpdate is a delegate notification from the config manager |
| 46 | // it is trigger when the config file has been updated and now the service needs |
| 47 | // to update its services accordingly |
| 48 | func (s *AppService) ConfigDidUpdate(c config.Root) { |
| 49 | s.configUpdateChan <- c |
| 50 | } |
| 51 | |
| 52 | // actionLoop handles the actions from running processes |
| 53 | func (s *AppService) actionLoop() { |
| 54 | for { |
| 55 | select { |
| 56 | case c := <-s.configUpdateChan: |
| 57 | s.handleConfigUpdate(c) |
| 58 | case <-s.shutdownC: |
| 59 | for _, service := range s.serviceManager.Services() { |
| 60 | service.Shutdown() |
| 61 | } |
| 62 | return |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | func (s *AppService) handleConfigUpdate(c config.Root) { |
| 68 | // handle the client forward listeners |
| 69 | activeServices := map[string]struct{}{} |
| 70 | for _, f := range c.Forwarders { |
| 71 | service := NewForwardService(f, s.log) |
| 72 | s.serviceManager.Add(service) |
| 73 | activeServices[service.Name()] = struct{}{} |
| 74 | } |
| 75 | |
| 76 | // handle resolver changes |
| 77 | if c.Resolver.Enabled { |
| 78 | service := NewResolverService(c.Resolver, s.log) |
| 79 | s.serviceManager.Add(service) |
| 80 | activeServices[service.Name()] = struct{}{} |
| 81 | |
| 82 | } |
| 83 | |
| 84 | // TODO: TUN-1451 - tunnels |
| 85 | |
| 86 | // remove any services that are no longer active |
| 87 | for _, service := range s.serviceManager.Services() { |
| 88 | if _, ok := activeServices[service.Name()]; !ok { |
| 89 | s.serviceManager.Remove(service.Name()) |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |