cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
connection/connection.go
79lines · modecode
| 1 | package connection |
| 2 | |
| 3 | import ( |
| 4 | "io" |
| 5 | "net/http" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/cloudflare/cloudflared/tunnelrpc/pogs" |
| 11 | "github.com/google/uuid" |
| 12 | ) |
| 13 | |
| 14 | const ( |
| 15 | lbProbeUserAgentPrefix = "Mozilla/5.0 (compatible; Cloudflare-Traffic-Manager/1.0; +https://www.cloudflare.com/traffic-manager/;" |
| 16 | ) |
| 17 | |
| 18 | type Config struct { |
| 19 | OriginClient OriginClient |
| 20 | GracePeriod time.Duration |
| 21 | ReplaceExisting bool |
| 22 | } |
| 23 | |
| 24 | type 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. |
| 30 | type Credentials struct { |
| 31 | AccountTag string |
| 32 | TunnelSecret []byte |
| 33 | TunnelID uuid.UUID |
| 34 | TunnelName string |
| 35 | } |
| 36 | |
| 37 | func (c *Credentials) Auth() pogs.TunnelAuth { |
| 38 | return pogs.TunnelAuth{ |
| 39 | AccountTag: c.AccountTag, |
| 40 | TunnelSecret: c.TunnelSecret, |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | type ClassicTunnelConfig struct { |
| 45 | Hostname string |
| 46 | OriginCert []byte |
| 47 | // feature-flag to use new edge reconnect tokens |
| 48 | UseReconnectToken bool |
| 49 | } |
| 50 | |
| 51 | func (c *ClassicTunnelConfig) IsTrialZone() bool { |
| 52 | return c.Hostname == "" |
| 53 | } |
| 54 | |
| 55 | type OriginClient interface { |
| 56 | Proxy(w ResponseWriter, req *http.Request, isWebsocket bool) error |
| 57 | } |
| 58 | |
| 59 | type ResponseWriter interface { |
| 60 | WriteRespHeaders(*http.Response) error |
| 61 | WriteErrorResponse() |
| 62 | io.ReadWriter |
| 63 | } |
| 64 | |
| 65 | type ConnectedFuse interface { |
| 66 | Connected() |
| 67 | IsConnected() bool |
| 68 | } |
| 69 | |
| 70 | func IsServerSentEvent(headers http.Header) bool { |
| 71 | if contentType := headers.Get("content-type"); contentType != "" { |
| 72 | return strings.HasPrefix(strings.ToLower(contentType), "text/event-stream") |
| 73 | } |
| 74 | return false |
| 75 | } |
| 76 | |
| 77 | func uint8ToString(input uint8) string { |
| 78 | return strconv.FormatUint(uint64(input), 10) |
| 79 | } |
| 80 | |