cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2020.3.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

connection/connection.go

57lines · modecode

1package connection
2
3import (
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
16const (
17 openStreamTimeout = 30 * time.Second
18)
19
20type Connection struct {
21 id uuid.UUID
22 muxer *h2mux.Muxer
23 addr *net.TCPAddr
24 isLongLived bool
25 longLivedID int
26}
27
28func 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
40func (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
46func (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
55func (c *Connection) Shutdown() {
56 c.muxer.Shutdown()
57}
58