selector.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. package features
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "hash/fnv"
  7. "net"
  8. "slices"
  9. "sync"
  10. "time"
  11. "github.com/rs/zerolog"
  12. )
  13. const (
  14. featureSelectorHostname = "cfd-features.argotunnel.com"
  15. defaultRefreshFreq = time.Hour * 6
  16. lookupTimeout = time.Second * 10
  17. )
  18. // If the TXT record adds other fields, the umarshal logic will ignore those keys
  19. // If the TXT record is missing a key, the field will unmarshal to the default Go value
  20. type featuresRecord struct {
  21. // support_datagram_v3
  22. DatagramV3Percentage int32 `json:"dv3"`
  23. // PostQuantumPercentage int32 `json:"pq"` // Removed in TUN-7970
  24. }
  25. func NewFeatureSelector(ctx context.Context, accountTag string, cliFeatures []string, pq bool, logger *zerolog.Logger) (*FeatureSelector, error) {
  26. return newFeatureSelector(ctx, accountTag, logger, newDNSResolver(), cliFeatures, pq, defaultRefreshFreq)
  27. }
  28. // FeatureSelector determines if this account will try new features. It periodically queries a DNS TXT record
  29. // to see which features are turned on.
  30. type FeatureSelector struct {
  31. accountHash int32
  32. logger *zerolog.Logger
  33. resolver resolver
  34. staticFeatures staticFeatures
  35. cliFeatures []string
  36. // lock protects concurrent access to dynamic features
  37. lock sync.RWMutex
  38. features featuresRecord
  39. }
  40. func newFeatureSelector(ctx context.Context, accountTag string, logger *zerolog.Logger, resolver resolver, cliFeatures []string, pq bool, refreshFreq time.Duration) (*FeatureSelector, error) {
  41. // Combine default features and user-provided features
  42. var pqMode *PostQuantumMode
  43. if pq {
  44. mode := PostQuantumStrict
  45. pqMode = &mode
  46. cliFeatures = append(cliFeatures, FeaturePostQuantum)
  47. }
  48. staticFeatures := staticFeatures{
  49. PostQuantumMode: pqMode,
  50. }
  51. selector := &FeatureSelector{
  52. accountHash: switchThreshold(accountTag),
  53. logger: logger,
  54. resolver: resolver,
  55. staticFeatures: staticFeatures,
  56. cliFeatures: Dedup(cliFeatures),
  57. }
  58. if err := selector.refresh(ctx); err != nil {
  59. logger.Err(err).Msg("Failed to fetch features, default to disable")
  60. }
  61. go selector.refreshLoop(ctx, refreshFreq)
  62. return selector, nil
  63. }
  64. func (fs *FeatureSelector) accountEnabled(percentage int32) bool {
  65. return percentage > fs.accountHash
  66. }
  67. func (fs *FeatureSelector) PostQuantumMode() PostQuantumMode {
  68. if fs.staticFeatures.PostQuantumMode != nil {
  69. return *fs.staticFeatures.PostQuantumMode
  70. }
  71. return PostQuantumPrefer
  72. }
  73. func (fs *FeatureSelector) DatagramVersion() DatagramVersion {
  74. fs.lock.RLock()
  75. defer fs.lock.RUnlock()
  76. // If user provides the feature via the cli, we take it as priority over remote feature evaluation
  77. if slices.Contains(fs.cliFeatures, FeatureDatagramV3) {
  78. return DatagramV3
  79. }
  80. // If the user specifies DatagramV2, we also take that over remote
  81. if slices.Contains(fs.cliFeatures, FeatureDatagramV2) {
  82. return DatagramV2
  83. }
  84. if fs.accountEnabled(fs.features.DatagramV3Percentage) {
  85. return DatagramV3
  86. }
  87. return DatagramV2
  88. }
  89. // ClientFeatures will return the list of currently available features that cloudflared should provide to the edge.
  90. //
  91. // This list is dynamic and can change in-between returns.
  92. func (fs *FeatureSelector) ClientFeatures() []string {
  93. // Evaluate any remote features along with static feature list to construct the list of features
  94. return Dedup(slices.Concat(defaultFeatures, fs.cliFeatures, []string{string(fs.DatagramVersion())}))
  95. }
  96. func (fs *FeatureSelector) refreshLoop(ctx context.Context, refreshFreq time.Duration) {
  97. ticker := time.NewTicker(refreshFreq)
  98. for {
  99. select {
  100. case <-ctx.Done():
  101. return
  102. case <-ticker.C:
  103. err := fs.refresh(ctx)
  104. if err != nil {
  105. fs.logger.Err(err).Msg("Failed to refresh feature selector")
  106. }
  107. }
  108. }
  109. }
  110. func (fs *FeatureSelector) refresh(ctx context.Context) error {
  111. record, err := fs.resolver.lookupRecord(ctx)
  112. if err != nil {
  113. return err
  114. }
  115. var features featuresRecord
  116. if err := json.Unmarshal(record, &features); err != nil {
  117. return err
  118. }
  119. fs.lock.Lock()
  120. defer fs.lock.Unlock()
  121. fs.features = features
  122. return nil
  123. }
  124. // resolver represents an object that can look up featuresRecord
  125. type resolver interface {
  126. lookupRecord(ctx context.Context) ([]byte, error)
  127. }
  128. type dnsResolver struct {
  129. resolver *net.Resolver
  130. }
  131. func newDNSResolver() *dnsResolver {
  132. return &dnsResolver{
  133. resolver: net.DefaultResolver,
  134. }
  135. }
  136. func (dr *dnsResolver) lookupRecord(ctx context.Context) ([]byte, error) {
  137. ctx, cancel := context.WithTimeout(ctx, lookupTimeout)
  138. defer cancel()
  139. records, err := dr.resolver.LookupTXT(ctx, featureSelectorHostname)
  140. if err != nil {
  141. return nil, err
  142. }
  143. if len(records) == 0 {
  144. return nil, fmt.Errorf("No TXT record found for %s to determine which features to opt-in", featureSelectorHostname)
  145. }
  146. return []byte(records[0]), nil
  147. }
  148. func switchThreshold(accountTag string) int32 {
  149. h := fnv.New32a()
  150. _, _ = h.Write([]byte(accountTag))
  151. return int32(h.Sum32() % 100)
  152. }