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