-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make subdomain testing possible
- Loading branch information
1 parent
65a237b
commit b7037fc
Showing
4 changed files
with
99 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package test | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
logging "github.com/ipfs/go-log" | ||
) | ||
|
||
var log = logging.Logger("conformance") | ||
|
||
func GetEnv(key string, fallback string) string { | ||
if value, ok := os.LookupEnv(key); ok { | ||
return value | ||
} | ||
return fallback | ||
} | ||
|
||
var GatewayUrl = strings.TrimRight( | ||
GetEnv("GATEWAY_URL", "http://localhost:8080"), | ||
"/") | ||
|
||
var SubdomainGatewayUrl = strings.TrimRight( | ||
GetEnv("SUBDOMAIN_GATEWAY_URL", "http://example.com:8080"), | ||
"/") | ||
|
||
var GatewayHost = "" | ||
var SubdomainGatewayHost = "" | ||
|
||
func init() { | ||
parse, err := url.Parse(GatewayUrl) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
GatewayHost = parse.Host | ||
|
||
parse, err = url.Parse(SubdomainGatewayUrl) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
SubdomainGatewayHost = parse.Host | ||
log.Debugf("SubdomainGatewayHost: %s", SubdomainGatewayHost) | ||
} | ||
|
||
func NewDialer() *net.Dialer { | ||
dialer := &net.Dialer{ | ||
Timeout: 30 * time.Second, | ||
KeepAlive: 30 * time.Second, | ||
DualStack: true, | ||
Resolver: &net.Resolver{ | ||
PreferGo: true, | ||
Dial: func(ctx context.Context, network, address string) (net.Conn, error) { | ||
log.Debugf("Custom Resolver dialing into network: %s, address: %s", network, address) | ||
|
||
d := net.Dialer{ | ||
Timeout: 30 * time.Second, | ||
} | ||
|
||
return d.DialContext(ctx, network, address) | ||
}, | ||
}, | ||
} | ||
|
||
http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { | ||
log.Debugf("Custom Dialer dialing into network: %s, address: %s", network, addr) | ||
|
||
// If we call into a subdomain `somethingsomething.example.com`, | ||
// actually dial the gateway url on its base address (probably localhost:8080) | ||
if strings.HasSuffix(addr, SubdomainGatewayHost) { | ||
addr = GatewayHost | ||
} | ||
|
||
log.Debugf("Custom Dialer dialing into (effective) network: %s, address: %s", network, addr) | ||
conn, err := dialer.DialContext(ctx, network, addr) | ||
return conn, err | ||
} | ||
|
||
return dialer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters