connection.go 2.2 KB

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