2020-12-25 12:55:46 +05:30
|
|
|
package httpclientpool
|
2020-12-23 20:46:19 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"crypto/tls"
|
|
|
|
|
"fmt"
|
|
|
|
|
"net"
|
|
|
|
|
"net/http"
|
2021-01-15 14:17:34 +05:30
|
|
|
"net/http/cookiejar"
|
2020-12-23 20:46:19 +05:30
|
|
|
"net/url"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"sync"
|
|
|
|
|
"time"
|
|
|
|
|
|
2021-11-10 10:00:03 -06:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/utils"
|
|
|
|
|
|
2020-12-23 20:46:19 +05:30
|
|
|
"github.com/pkg/errors"
|
2021-09-03 17:25:50 +03:00
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
|
"golang.org/x/net/publicsuffix"
|
|
|
|
|
|
2020-12-23 20:46:19 +05:30
|
|
|
"github.com/projectdiscovery/fastdialer/fastdialer"
|
2021-04-18 11:57:43 +02:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolstate"
|
2020-12-23 20:46:19 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/types"
|
2020-12-23 22:09:11 +05:30
|
|
|
"github.com/projectdiscovery/rawhttp"
|
2020-12-23 20:46:19 +05:30
|
|
|
"github.com/projectdiscovery/retryablehttp-go"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2021-06-26 23:49:31 +08:00
|
|
|
// Dialer is a copy of the fastdialer from protocolstate
|
2021-06-14 17:14:16 +05:30
|
|
|
Dialer *fastdialer.Dialer
|
|
|
|
|
|
2022-03-15 18:10:05 +05:30
|
|
|
rawHttpClient *rawhttp.Client
|
|
|
|
|
forceMaxRedirects int
|
|
|
|
|
poolMutex *sync.RWMutex
|
|
|
|
|
normalClient *retryablehttp.Client
|
|
|
|
|
clientPool map[string]*retryablehttp.Client
|
2020-12-23 20:46:19 +05:30
|
|
|
)
|
|
|
|
|
|
2020-12-23 22:09:11 +05:30
|
|
|
// Init initializes the clientpool implementation
|
|
|
|
|
func Init(options *types.Options) error {
|
2021-09-07 17:31:46 +03:00
|
|
|
// Don't create clients if already created in the past.
|
2020-12-23 22:09:11 +05:30
|
|
|
if normalClient != nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-03-15 18:10:05 +05:30
|
|
|
if options.FollowRedirects {
|
|
|
|
|
forceMaxRedirects = options.MaxRedirects
|
|
|
|
|
}
|
2020-12-23 20:46:19 +05:30
|
|
|
poolMutex = &sync.RWMutex{}
|
|
|
|
|
clientPool = make(map[string]*retryablehttp.Client)
|
2020-12-23 22:09:11 +05:30
|
|
|
|
2020-12-29 11:42:46 +05:30
|
|
|
client, err := wrappedGet(options, &Configuration{})
|
|
|
|
|
if err != nil {
|
2020-12-23 22:09:11 +05:30
|
|
|
return err
|
|
|
|
|
}
|
2020-12-29 11:42:46 +05:30
|
|
|
normalClient = client
|
2020-12-23 22:09:11 +05:30
|
|
|
return nil
|
2020-12-23 20:46:19 +05:30
|
|
|
}
|
|
|
|
|
|
2021-09-03 17:25:50 +03:00
|
|
|
// ConnectionConfiguration contains the custom configuration options for a connection
|
2021-08-08 21:52:01 +02:00
|
|
|
type ConnectionConfiguration struct {
|
|
|
|
|
// DisableKeepAlive of the connection
|
|
|
|
|
DisableKeepAlive bool
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 20:46:19 +05:30
|
|
|
// Configuration contains the custom configuration options for a client
|
|
|
|
|
type Configuration struct {
|
|
|
|
|
// Threads contains the threads for the client
|
|
|
|
|
Threads int
|
|
|
|
|
// MaxRedirects is the maximum number of redirects to follow
|
|
|
|
|
MaxRedirects int
|
2022-06-27 18:36:46 +05:30
|
|
|
// NoTimeout disables http request timeout for context based usage
|
|
|
|
|
NoTimeout bool
|
2021-02-26 13:13:11 +05:30
|
|
|
// CookieReuse enables cookie reuse for the http client (cookiejar impl)
|
|
|
|
|
CookieReuse bool
|
2020-12-23 20:46:19 +05:30
|
|
|
// FollowRedirects specifies whether to follow redirects
|
|
|
|
|
FollowRedirects bool
|
2021-08-08 21:52:01 +02:00
|
|
|
// Connection defines custom connection configuration
|
|
|
|
|
Connection *ConnectionConfiguration
|
2020-12-23 20:46:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Hash returns the hash of the configuration to allow client pooling
|
|
|
|
|
func (c *Configuration) Hash() string {
|
|
|
|
|
builder := &strings.Builder{}
|
2020-12-23 22:09:11 +05:30
|
|
|
builder.Grow(16)
|
2020-12-23 20:46:19 +05:30
|
|
|
builder.WriteString("t")
|
|
|
|
|
builder.WriteString(strconv.Itoa(c.Threads))
|
|
|
|
|
builder.WriteString("m")
|
|
|
|
|
builder.WriteString(strconv.Itoa(c.MaxRedirects))
|
2022-06-27 18:36:46 +05:30
|
|
|
builder.WriteString("n")
|
|
|
|
|
builder.WriteString(strconv.FormatBool(c.NoTimeout))
|
2020-12-23 20:46:19 +05:30
|
|
|
builder.WriteString("f")
|
|
|
|
|
builder.WriteString(strconv.FormatBool(c.FollowRedirects))
|
2021-01-15 14:17:34 +05:30
|
|
|
builder.WriteString("r")
|
|
|
|
|
builder.WriteString(strconv.FormatBool(c.CookieReuse))
|
2021-08-08 21:52:01 +02:00
|
|
|
builder.WriteString("c")
|
|
|
|
|
builder.WriteString(strconv.FormatBool(c.Connection != nil))
|
2020-12-23 20:46:19 +05:30
|
|
|
hash := builder.String()
|
|
|
|
|
return hash
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-03 17:25:50 +03:00
|
|
|
// HasStandardOptions checks whether the configuration requires custom settings
|
2021-08-08 21:52:01 +02:00
|
|
|
func (c *Configuration) HasStandardOptions() bool {
|
2022-06-27 18:36:46 +05:30
|
|
|
return c.Threads == 0 && c.MaxRedirects == 0 && !c.FollowRedirects && !c.CookieReuse && c.Connection == nil && !c.NoTimeout
|
2021-08-08 21:52:01 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-23 22:09:11 +05:30
|
|
|
// GetRawHTTP returns the rawhttp request client
|
2021-06-25 08:16:54 +02:00
|
|
|
func GetRawHTTP(options *types.Options) *rawhttp.Client {
|
2021-11-25 18:54:16 +02:00
|
|
|
if rawHttpClient == nil {
|
|
|
|
|
rawHttpOptions := rawhttp.DefaultOptions
|
2022-03-10 13:49:17 +08:00
|
|
|
if types.ProxyURL != "" {
|
|
|
|
|
rawHttpOptions.Proxy = types.ProxyURL
|
|
|
|
|
} else if types.ProxySocksURL != "" {
|
|
|
|
|
rawHttpOptions.Proxy = types.ProxySocksURL
|
2022-07-26 06:38:53 -05:00
|
|
|
} else if Dialer != nil {
|
|
|
|
|
rawHttpOptions.FastDialer = Dialer
|
2022-03-10 13:49:17 +08:00
|
|
|
}
|
2021-11-25 18:54:16 +02:00
|
|
|
rawHttpOptions.Timeout = time.Duration(options.Timeout) * time.Second
|
|
|
|
|
rawHttpClient = rawhttp.NewClient(rawHttpOptions)
|
2020-12-23 22:09:11 +05:30
|
|
|
}
|
2021-11-25 18:54:16 +02:00
|
|
|
return rawHttpClient
|
2020-12-23 22:09:11 +05:30
|
|
|
}
|
|
|
|
|
|
2020-12-23 20:46:19 +05:30
|
|
|
// Get creates or gets a client for the protocol based on custom configuration
|
|
|
|
|
func Get(options *types.Options, configuration *Configuration) (*retryablehttp.Client, error) {
|
2021-08-08 21:52:01 +02:00
|
|
|
if configuration.HasStandardOptions() {
|
2020-12-23 22:09:11 +05:30
|
|
|
return normalClient, nil
|
|
|
|
|
}
|
2020-12-29 11:42:46 +05:30
|
|
|
return wrappedGet(options, configuration)
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-26 23:49:31 +08:00
|
|
|
// wrappedGet wraps a get operation without normal client check
|
2020-12-29 11:42:46 +05:30
|
|
|
func wrappedGet(options *types.Options, configuration *Configuration) (*retryablehttp.Client, error) {
|
2020-12-23 20:46:19 +05:30
|
|
|
var err error
|
|
|
|
|
|
2021-01-16 12:06:27 +05:30
|
|
|
if Dialer == nil {
|
2021-04-18 11:57:43 +02:00
|
|
|
Dialer = protocolstate.Dialer
|
2020-12-23 20:46:19 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hash := configuration.Hash()
|
|
|
|
|
poolMutex.RLock()
|
|
|
|
|
if client, ok := clientPool[hash]; ok {
|
|
|
|
|
poolMutex.RUnlock()
|
|
|
|
|
return client, nil
|
|
|
|
|
}
|
|
|
|
|
poolMutex.RUnlock()
|
|
|
|
|
|
|
|
|
|
// Multiple Host
|
2021-11-25 18:54:16 +02:00
|
|
|
retryableHttpOptions := retryablehttp.DefaultOptionsSpraying
|
2020-12-23 20:46:19 +05:30
|
|
|
disableKeepAlives := true
|
|
|
|
|
maxIdleConns := 0
|
|
|
|
|
maxConnsPerHost := 0
|
|
|
|
|
maxIdleConnsPerHost := -1
|
|
|
|
|
|
|
|
|
|
if configuration.Threads > 0 {
|
|
|
|
|
// Single host
|
2021-11-25 18:54:16 +02:00
|
|
|
retryableHttpOptions = retryablehttp.DefaultOptionsSingle
|
2020-12-23 20:46:19 +05:30
|
|
|
disableKeepAlives = false
|
|
|
|
|
maxIdleConnsPerHost = 500
|
|
|
|
|
maxConnsPerHost = 500
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-25 18:54:16 +02:00
|
|
|
retryableHttpOptions.RetryWaitMax = 10 * time.Second
|
|
|
|
|
retryableHttpOptions.RetryMax = options.Retries
|
2020-12-23 20:46:19 +05:30
|
|
|
followRedirects := configuration.FollowRedirects
|
|
|
|
|
maxRedirects := configuration.MaxRedirects
|
|
|
|
|
|
2022-03-15 18:10:05 +05:30
|
|
|
if forceMaxRedirects > 0 {
|
|
|
|
|
followRedirects = true
|
|
|
|
|
maxRedirects = forceMaxRedirects
|
|
|
|
|
}
|
2022-04-27 11:19:44 -05:00
|
|
|
if options.DisableRedirects {
|
|
|
|
|
options.FollowRedirects = false
|
|
|
|
|
followRedirects = false
|
|
|
|
|
maxRedirects = 0
|
|
|
|
|
}
|
2021-08-08 21:52:01 +02:00
|
|
|
// override connection's settings if required
|
|
|
|
|
if configuration.Connection != nil {
|
|
|
|
|
disableKeepAlives = configuration.Connection.DisableKeepAlive
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-21 13:54:56 -04:00
|
|
|
// Set the base TLS configuration definition
|
|
|
|
|
tlsConfig := &tls.Config{
|
|
|
|
|
Renegotiation: tls.RenegotiateOnceAsClient,
|
|
|
|
|
InsecureSkipVerify: true,
|
2022-06-04 14:15:16 +02:00
|
|
|
MinVersion: tls.VersionTLS10,
|
2021-10-21 13:54:56 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-11 12:30:39 +02:00
|
|
|
if options.SNI != "" {
|
|
|
|
|
tlsConfig.ServerName = options.SNI
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-27 12:11:42 -04:00
|
|
|
// Add the client certificate authentication to the request if it's configured
|
2021-11-10 18:12:49 +01:00
|
|
|
tlsConfig, err = utils.AddConfiguredClientCertToRequest(tlsConfig, options)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "could not create client certificate")
|
|
|
|
|
}
|
2021-10-21 13:54:56 -04:00
|
|
|
|
2020-12-23 20:46:19 +05:30
|
|
|
transport := &http.Transport{
|
2021-01-16 12:06:27 +05:30
|
|
|
DialContext: Dialer.Dial,
|
2022-05-12 13:13:56 +02:00
|
|
|
DialTLSContext: Dialer.DialTLS,
|
2020-12-23 20:46:19 +05:30
|
|
|
MaxIdleConns: maxIdleConns,
|
|
|
|
|
MaxIdleConnsPerHost: maxIdleConnsPerHost,
|
|
|
|
|
MaxConnsPerHost: maxConnsPerHost,
|
2021-10-21 13:54:56 -04:00
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
|
DisableKeepAlives: disableKeepAlives,
|
2020-12-23 20:46:19 +05:30
|
|
|
}
|
2021-12-31 12:46:26 +01:00
|
|
|
if types.ProxyURL != "" {
|
|
|
|
|
if proxyURL, err := url.Parse(types.ProxyURL); err == nil {
|
2021-11-10 10:00:03 -06:00
|
|
|
transport.Proxy = http.ProxyURL(proxyURL)
|
|
|
|
|
}
|
2021-12-31 12:46:26 +01:00
|
|
|
} else if types.ProxySocksURL != "" {
|
|
|
|
|
var proxyAuth *proxy.Auth
|
|
|
|
|
socksURL, proxyErr := url.Parse(types.ProxySocksURL)
|
|
|
|
|
if proxyErr == nil {
|
|
|
|
|
proxyAuth = &proxy.Auth{}
|
|
|
|
|
proxyAuth.User = socksURL.User.Username()
|
|
|
|
|
proxyAuth.Password, _ = socksURL.User.Password()
|
|
|
|
|
}
|
|
|
|
|
dialer, proxyErr := proxy.SOCKS5("tcp", fmt.Sprintf("%s:%s", socksURL.Hostname(), socksURL.Port()), proxyAuth, proxy.Direct)
|
|
|
|
|
dc := dialer.(interface {
|
|
|
|
|
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
|
|
})
|
|
|
|
|
if proxyErr == nil {
|
|
|
|
|
transport.DialContext = dc.DialContext
|
2022-07-01 18:01:00 +08:00
|
|
|
transport.DialTLSContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
|
// upgrade proxy connection to tls
|
|
|
|
|
conn, err := dc.DialContext(ctx, network, addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
return tls.Client(conn, tlsConfig), nil
|
|
|
|
|
}
|
2021-12-31 12:46:26 +01:00
|
|
|
}
|
2020-12-23 20:46:19 +05:30
|
|
|
}
|
|
|
|
|
|
2021-01-15 14:17:34 +05:30
|
|
|
var jar *cookiejar.Jar
|
|
|
|
|
if configuration.CookieReuse {
|
|
|
|
|
if jar, err = cookiejar.New(&cookiejar.Options{PublicSuffixList: publicsuffix.List}); err != nil {
|
|
|
|
|
return nil, errors.Wrap(err, "could not create cookiejar")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-27 18:36:46 +05:30
|
|
|
httpclient := &http.Client{
|
2020-12-23 20:46:19 +05:30
|
|
|
Transport: transport,
|
|
|
|
|
CheckRedirect: makeCheckRedirectFunc(followRedirects, maxRedirects),
|
2022-06-27 18:36:46 +05:30
|
|
|
}
|
|
|
|
|
if !configuration.NoTimeout {
|
|
|
|
|
httpclient.Timeout = time.Duration(options.Timeout) * time.Second
|
|
|
|
|
}
|
|
|
|
|
client := retryablehttp.NewWithHTTPClient(httpclient, retryableHttpOptions)
|
2021-01-15 14:17:34 +05:30
|
|
|
if jar != nil {
|
|
|
|
|
client.HTTPClient.Jar = jar
|
|
|
|
|
}
|
2020-12-23 22:09:11 +05:30
|
|
|
client.CheckRetry = retryablehttp.HostSprayRetryPolicy()
|
2020-12-23 20:46:19 +05:30
|
|
|
|
2021-01-15 14:17:34 +05:30
|
|
|
// Only add to client pool if we don't have a cookie jar in place.
|
|
|
|
|
if jar == nil {
|
|
|
|
|
poolMutex.Lock()
|
|
|
|
|
clientPool[hash] = client
|
|
|
|
|
poolMutex.Unlock()
|
|
|
|
|
}
|
2020-12-23 20:46:19 +05:30
|
|
|
return client, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const defaultMaxRedirects = 10
|
|
|
|
|
|
|
|
|
|
type checkRedirectFunc func(req *http.Request, via []*http.Request) error
|
|
|
|
|
|
|
|
|
|
func makeCheckRedirectFunc(followRedirects bool, maxRedirects int) checkRedirectFunc {
|
|
|
|
|
return func(req *http.Request, via []*http.Request) error {
|
|
|
|
|
if !followRedirects {
|
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if maxRedirects == 0 {
|
|
|
|
|
if len(via) > defaultMaxRedirects {
|
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(via) > maxRedirects {
|
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|