cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
cmd/cloudflared/app_forward_service.go
51lines · modecode
| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "github.com/rs/zerolog" |
| 5 | |
| 6 | "github.com/cloudflare/cloudflared/cmd/cloudflared/access" |
| 7 | "github.com/cloudflare/cloudflared/config" |
| 8 | ) |
| 9 | |
| 10 | // ForwardServiceType is used to identify what kind of overwatch service this is |
| 11 | const ForwardServiceType = "forward" |
| 12 | |
| 13 | // ForwarderService is used to wrap the access package websocket forwarders |
| 14 | // into a service model for the overwatch package. |
| 15 | // it also holds a reference to the config object that represents its state |
| 16 | type ForwarderService struct { |
| 17 | forwarder config.Forwarder |
| 18 | shutdown chan struct{} |
| 19 | log *zerolog.Logger |
| 20 | } |
| 21 | |
| 22 | // NewForwardService creates a new forwarder service |
| 23 | func NewForwardService(f config.Forwarder, log *zerolog.Logger) *ForwarderService { |
| 24 | return &ForwarderService{forwarder: f, shutdown: make(chan struct{}, 1), log: log} |
| 25 | } |
| 26 | |
| 27 | // Name is used to figure out this service is related to the others (normally the addr it binds to) |
| 28 | // e.g. localhost:78641 or 127.0.0.1:2222 since this is a websocket forwarder |
| 29 | func (s *ForwarderService) Name() string { |
| 30 | return s.forwarder.Listener |
| 31 | } |
| 32 | |
| 33 | // Type is used to identify what kind of overwatch service this is |
| 34 | func (s *ForwarderService) Type() string { |
| 35 | return ForwardServiceType |
| 36 | } |
| 37 | |
| 38 | // Hash is used to figure out if this forwarder is the unchanged or not from the config file updates |
| 39 | func (s *ForwarderService) Hash() string { |
| 40 | return s.forwarder.Hash() |
| 41 | } |
| 42 | |
| 43 | // Shutdown stops the websocket listener |
| 44 | func (s *ForwarderService) Shutdown() { |
| 45 | s.shutdown <- struct{}{} |
| 46 | } |
| 47 | |
| 48 | // Run is the run loop that is started by the overwatch service |
| 49 | func (s *ForwarderService) Run() error { |
| 50 | return access.StartForwarder(s.forwarder, s.shutdown, s.log) |
| 51 | } |
| 52 | |