connection.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package connection
  2. import (
  3. "io"
  4. "net/http"
  5. "strconv"
  6. "strings"
  7. "time"
  8. "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
  9. "github.com/google/uuid"
  10. )
  11. const LogFieldConnIndex = "connIndex"
  12. type Config struct {
  13. OriginClient OriginClient
  14. GracePeriod time.Duration
  15. ReplaceExisting bool
  16. }
  17. type NamedTunnelConfig struct {
  18. Credentials Credentials
  19. Client pogs.ClientInfo
  20. }
  21. // Credentials are stored in the credentials file and contain all info needed to run a tunnel.
  22. type Credentials struct {
  23. AccountTag string
  24. TunnelSecret []byte
  25. TunnelID uuid.UUID
  26. TunnelName string
  27. }
  28. func (c *Credentials) Auth() pogs.TunnelAuth {
  29. return pogs.TunnelAuth{
  30. AccountTag: c.AccountTag,
  31. TunnelSecret: c.TunnelSecret,
  32. }
  33. }
  34. type ClassicTunnelConfig struct {
  35. Hostname string
  36. OriginCert []byte
  37. // feature-flag to use new edge reconnect tokens
  38. UseReconnectToken bool
  39. }
  40. func (c *ClassicTunnelConfig) IsTrialZone() bool {
  41. return c.Hostname == ""
  42. }
  43. type OriginClient interface {
  44. Proxy(w ResponseWriter, req *http.Request, isWebsocket bool) error
  45. }
  46. type ResponseWriter interface {
  47. WriteRespHeaders(*http.Response) error
  48. WriteErrorResponse()
  49. io.ReadWriter
  50. }
  51. type ConnectedFuse interface {
  52. Connected()
  53. IsConnected() bool
  54. }
  55. func IsServerSentEvent(headers http.Header) bool {
  56. if contentType := headers.Get("content-type"); contentType != "" {
  57. return strings.HasPrefix(strings.ToLower(contentType), "text/event-stream")
  58. }
  59. return false
  60. }
  61. func uint8ToString(input uint8) string {
  62. return strconv.FormatUint(uint64(input), 10)
  63. }