config.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package client
  2. import (
  3. "fmt"
  4. "net"
  5. "github.com/google/uuid"
  6. "github.com/rs/zerolog"
  7. "github.com/cloudflare/cloudflared/features"
  8. "github.com/cloudflare/cloudflared/tunnelrpc/pogs"
  9. )
  10. // Config captures the local client runtime configuration.
  11. type Config struct {
  12. ConnectorID uuid.UUID
  13. Version string
  14. Arch string
  15. featureSelector features.FeatureSelector
  16. }
  17. func NewConfig(version string, arch string, featureSelector features.FeatureSelector) (*Config, error) {
  18. connectorID, err := uuid.NewRandom()
  19. if err != nil {
  20. return nil, fmt.Errorf("unable to generate a connector UUID: %w", err)
  21. }
  22. return &Config{
  23. ConnectorID: connectorID,
  24. Version: version,
  25. Arch: arch,
  26. featureSelector: featureSelector,
  27. }, nil
  28. }
  29. // ConnectionOptionsSnapshot is a snapshot of the current client information used to initialize a connection.
  30. //
  31. // The FeatureSnapshot is the features that are available for this connection. At the client level they may
  32. // change, but they will not change within the scope of this struct.
  33. type ConnectionOptionsSnapshot struct {
  34. client pogs.ClientInfo
  35. originLocalIP net.IP
  36. numPreviousAttempts uint8
  37. FeatureSnapshot features.FeatureSnapshot
  38. }
  39. func (c *Config) ConnectionOptionsSnapshot(originIP net.IP, previousAttempts uint8) *ConnectionOptionsSnapshot {
  40. snapshot := c.featureSelector.Snapshot()
  41. return &ConnectionOptionsSnapshot{
  42. client: pogs.ClientInfo{
  43. ClientID: c.ConnectorID[:],
  44. Version: c.Version,
  45. Arch: c.Arch,
  46. Features: snapshot.FeaturesList,
  47. },
  48. originLocalIP: originIP,
  49. numPreviousAttempts: previousAttempts,
  50. FeatureSnapshot: snapshot,
  51. }
  52. }
  53. func (c ConnectionOptionsSnapshot) ConnectionOptions() *pogs.ConnectionOptions {
  54. return &pogs.ConnectionOptions{
  55. Client: c.client,
  56. OriginLocalIP: c.originLocalIP,
  57. ReplaceExisting: false,
  58. CompressionQuality: 0,
  59. NumPreviousAttempts: c.numPreviousAttempts,
  60. }
  61. }
  62. func (c ConnectionOptionsSnapshot) LogFields(event *zerolog.Event) *zerolog.Event {
  63. return event.Strs("features", c.client.Features)
  64. }