cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
connection/connection.go
57lines · modecode
| 1 | package connection |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net" |
| 6 | "time" |
| 7 | |
| 8 | "github.com/google/uuid" |
| 9 | "github.com/pkg/errors" |
| 10 | "github.com/sirupsen/logrus" |
| 11 | |
| 12 | "github.com/cloudflare/cloudflared/h2mux" |
| 13 | tunnelpogs "github.com/cloudflare/cloudflared/tunnelrpc/pogs" |
| 14 | ) |
| 15 | |
| 16 | const ( |
| 17 | openStreamTimeout = 30 * time.Second |
| 18 | ) |
| 19 | |
| 20 | type Connection struct { |
| 21 | id uuid.UUID |
| 22 | muxer *h2mux.Muxer |
| 23 | addr *net.TCPAddr |
| 24 | isLongLived bool |
| 25 | longLivedID int |
| 26 | } |
| 27 | |
| 28 | func newConnection(muxer *h2mux.Muxer, addr *net.TCPAddr) (*Connection, error) { |
| 29 | id, err := uuid.NewRandom() |
| 30 | if err != nil { |
| 31 | return nil, err |
| 32 | } |
| 33 | return &Connection{ |
| 34 | id: id, |
| 35 | muxer: muxer, |
| 36 | addr: addr, |
| 37 | }, nil |
| 38 | } |
| 39 | |
| 40 | func (c *Connection) Serve(ctx context.Context) error { |
| 41 | // Serve doesn't return until h2mux is shutdown |
| 42 | return c.muxer.Serve(ctx) |
| 43 | } |
| 44 | |
| 45 | // Connect is used to establish connections with cloudflare's edge network |
| 46 | func (c *Connection) Connect(ctx context.Context, parameters *tunnelpogs.ConnectParameters, logger *logrus.Entry) (tunnelpogs.ConnectResult, error) { |
| 47 | tsClient, err := NewRPCClient(ctx, c.muxer, logger.WithField("rpc", "connect"), openStreamTimeout) |
| 48 | if err != nil { |
| 49 | return nil, errors.Wrap(err, "cannot create new RPC connection") |
| 50 | } |
| 51 | defer tsClient.Close() |
| 52 | return tsClient.Connect(ctx, parameters) |
| 53 | } |
| 54 | |
| 55 | func (c *Connection) Shutdown() { |
| 56 | c.muxer.Shutdown() |
| 57 | } |
| 58 | |