forked from projectdiscovery/retryablehttp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
85 lines (77 loc) · 2.78 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package retryablehttp
import (
"context"
"crypto/tls"
"net"
"net/http"
"os"
"strings"
"time"
"github.com/projectdiscovery/fastdialer/fastdialer"
)
// DisableZTLSFallback disables use of ztls when there is error in tls handshake
// can also be disabled by setting DISABLE_ZTLS_FALLBACK env variable to true
var DisableZTLSFallback = false
// DefaultHostSprayingTransport returns a new http.Transport with similar default values to
// http.DefaultTransport, but with idle connections and keepalives disabled.
func DefaultHostSprayingTransport() *http.Transport {
transport := DefaultReusePooledTransport()
transport.DisableKeepAlives = true
transport.MaxIdleConnsPerHost = -1
return transport
}
// DefaultReusePooledTransport returns a new http.Transport with similar default
// values to http.DefaultTransport. Do not use this for transient transports as
// it can leak file descriptors over time. Only use this for transports that
// will be re-used for the same host(s).
func DefaultReusePooledTransport() *http.Transport {
opts := fastdialer.DefaultOptions
opts.CacheType = fastdialer.Memory
fd, _ := fastdialer.NewDialer(opts)
transport := &http.Transport{
Proxy: http.ProxyFromEnvironment,
MaxIdleConns: 100,
IdleConnTimeout: 90 * time.Second,
TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
MaxIdleConnsPerHost: 100,
MaxResponseHeaderBytes: 4096, // net/http default is 10Mb
TLSClientConfig: &tls.Config{
Renegotiation: tls.RenegotiateOnceAsClient, // Renegotiation is not supported in TLS 1.3 as per docs
InsecureSkipVerify: true,
MinVersion: tls.VersionTLS10,
},
}
if fd != nil {
transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return fd.Dial(ctx, network, addr)
}
transport.DialTLSContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return fd.DialTLS(ctx, network, addr)
}
}
return transport
}
// DefaultClient returns a new http.Client with similar default values to
// http.Client, but with a non-shared Transport, idle connections disabled, and
// keepalives disabled.
func DefaultClient() *http.Client {
return &http.Client{
Transport: DefaultHostSprayingTransport(),
}
}
// DefaultPooledClient returns a new http.Client with similar default values to
// http.Client, but with a shared Transport. Do not use this function for
// transient clients as it can leak file descriptors over time. Only use this
// for clients that will be re-used for the same host(s).
func DefaultPooledClient() *http.Client {
return &http.Client{
Transport: DefaultReusePooledTransport(),
}
}
func init() {
value := os.Getenv("DISABLE_ZTLS_FALLBACK")
if strings.EqualFold(value, "true") {
DisableZTLSFallback = true
}
}