cloudflare/cloudflared

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2021.5.8

Branches

Tags

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

Clone

HTTPS

Download ZIP

connection/connection.go

114lines · modecode

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