Skip to content

Commit

Permalink
Avoid ever modifying the set masquerades slice
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Nov 20, 2024
1 parent ff73854 commit 3cf2858
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
23 changes: 12 additions & 11 deletions fronted.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,25 @@ func newFronted(pool *x509.CertPool, providers map[string]*Provider,
return nil, fmt.Errorf("no masquerades found in providers")
}

// copy providers
providersCopy := make(map[string]*Provider, len(providers))
for k, p := range providers {
providersCopy[k] = NewProvider(p.HostAliases, p.TestURL, p.Masquerades, p.Validator, p.PassthroughPatterns, p.SNIConfig, p.VerifyHostname)
}

f := &fronted{
certPool: pool,
masquerades: make(sortedMasquerades, 0, size),
masquerades: loadMasquerades(providersCopy, size),
maxAllowedCachedAge: defaultMaxAllowedCachedAge,
maxCacheSize: defaultMaxCacheSize,
cacheSaveInterval: defaultCacheSaveInterval,
cacheDirty: make(chan interface{}, 1),
cacheClosed: make(chan interface{}),
defaultProviderID: defaultProviderID,
providers: make(map[string]*Provider),
providers: providersCopy,
clientHelloID: clientHelloID,
}

// copy providers
for k, p := range providers {
f.providers[k] = NewProvider(p.HostAliases, p.TestURL, p.Masquerades, p.Validator, p.PassthroughPatterns, p.SNIConfig, p.VerifyHostname)
}

f.loadCandidates(f.providers)
if cacheFile != "" {
f.initCaching(cacheFile)
}
Expand All @@ -89,14 +89,14 @@ func newFronted(pool *x509.CertPool, providers map[string]*Provider,
return f, nil
}

func (f *fronted) loadCandidates(initial map[string]*Provider) {
func loadMasquerades(initial map[string]*Provider, size int) sortedMasquerades {
log.Debugf("Loading candidates for %d providers", len(initial))
defer log.Debug("Finished loading candidates")

masquerades := make(sortedMasquerades, 0, size)
for key, p := range initial {
arr := p.Masquerades
size := len(arr)
log.Debugf("Adding %d candidates for %v", size, key)

// make a shuffled copy of arr
// ('inside-out' Fisher-Yates)
Expand All @@ -108,9 +108,10 @@ func (f *fronted) loadCandidates(initial map[string]*Provider) {
}

for _, c := range sh {
f.masquerades = append(f.masquerades, &masquerade{Masquerade: *c, ProviderID: key})
masquerades = append(masquerades, &masquerade{Masquerade: *c, ProviderID: key})
}
}
return masquerades
}

func (f *fronted) providerFor(m MasqueradeInterface) *Provider {
Expand Down
8 changes: 4 additions & 4 deletions fronted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ func TestVet(t *testing.T) {
t.Fatal("None of the default masquerades vetted successfully")
}

func TestLoadCandidates(t *testing.T) {
func TestLoadMasquerades(t *testing.T) {
providers := testProviders()

expected := make(map[Masquerade]bool)
Expand All @@ -142,12 +142,12 @@ func TestLoadCandidates(t *testing.T) {
}
}

newMasquerades := loadMasquerades(providers, len(expected))

d := &fronted{
masquerades: make(sortedMasquerades, 0, len(expected)),
masquerades: newMasquerades,
}

d.loadCandidates(providers)

actual := make(map[Masquerade]bool)
count := 0
for _, m := range d.masquerades {
Expand Down

0 comments on commit 3cf2858

Please sign in to comment.