2020-12-25 12:55:46 +05:30
|
|
|
package httpclientpool
|
2020-12-23 20:46:19 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"crypto/tls"
|
2021-10-21 13:54:56 -04:00
|
|
|
"crypto/x509"
|
2020-12-23 20:46:19 +05:30
|
|
|
"fmt"
|
2021-10-21 13:54:56 -04:00
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
2020-12-23 20:46:19 +05:30
|
|
|
"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"
|
|
|
|
|
|
|
|
|
|
"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
|
|
|
|
|
|
2020-12-23 22:09:11 +05:30
|
|
|
rawhttpClient *rawhttp.Client
|
|
|
|
|
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
|
|
|
|
|
}
|
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
|
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))
|
|
|
|
|
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 {
|
|
|
|
|
return c.Threads == 0 && c.MaxRedirects == 0 && !c.FollowRedirects && !c.CookieReuse && c.Connection == nil
|
|
|
|
|
}
|
|
|
|
|
|
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 {
|
2020-12-23 22:09:11 +05:30
|
|
|
if rawhttpClient == nil {
|
2021-06-25 08:16:54 +02:00
|
|
|
rawhttpOptions := rawhttp.DefaultOptions
|
|
|
|
|
rawhttpOptions.Timeout = time.Duration(options.Timeout) * time.Second
|
|
|
|
|
rawhttpClient = rawhttp.NewClient(rawhttpOptions)
|
2020-12-23 22:09:11 +05:30
|
|
|
}
|
|
|
|
|
return rawhttpClient
|
|
|
|
|
}
|
|
|
|
|
|
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 proxyURL *url.URL
|
|
|
|
|
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()
|
|
|
|
|
|
|
|
|
|
if options.ProxyURL != "" {
|
|
|
|
|
proxyURL, err = url.Parse(options.ProxyURL)
|
|
|
|
|
}
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Multiple Host
|
|
|
|
|
retryablehttpOptions := retryablehttp.DefaultOptionsSpraying
|
|
|
|
|
disableKeepAlives := true
|
|
|
|
|
maxIdleConns := 0
|
|
|
|
|
maxConnsPerHost := 0
|
|
|
|
|
maxIdleConnsPerHost := -1
|
|
|
|
|
|
|
|
|
|
if configuration.Threads > 0 {
|
|
|
|
|
// Single host
|
|
|
|
|
retryablehttpOptions = retryablehttp.DefaultOptionsSingle
|
|
|
|
|
disableKeepAlives = false
|
|
|
|
|
maxIdleConnsPerHost = 500
|
|
|
|
|
maxConnsPerHost = 500
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
retryablehttpOptions.RetryWaitMax = 10 * time.Second
|
|
|
|
|
retryablehttpOptions.RetryMax = options.Retries
|
|
|
|
|
followRedirects := configuration.FollowRedirects
|
|
|
|
|
maxRedirects := configuration.MaxRedirects
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Build the TLS config with the client certificate if it has been configured with the appropriate options.
|
|
|
|
|
// Only one of the options needs to be checked since the validation checks in main.go ensure that all three
|
|
|
|
|
// files are set if any of the client certification configuration options are.
|
|
|
|
|
if len(options.ClientCertFile) > 0 {
|
|
|
|
|
// Load the client certificate using the PEM encoded client certificate and the private key file
|
|
|
|
|
cert, err := tls.LoadX509KeyPair(options.ClientCertFile, options.ClientKeyFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
tlsConfig.Certificates = []tls.Certificate{cert}
|
|
|
|
|
|
|
|
|
|
// Load the certificate authority PEM certificate into the TLS configuration
|
|
|
|
|
caCert, err := ioutil.ReadFile(options.ClientCAFile)
|
|
|
|
|
if err != nil {
|
|
|
|
|
log.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
|
caCertPool.AppendCertsFromPEM(caCert)
|
|
|
|
|
tlsConfig.RootCAs = caCertPool
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 20:46:19 +05:30
|
|
|
transport := &http.Transport{
|
2021-01-16 12:06:27 +05:30
|
|
|
DialContext: Dialer.Dial,
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Attempts to overwrite the dial function with the socks proxied version
|
|
|
|
|
if options.ProxySocksURL != "" {
|
|
|
|
|
var proxyAuth *proxy.Auth
|
|
|
|
|
|
2021-02-26 13:13:11 +05:30
|
|
|
socksURL, proxyErr := url.Parse(options.ProxySocksURL)
|
|
|
|
|
if proxyErr == nil {
|
2020-12-23 20:46:19 +05:30
|
|
|
proxyAuth = &proxy.Auth{}
|
|
|
|
|
proxyAuth.User = socksURL.User.Username()
|
|
|
|
|
proxyAuth.Password, _ = socksURL.User.Password()
|
|
|
|
|
}
|
2021-02-26 13:13:11 +05:30
|
|
|
dialer, proxyErr := proxy.SOCKS5("tcp", fmt.Sprintf("%s:%s", socksURL.Hostname(), socksURL.Port()), proxyAuth, proxy.Direct)
|
2020-12-23 20:46:19 +05:30
|
|
|
dc := dialer.(interface {
|
|
|
|
|
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
|
|
})
|
2021-02-26 13:13:11 +05:30
|
|
|
if proxyErr == nil {
|
2020-12-23 20:46:19 +05:30
|
|
|
transport.DialContext = dc.DialContext
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if proxyURL != nil {
|
|
|
|
|
transport.Proxy = http.ProxyURL(proxyURL)
|
|
|
|
|
}
|
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 20:46:19 +05:30
|
|
|
client := retryablehttp.NewWithHTTPClient(&http.Client{
|
|
|
|
|
Transport: transport,
|
|
|
|
|
Timeout: time.Duration(options.Timeout) * time.Second,
|
|
|
|
|
CheckRedirect: makeCheckRedirectFunc(followRedirects, maxRedirects),
|
|
|
|
|
}, 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
|
|
|
|
|
}
|
|
|
|
|
}
|