diff options
author | Jordan <me@jordan.im> | 2022-02-14 23:17:42 -0700 |
---|---|---|
committer | Jordan <me@jordan.im> | 2022-02-14 23:17:42 -0700 |
commit | a39310f111cef49ff630cc12fdebabc4df37ec28 (patch) | |
tree | b2f3778cb49ed2ed2c3283db079285ca0084b704 /client.go | |
parent | a6a6fef1c7cc7d6878e8aa36541565fb3e0c9747 (diff) | |
download | crawl-a39310f111cef49ff630cc12fdebabc4df37ec28.tar.gz crawl-a39310f111cef49ff630cc12fdebabc4df37ec28.zip |
client, crawl: fix/simplify net.Dialer overrides
Diffstat (limited to 'client.go')
-rw-r--r-- | client.go | 47 |
1 files changed, 14 insertions, 33 deletions
@@ -37,11 +37,21 @@ func NewHTTPClient() *http.Client { } } -// NewHTTPClientWithDNSOverride returns an http.Client suitable for -// crawling, with some additional DNS overrides. -func NewHTTPClientWithDNSOverride(dnsMap map[string]string) *http.Client { +// NewHTTPClientWithOverrides returns an http.Client suitable for +// crawling, with additional (optional) DNS and LocalAddr overrides. +func NewHTTPClientWithOverrides(dnsMap map[string]string, localAddr *net.IPAddr) *http.Client { jar, _ := cookiejar.New(nil) // nolint - dialer := new(net.Dialer) + var dialer *net.Dialer + if localAddr != nil { + localTCPAddr := net.TCPAddr{ + IP: localAddr.IP, + } + dialer = &net.Dialer{ + LocalAddr: &localTCPAddr, + } + } else { + dialer = new(net.Dialer) + } transport := &http.Transport{ DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { host, port, err := net.SplitHostPort(addr) @@ -66,32 +76,3 @@ 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, - } -} |