aboutsummaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
Diffstat (limited to 'client.go')
-rw-r--r--client.go29
1 files changed, 29 insertions, 0 deletions
diff --git a/client.go b/client.go
index c028e42..2284e9e 100644
--- a/client.go
+++ b/client.go
@@ -66,3 +66,32 @@ func NewHTTPClientWithDNSOverride(dnsMap map[string]string) *http.Client {
Jar: jar,
}
}
+
+// NewHTTPClientWithLocalAddrOverride returns an http.Client suitable for
+// crawling, with a LocalAddr override for making outbound connections using
+// an explicit interface
+func NewHTTPClientWithLocalAddrOverride(addr *net.IPAddr) *http.Client {
+ jar, _ := cookiejar.New(nil) // nolint
+ localTCPAddr := net.TCPAddr{
+ IP: addr.IP,
+ }
+ transport := &http.Transport{
+ DialContext: (&net.Dialer{
+ LocalAddr: &localTCPAddr,
+ Timeout: 30 * time.Second,
+ KeepAlive: 30 * time.Second,
+ DualStack: false,
+ }).DialContext,
+ TLSClientConfig: &tls.Config{
+ InsecureSkipVerify: true, // nolint
+ },
+ }
+ return &http.Client{
+ Timeout: defaultClientTimeout,
+ Transport: transport,
+ CheckRedirect: func(req *http.Request, via []*http.Request) error {
+ return http.ErrUseLastResponse
+ },
+ Jar: jar,
+ }
+}