cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
connection/control.go
128lines · modeblame
12ad264eSudarsan Reddy4 years ago | 1 | package connection |
| 2 | | |
| 3 | import ( | |
| 4 | "context" | |
| 5 | "io" | |
dd540af6Devin Carr4 years ago | 6 | "net" |
12ad264eSudarsan Reddy4 years ago | 7 | "time" |
| 8 | | |
| 9 | "github.com/rs/zerolog" | |
| 10 | | |
991f01feDevin Carr3 years ago | 11 | "github.com/cloudflare/cloudflared/management" |
12ad264eSudarsan Reddy4 years ago | 12 | tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs" |
| 13 | ) | |
| 14 | | |
| 15 | // RPCClientFunc derives a named tunnel rpc client that can then be used to register and unregister connections. | |
| 16 | type RPCClientFunc func(context.Context, io.ReadWriteCloser, *zerolog.Logger) NamedTunnelRPCClient | |
| 17 | | |
| 18 | type controlStream struct { | |
| 19 | observer *Observer | |
| 20 | | |
e22422aacthuang4 years ago | 21 | connectedFuse ConnectedFuse |
| 22 | namedTunnelProperties *NamedTunnelProperties | |
| 23 | connIndex uint8 | |
dd540af6Devin Carr4 years ago | 24 | edgeAddress net.IP |
99f39225Sudarsan Reddy3 years ago | 25 | protocol Protocol |
12ad264eSudarsan Reddy4 years ago | 26 | |
| 27 | newRPCClientFunc RPCClientFunc | |
| 28 | | |
| 29 | gracefulShutdownC <-chan struct{} | |
| 30 | gracePeriod time.Duration | |
| 31 | stoppedGracefully bool | |
| 32 | } | |
| 33 | | |
| 34 | // ControlStreamHandler registers connections with origintunneld and initiates graceful shutdown. | |
| 35 | type ControlStreamHandler interface { | |
1086d5edNuno Diegues4 years ago | 36 | // ServeControlStream handles the control plane of the transport in the current goroutine calling this |
99d4e486João Oliveirinha4 years ago | 37 | ServeControlStream(ctx context.Context, rw io.ReadWriteCloser, connOptions *tunnelpogs.ConnectionOptions, tunnelConfigGetter TunnelConfigJSONGetter) error |
1086d5edNuno Diegues4 years ago | 38 | // IsStopped tells whether the method above has finished |
12ad264eSudarsan Reddy4 years ago | 39 | IsStopped() bool |
| 40 | } | |
| 41 | | |
99d4e486João Oliveirinha4 years ago | 42 | type TunnelConfigJSONGetter interface { |
| 43 | GetConfigJSON() ([]byte, error) | |
| 44 | } | |
| 45 | | |
12ad264eSudarsan Reddy4 years ago | 46 | // NewControlStream returns a new instance of ControlStreamHandler |
| 47 | func NewControlStream( | |
| 48 | observer *Observer, | |
| 49 | connectedFuse ConnectedFuse, | |
e22422aacthuang4 years ago | 50 | namedTunnelConfig *NamedTunnelProperties, |
12ad264eSudarsan Reddy4 years ago | 51 | connIndex uint8, |
dd540af6Devin Carr4 years ago | 52 | edgeAddress net.IP, |
12ad264eSudarsan Reddy4 years ago | 53 | newRPCClientFunc RPCClientFunc, |
| 54 | gracefulShutdownC <-chan struct{}, | |
| 55 | gracePeriod time.Duration, | |
99f39225Sudarsan Reddy3 years ago | 56 | protocol Protocol, |
12ad264eSudarsan Reddy4 years ago | 57 | ) ControlStreamHandler { |
| 58 | if newRPCClientFunc == nil { | |
| 59 | newRPCClientFunc = newRegistrationRPCClient | |
| 60 | } | |
| 61 | return &controlStream{ | |
e22422aacthuang4 years ago | 62 | observer: observer, |
| 63 | connectedFuse: connectedFuse, | |
| 64 | namedTunnelProperties: namedTunnelConfig, | |
| 65 | newRPCClientFunc: newRPCClientFunc, | |
| 66 | connIndex: connIndex, | |
dd540af6Devin Carr4 years ago | 67 | edgeAddress: edgeAddress, |
e22422aacthuang4 years ago | 68 | gracefulShutdownC: gracefulShutdownC, |
| 69 | gracePeriod: gracePeriod, | |
99f39225Sudarsan Reddy3 years ago | 70 | protocol: protocol, |
12ad264eSudarsan Reddy4 years ago | 71 | } |
| 72 | } | |
| 73 | | |
| 74 | func (c *controlStream) ServeControlStream( | |
| 75 | ctx context.Context, | |
| 76 | rw io.ReadWriteCloser, | |
| 77 | connOptions *tunnelpogs.ConnectionOptions, | |
99d4e486João Oliveirinha4 years ago | 78 | tunnelConfigGetter TunnelConfigJSONGetter, |
12ad264eSudarsan Reddy4 years ago | 79 | ) error { |
| 80 | rpcClient := c.newRPCClientFunc(ctx, rw, c.observer.log) | |
| 81 | | |
dd540af6Devin Carr4 years ago | 82 | registrationDetails, err := rpcClient.RegisterConnection(ctx, c.namedTunnelProperties, connOptions, c.connIndex, c.edgeAddress, c.observer) |
99d4e486João Oliveirinha4 years ago | 83 | if err != nil { |
d7da74cbSudarsan Reddy4 years ago | 84 | rpcClient.Close() |
12ad264eSudarsan Reddy4 years ago | 85 | return err |
| 86 | } | |
99f39225Sudarsan Reddy3 years ago | 87 | |
991f01feDevin Carr3 years ago | 88 | c.observer.logConnected(registrationDetails.UUID, c.connIndex, registrationDetails.Location, c.edgeAddress, c.protocol) |
99f39225Sudarsan Reddy3 years ago | 89 | c.observer.sendConnectedEvent(c.connIndex, c.protocol, registrationDetails.Location) |
12ad264eSudarsan Reddy4 years ago | 90 | c.connectedFuse.Connected() |
| 91 | | |
99d4e486João Oliveirinha4 years ago | 92 | // if conn index is 0 and tunnel is not remotely managed, then send local ingress rules configuration |
| 93 | if c.connIndex == 0 && !registrationDetails.TunnelIsRemotelyManaged { | |
| 94 | if tunnelConfig, err := tunnelConfigGetter.GetConfigJSON(); err == nil { | |
| 95 | if err := rpcClient.SendLocalConfiguration(ctx, tunnelConfig, c.observer); err != nil { | |
| 96 | c.observer.log.Err(err).Msg("unable to send local configuration") | |
| 97 | } | |
| 98 | } else { | |
| 99 | c.observer.log.Err(err).Msg("failed to obtain current configuration") | |
| 100 | } | |
| 101 | } | |
| 102 | | |
1086d5edNuno Diegues4 years ago | 103 | c.waitForUnregister(ctx, rpcClient) |
27e1277aSudarsan Reddy4 years ago | 104 | return nil |
| 105 | } | |
| 106 | | |
| 107 | func (c *controlStream) waitForUnregister(ctx context.Context, rpcClient NamedTunnelRPCClient) { | |
12ad264eSudarsan Reddy4 years ago | 108 | // wait for connection termination or start of graceful shutdown |
d7da74cbSudarsan Reddy4 years ago | 109 | defer rpcClient.Close() |
12ad264eSudarsan Reddy4 years ago | 110 | select { |
| 111 | case <-ctx.Done(): | |
| 112 | break | |
| 113 | case <-c.gracefulShutdownC: | |
| 114 | c.stoppedGracefully = true | |
| 115 | } | |
| 116 | | |
| 117 | c.observer.sendUnregisteringEvent(c.connIndex) | |
| 118 | rpcClient.GracefulShutdown(ctx, c.gracePeriod) | |
991f01feDevin Carr3 years ago | 119 | c.observer.log.Info(). |
| 120 | Int(management.EventTypeKey, int(management.Cloudflared)). | |
| 121 | Uint8(LogFieldConnIndex, c.connIndex). | |
| 122 | IPAddr(LogFieldIPAddress, c.edgeAddress). | |
| 123 | Msg("Unregistered tunnel connection") | |
12ad264eSudarsan Reddy4 years ago | 124 | } |
| 125 | | |
| 126 | func (c *controlStream) IsStopped() bool { | |
| 127 | return c.stoppedGracefully | |
| 128 | } |