cloudflare/cloudflared

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2021.3.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

connection/connection.go

113lines · modecode

1package connection
2
3import (
4 "fmt"
5 "io"
6 "net/http"
7 "strconv"
8 "strings"
9 "time"
10
11 "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
12 "github.com/google/uuid"
13)
14
15const LogFieldConnIndex = "connIndex"
16
17type Config struct {
18 OriginProxy OriginProxy
19 GracePeriod time.Duration
20 ReplaceExisting bool
21}
22
23type NamedTunnelConfig struct {
24 Credentials Credentials
25 Client pogs.ClientInfo
26}
27
28// Credentials are stored in the credentials file and contain all info needed to run a tunnel.
29type Credentials struct {
30 AccountTag string
31 TunnelSecret []byte
32 TunnelID uuid.UUID
33 TunnelName string
34}
35
36func (c *Credentials) Auth() pogs.TunnelAuth {
37 return pogs.TunnelAuth{
38 AccountTag: c.AccountTag,
39 TunnelSecret: c.TunnelSecret,
40 }
41}
42
43type ClassicTunnelConfig struct {
44 Hostname string
45 OriginCert []byte
46 // feature-flag to use new edge reconnect tokens
47 UseReconnectToken bool
48}
49
50func (c *ClassicTunnelConfig) IsTrialZone() bool {
51 return c.Hostname == ""
52}
53
54// Type indicates the connection type of the connection.
55type Type int
56
57const (
58 TypeWebsocket Type = iota
59 TypeTCP
60 TypeControlStream
61 TypeHTTP
62)
63
64// ShouldFlush returns whether this kind of connection should actively flush data
65func (t Type) shouldFlush() bool {
66 switch t {
67 case TypeWebsocket, TypeTCP, TypeControlStream:
68 return true
69 default:
70 return false
71 }
72}
73
74func (t Type) String() string {
75 switch t {
76 case TypeWebsocket:
77 return "websocket"
78 case TypeTCP:
79 return "tcp"
80 case TypeControlStream:
81 return "control stream"
82 case TypeHTTP:
83 return "http"
84 default:
85 return fmt.Sprintf("Unknown Type %d", t)
86 }
87}
88
89type OriginProxy interface {
90 // If Proxy returns an error, the caller is responsible for writing the error status to ResponseWriter
91 Proxy(w ResponseWriter, req *http.Request, sourceConnectionType Type) error
92}
93
94type ResponseWriter interface {
95 WriteRespHeaders(status int, header http.Header) error
96 io.Writer
97}
98
99type ConnectedFuse interface {
100 Connected()
101 IsConnected() bool
102}
103
104func IsServerSentEvent(headers http.Header) bool {
105 if contentType := headers.Get("content-type"); contentType != "" {
106 return strings.HasPrefix(strings.ToLower(contentType), "text/event-stream")
107 }
108 return false
109}
110
111func uint8ToString(input uint8) string {
112 return strconv.FormatUint(uint64(input), 10)
113}
114