Skip to content

Commit

Permalink
Naming and initialization cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Oct 24, 2024
1 parent c7a73e6 commit 9ce2087
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 130 deletions.
12 changes: 6 additions & 6 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import (
"time"
)

func (d *direct) initCaching(cacheFile string) {
func (d *fronted) initCaching(cacheFile string) {
d.prepopulateMasquerades(cacheFile)
go d.maintainCache(cacheFile)
}

func (d *direct) prepopulateMasquerades(cacheFile string) {
func (d *fronted) prepopulateMasquerades(cacheFile string) {
bytes, err := os.ReadFile(cacheFile)
if err != nil {
// This is not a big deal since we'll just fill the cache later
Expand Down Expand Up @@ -48,7 +48,7 @@ func (d *direct) prepopulateMasquerades(cacheFile string) {
}
}

func (d *direct) markCacheDirty() {
func (d *fronted) markCacheDirty() {
select {
case d.cacheDirty <- nil:
// okay
Expand All @@ -57,7 +57,7 @@ func (d *direct) markCacheDirty() {
}
}

func (d *direct) maintainCache(cacheFile string) {
func (d *fronted) maintainCache(cacheFile string) {
for {
select {
case <-d.cacheClosed:
Expand All @@ -73,7 +73,7 @@ func (d *direct) maintainCache(cacheFile string) {
}
}

func (d *direct) updateCache(cacheFile string) {
func (d *fronted) updateCache(cacheFile string) {
log.Debugf("Updating cache at %v", cacheFile)
cache := d.masquerades.sortedCopy()
sizeToSave := len(cache)
Expand Down Expand Up @@ -101,7 +101,7 @@ func (d *direct) updateCache(cacheFile string) {
}
}

func (d *direct) closeCache() {
func (d *fronted) closeCache() {
d.closeCacheOnce.Do(func() {
close(d.cacheClosed)
})
Expand Down
4 changes: 2 additions & 2 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func TestCaching(t *testing.T) {
cloudsackID: NewProvider(nil, "", nil, nil, nil, nil, nil),
}

makeDirect := func() *direct {
d := &direct{
makeDirect := func() *fronted {
d := &fronted{
masquerades: make(sortedMasquerades, 0, 1000),
maxAllowedCachedAge: 250 * time.Millisecond,
maxCacheSize: 4,
Expand Down
50 changes: 11 additions & 39 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ func Configure(pool *x509.CertPool, providers map[string]*Provider, defaultProvi
}
}

// NewDirect creates a new http.RoundTripper that does direct domain fronting
// NewFronted creates a new http.RoundTripper that does direct domain fronting
// using the default context. If the default context isn't configured within
// the given timeout, this method returns nil, false.
func NewDirect(timeout time.Duration) (http.RoundTripper, bool) {
return DefaultContext.NewDirect(timeout)
func NewFronted(timeout time.Duration) (http.RoundTripper, bool) {
return DefaultContext.NewFronted(timeout)
}

// Close closes any existing cache file in the default context
Expand Down Expand Up @@ -68,51 +68,23 @@ func (fctx *FrontingContext) ConfigureWithHello(pool *x509.CertPool, providers m

_existing, ok := fctx.instance.Get(0)
if ok && _existing != nil {
existing := _existing.(*direct)
existing := _existing.(*fronted)
log.Debugf("Closing cache from existing instance for %s context", fctx.name)
existing.closeCache()
}

size := 0
for _, p := range providers {
size += len(p.Masquerades)
f, err := newFronted(pool, providers, defaultProviderID, cacheFile, clientHelloID)
if err != nil {
return err
}

if size == 0 {
return fmt.Errorf("no masquerades for %s context", fctx.name)
}

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

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

d.loadCandidates(d.providers)
if cacheFile != "" {
d.initCaching(cacheFile)
}
d.findWorkingMasquerades()
fctx.instance.Set(d)
fctx.instance.Set(f)
return nil
}

// NewDirect creates a new http.RoundTripper that does direct domain fronting.
// NewFronted creates a new http.RoundTripper that does direct domain fronting.
// If the context isn't configured within the given timeout, this method
// returns nil, false.
func (fctx *FrontingContext) NewDirect(timeout time.Duration) (http.RoundTripper, bool) {
func (fctx *FrontingContext) NewFronted(timeout time.Duration) (http.RoundTripper, bool) {
instance, ok := fctx.instance.Get(timeout)
if !ok {
log.Errorf("No DirectHttpClient available within %v for context %s", timeout, fctx.name)
Expand All @@ -125,7 +97,7 @@ func (fctx *FrontingContext) NewDirect(timeout time.Duration) (http.RoundTripper
func (fctx *FrontingContext) Close() {
_existing, ok := fctx.instance.Get(0)
if ok && _existing != nil {
existing := _existing.(*direct)
existing := _existing.(*fronted)
log.Debugf("Closing cache from existing instance in %s context", fctx.name)
existing.closeCache()
}
Expand Down
Loading

0 comments on commit 9ce2087

Please sign in to comment.