Skip to content

Commit

Permalink
Naming tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Nov 25, 2024
1 parent d16339d commit cc002f7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
15 changes: 7 additions & 8 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,24 @@ import (
)

// Create an interface for the fronting context
type Fronting interface {
type Fronted interface {
UpdateConfig(pool *x509.CertPool, providers map[string]*Provider, defaultProviderID string)
NewRoundTripper(timeout time.Duration) (http.RoundTripper, bool)
NewRoundTripper(timeout time.Duration) (http.RoundTripper, error)
Close()
}

var defaultContext = newFrontingContext("default")

// Make sure that the default context is a Fronting
var _ Fronting = defaultContext
var _ Fronted = defaultContext

// Configure sets the masquerades to use, the trusted root CAs, and the
// cache file for caching masquerades to set up direct domain fronting
// in the default context.
//
// defaultProviderID is used when a masquerade without a provider is
// encountered (eg in a cache file)
func NewFronter(pool *x509.CertPool, providers map[string]*Provider, defaultProviderID string, cacheFile string) (Fronting, error) {
func NewFronted(pool *x509.CertPool, providers map[string]*Provider, defaultProviderID string, cacheFile string) (Fronted, error) {
if err := defaultContext.configure(pool, providers, defaultProviderID, cacheFile); err != nil {
return nil, log.Errorf("Error configuring fronting %s context: %s!!", defaultContext.name, err)
}
Expand Down Expand Up @@ -94,18 +94,17 @@ func (fctx *frontingContext) configureWithHello(pool *x509.CertPool, providers m
// 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) NewRoundTripper(timeout time.Duration) (http.RoundTripper, bool) {
func (fctx *frontingContext) NewRoundTripper(timeout time.Duration) (http.RoundTripper, error) {
start := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
instance, err := fctx.instance.Get(ctx)
if err != nil {
log.Errorf("No DirectHttpClient available within %v for context %s", timeout, fctx.name)
return nil, false
return nil, log.Errorf("No DirectHttpClient available within %v for context %s with error %v", timeout, fctx.name, err)
} else {
log.Debugf("DirectHttpClient available for context %s after %v with duration %v", fctx.name, time.Since(start), timeout)
}
return instance.(http.RoundTripper), true
return instance.(http.RoundTripper), nil
}

// Close closes any existing cache file in the default contexxt.
Expand Down
14 changes: 7 additions & 7 deletions fronted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestDirectDomainFrontingWithSNIConfig(t *testing.T) {
testContext.configure(certs, p, "akamai", cacheFile)

transport, ok := testContext.NewRoundTripper(30 * time.Second)
require.True(t, ok)
require.NoError(t, ok)
client := &http.Client{
Transport: transport,
}
Expand Down Expand Up @@ -89,7 +89,7 @@ func doTestDomainFronting(t *testing.T, cacheFile string, expectedMasqueradesAtE
testContext.configure(certs, p, testProviderID, cacheFile)

transport, ok := testContext.NewRoundTripper(30 * time.Second)
require.True(t, ok)
require.NoError(t, ok)

client := &http.Client{
Transport: transport,
Expand All @@ -98,7 +98,7 @@ func doTestDomainFronting(t *testing.T, cacheFile string, expectedMasqueradesAtE
require.True(t, doCheck(client, http.MethodPost, http.StatusAccepted, pingURL))

transport, ok = testContext.NewRoundTripper(30 * time.Second)
require.True(t, ok)
require.NoError(t, ok)
client = &http.Client{
Transport: transport,
}
Expand Down Expand Up @@ -244,7 +244,7 @@ func TestHostAliasesBasic(t *testing.T) {
testContext.configure(certs, map[string]*Provider{"cloudsack": p}, "cloudsack", "")

rt, ok := testContext.NewRoundTripper(30 * time.Second)
if !assert.True(t, ok, "failed to obtain direct roundtripper") {
if !assert.NoError(t, ok, "failed to obtain direct roundtripper") {
return
}
client := &http.Client{Transport: rt}
Expand Down Expand Up @@ -356,7 +356,7 @@ func TestHostAliasesMulti(t *testing.T) {
testContext := newFrontingContext("TestHostAliasesMulti")
testContext.configure(certs, providers, "cloudsack", "")
rt, ok := testContext.NewRoundTripper(30 * time.Second)
if !assert.True(t, ok, "failed to obtain direct roundtripper") {
if !assert.NoError(t, ok, "failed to obtain direct roundtripper") {
return
}
client := &http.Client{Transport: rt}
Expand Down Expand Up @@ -484,7 +484,7 @@ func TestPassthrough(t *testing.T) {
testContext.configure(certs, map[string]*Provider{"cloudsack": p}, "cloudsack", "")

rt, ok := testContext.NewRoundTripper(30 * time.Second)
if !assert.True(t, ok, "failed to obtain direct roundtripper") {
if !assert.NoError(t, ok, "failed to obtain direct roundtripper") {
return
}
client := &http.Client{Transport: rt}
Expand Down Expand Up @@ -637,7 +637,7 @@ func TestCustomValidators(t *testing.T) {
testContext := newFrontingContext(test.name)
setup(testContext, test.validator)
direct, ok := testContext.NewRoundTripper(30 * time.Second)
require.True(t, ok)
require.NoError(t, ok)
client := &http.Client{
Transport: direct,
}
Expand Down

0 comments on commit cc002f7

Please sign in to comment.