|
| 1 | +package features |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "hash/fnv" |
| 8 | + "net" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/rs/zerolog" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + featureSelectorHostname = "cfd-features.argotunnel.com" |
| 17 | + defaultRefreshFreq = time.Hour * 6 |
| 18 | + lookupTimeout = time.Second * 10 |
| 19 | +) |
| 20 | + |
| 21 | +type PostQuantumMode uint8 |
| 22 | + |
| 23 | +const ( |
| 24 | + PostQuantumDisabled PostQuantumMode = iota |
| 25 | + // Prefer post quantum, but fallback if connection cannot be established |
| 26 | + PostQuantumPrefer |
| 27 | + // If the user passes the --post-quantum flag, we override |
| 28 | + // CurvePreferences to only support hybrid post-quantum key agreements. |
| 29 | + PostQuantumStrict |
| 30 | +) |
| 31 | + |
| 32 | +// If the TXT record adds other fields, the umarshal logic will ignore those keys |
| 33 | +// If the TXT record is missing a key, the field will unmarshal to the default Go value |
| 34 | +type featuresRecord struct { |
| 35 | + PostQuantumPercentage int32 `json:"pq"` |
| 36 | +} |
| 37 | + |
| 38 | +func NewFeatureSelector(ctx context.Context, accountTag string, staticFeatures StaticFeatures, logger *zerolog.Logger) (*FeatureSelector, error) { |
| 39 | + return newFeatureSelector(ctx, accountTag, logger, newDNSResolver(), staticFeatures, defaultRefreshFreq) |
| 40 | +} |
| 41 | + |
| 42 | +// FeatureSelector determines if this account will try new features. It preiodically queries a DNS TXT record |
| 43 | +// to see which features are turned on |
| 44 | +type FeatureSelector struct { |
| 45 | + accountHash int32 |
| 46 | + logger *zerolog.Logger |
| 47 | + resolver resolver |
| 48 | + |
| 49 | + staticFeatures StaticFeatures |
| 50 | + |
| 51 | + // lock protects concurrent access to dynamic features |
| 52 | + lock sync.RWMutex |
| 53 | + features featuresRecord |
| 54 | +} |
| 55 | + |
| 56 | +// Features set by user provided flags |
| 57 | +type StaticFeatures struct { |
| 58 | + PostQuantumMode *PostQuantumMode |
| 59 | +} |
| 60 | + |
| 61 | +func newFeatureSelector(ctx context.Context, accountTag string, logger *zerolog.Logger, resolver resolver, staticFeatures StaticFeatures, refreshFreq time.Duration) (*FeatureSelector, error) { |
| 62 | + selector := &FeatureSelector{ |
| 63 | + accountHash: switchThreshold(accountTag), |
| 64 | + logger: logger, |
| 65 | + resolver: resolver, |
| 66 | + staticFeatures: staticFeatures, |
| 67 | + } |
| 68 | + |
| 69 | + if err := selector.refresh(ctx); err != nil { |
| 70 | + logger.Err(err).Msg("Failed to fetch features, default to disable") |
| 71 | + } |
| 72 | + |
| 73 | + go selector.refreshLoop(ctx, refreshFreq) |
| 74 | + |
| 75 | + return selector, nil |
| 76 | +} |
| 77 | + |
| 78 | +func (fs *FeatureSelector) PostQuantumMode() PostQuantumMode { |
| 79 | + if fs.staticFeatures.PostQuantumMode != nil { |
| 80 | + return *fs.staticFeatures.PostQuantumMode |
| 81 | + } |
| 82 | + |
| 83 | + fs.lock.RLock() |
| 84 | + defer fs.lock.RUnlock() |
| 85 | + |
| 86 | + if fs.features.PostQuantumPercentage > fs.accountHash { |
| 87 | + return PostQuantumPrefer |
| 88 | + } |
| 89 | + return PostQuantumDisabled |
| 90 | +} |
| 91 | + |
| 92 | +func (fs *FeatureSelector) refreshLoop(ctx context.Context, refreshFreq time.Duration) { |
| 93 | + ticker := time.NewTicker(refreshFreq) |
| 94 | + for { |
| 95 | + select { |
| 96 | + case <-ctx.Done(): |
| 97 | + return |
| 98 | + case <-ticker.C: |
| 99 | + err := fs.refresh(ctx) |
| 100 | + if err != nil { |
| 101 | + fs.logger.Err(err).Msg("Failed to refresh feature selector") |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +func (fs *FeatureSelector) refresh(ctx context.Context) error { |
| 108 | + record, err := fs.resolver.lookupRecord(ctx) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + |
| 113 | + var features featuresRecord |
| 114 | + if err := json.Unmarshal(record, &features); err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + pq_enabled := features.PostQuantumPercentage > fs.accountHash |
| 119 | + fs.logger.Debug().Int32("account_hash", fs.accountHash).Int32("pq_perct", features.PostQuantumPercentage).Bool("pq_enabled", pq_enabled).Msg("Refreshed feature") |
| 120 | + |
| 121 | + fs.lock.Lock() |
| 122 | + defer fs.lock.Unlock() |
| 123 | + |
| 124 | + fs.features = features |
| 125 | + |
| 126 | + return nil |
| 127 | +} |
| 128 | + |
| 129 | +// resolver represents an object that can look up featuresRecord |
| 130 | +type resolver interface { |
| 131 | + lookupRecord(ctx context.Context) ([]byte, error) |
| 132 | +} |
| 133 | + |
| 134 | +type dnsResolver struct { |
| 135 | + resolver *net.Resolver |
| 136 | +} |
| 137 | + |
| 138 | +func newDNSResolver() *dnsResolver { |
| 139 | + return &dnsResolver{ |
| 140 | + resolver: net.DefaultResolver, |
| 141 | + } |
| 142 | +} |
| 143 | + |
| 144 | +func (dr *dnsResolver) lookupRecord(ctx context.Context) ([]byte, error) { |
| 145 | + ctx, cancel := context.WithTimeout(ctx, lookupTimeout) |
| 146 | + defer cancel() |
| 147 | + |
| 148 | + records, err := dr.resolver.LookupTXT(ctx, featureSelectorHostname) |
| 149 | + if err != nil { |
| 150 | + return nil, err |
| 151 | + } |
| 152 | + |
| 153 | + if len(records) == 0 { |
| 154 | + return nil, fmt.Errorf("No TXT record found for %s to determine which features to opt-in", featureSelectorHostname) |
| 155 | + } |
| 156 | + |
| 157 | + return []byte(records[0]), nil |
| 158 | +} |
| 159 | + |
| 160 | +func switchThreshold(accountTag string) int32 { |
| 161 | + h := fnv.New32a() |
| 162 | + _, _ = h.Write([]byte(accountTag)) |
| 163 | + return int32(h.Sum32() % 100) |
| 164 | +} |
0 commit comments