2020-12-25 12:55:46 +05:30
|
|
|
package httpclientpool
|
2020-12-23 20:46:19 +05:30
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"crypto/tls"
|
2024-12-19 22:01:41 +07:00
|
|
|
"fmt"
|
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"
|
|
|
|
|
|
2023-06-21 13:47:18 +02:00
|
|
|
"github.com/projectdiscovery/fastdialer/fastdialer/ja3/impersonate"
|
2024-07-15 13:27:15 +03:00
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols"
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/protocolstate"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/protocols/utils"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types"
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/types/scanstrategy"
|
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"
|
2024-12-19 22:01:41 +07:00
|
|
|
urlutil "github.com/projectdiscovery/utils/url"
|
2020-12-23 20:46:19 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
2022-03-15 18:10:05 +05:30
|
|
|
forceMaxRedirects int
|
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 {
|
2022-09-29 00:41:28 +02:00
|
|
|
if options.ShouldFollowHTTPRedirects() {
|
2022-03-15 18:10:05 +05:30
|
|
|
forceMaxRedirects = options.MaxRedirects
|
|
|
|
|
}
|
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
|
2024-11-19 11:51:32 +05:30
|
|
|
// CustomMaxTimeout is the custom timeout for the connection
|
|
|
|
|
// This overrides all other timeouts and is used for accurate time based fuzzing.
|
|
|
|
|
CustomMaxTimeout time.Duration
|
2023-02-13 12:16:41 +01:00
|
|
|
cookiejar *cookiejar.Jar
|
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cc *ConnectionConfiguration) SetCookieJar(cookiejar *cookiejar.Jar) {
|
|
|
|
|
cc.mu.Lock()
|
|
|
|
|
defer cc.mu.Unlock()
|
|
|
|
|
|
|
|
|
|
cc.cookiejar = cookiejar
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cc *ConnectionConfiguration) GetCookieJar() *cookiejar.Jar {
|
|
|
|
|
cc.mu.RLock()
|
|
|
|
|
defer cc.mu.RUnlock()
|
|
|
|
|
|
|
|
|
|
return cc.cookiejar
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (cc *ConnectionConfiguration) HasCookieJar() bool {
|
|
|
|
|
cc.mu.RLock()
|
|
|
|
|
defer cc.mu.RUnlock()
|
|
|
|
|
|
|
|
|
|
return cc.cookiejar != nil
|
2021-08-08 21:52:01 +02:00
|
|
|
}
|
|
|
|
|
|
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
|
2023-11-18 10:32:10 +03:00
|
|
|
// DisableCookie disables cookie reuse for the http client (cookiejar impl)
|
|
|
|
|
DisableCookie bool
|
2022-09-29 00:41:28 +02:00
|
|
|
// FollowRedirects specifies the redirects flow
|
|
|
|
|
RedirectFlow RedirectFlow
|
2021-08-08 21:52:01 +02:00
|
|
|
// Connection defines custom connection configuration
|
|
|
|
|
Connection *ConnectionConfiguration
|
2024-05-25 00:29:04 +05:30
|
|
|
// ResponseHeaderTimeout is the timeout for response body to be read from the server
|
|
|
|
|
ResponseHeaderTimeout time.Duration
|
2020-12-23 20:46:19 +05:30
|
|
|
}
|
|
|
|
|
|
2024-08-19 23:02:27 +03:00
|
|
|
func (c *Configuration) Clone() *Configuration {
|
|
|
|
|
clone := *c
|
|
|
|
|
if c.Connection != nil {
|
|
|
|
|
cloneConnection := &ConnectionConfiguration{
|
|
|
|
|
DisableKeepAlive: c.Connection.DisableKeepAlive,
|
2025-02-13 18:46:28 +05:30
|
|
|
CustomMaxTimeout: c.Connection.CustomMaxTimeout,
|
2024-08-19 23:02:27 +03:00
|
|
|
}
|
|
|
|
|
if c.Connection.HasCookieJar() {
|
|
|
|
|
cookiejar := *c.Connection.GetCookieJar()
|
|
|
|
|
cloneConnection.SetCookieJar(&cookiejar)
|
|
|
|
|
}
|
|
|
|
|
clone.Connection = cloneConnection
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return &clone
|
|
|
|
|
}
|
|
|
|
|
|
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")
|
2022-09-29 00:41:28 +02:00
|
|
|
builder.WriteString(strconv.Itoa(int(c.RedirectFlow)))
|
2021-01-15 14:17:34 +05:30
|
|
|
builder.WriteString("r")
|
2023-11-18 10:32:10 +03:00
|
|
|
builder.WriteString(strconv.FormatBool(c.DisableCookie))
|
2021-08-08 21:52:01 +02:00
|
|
|
builder.WriteString("c")
|
|
|
|
|
builder.WriteString(strconv.FormatBool(c.Connection != nil))
|
2024-11-19 11:51:32 +05:30
|
|
|
if c.Connection != nil && c.Connection.CustomMaxTimeout > 0 {
|
|
|
|
|
builder.WriteString("k")
|
|
|
|
|
builder.WriteString(c.Connection.CustomMaxTimeout.String())
|
|
|
|
|
}
|
2024-05-25 00:29:04 +05:30
|
|
|
builder.WriteString("r")
|
|
|
|
|
builder.WriteString(strconv.FormatInt(int64(c.ResponseHeaderTimeout.Seconds()), 10))
|
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 {
|
2024-05-25 00:29:04 +05:30
|
|
|
return c.Threads == 0 && c.MaxRedirects == 0 && c.RedirectFlow == DontFollowRedirect && c.DisableCookie && c.Connection == nil && !c.NoTimeout && c.ResponseHeaderTimeout == 0
|
2021-08-08 21:52:01 +02:00
|
|
|
}
|
|
|
|
|
|
2020-12-23 22:09:11 +05:30
|
|
|
// GetRawHTTP returns the rawhttp request client
|
2024-07-15 13:27:15 +03:00
|
|
|
func GetRawHTTP(options *protocols.ExecutorOptions) *rawhttp.Client {
|
2025-07-09 14:47:26 -05:00
|
|
|
dialers := protocolstate.GetDialersWithId(options.Options.ExecutionId)
|
|
|
|
|
if dialers == nil {
|
|
|
|
|
panic("dialers not initialized for execution id: " + options.Options.ExecutionId)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Lock the dialers to avoid a race when setting RawHTTPClient
|
|
|
|
|
dialers.Lock()
|
|
|
|
|
defer dialers.Unlock()
|
|
|
|
|
|
|
|
|
|
if dialers.RawHTTPClient != nil {
|
|
|
|
|
return dialers.RawHTTPClient
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-18 13:40:58 -05:00
|
|
|
rawHttpOptionsCopy := *rawhttp.DefaultOptions
|
2025-07-09 14:47:26 -05:00
|
|
|
if options.Options.AliveHttpProxy != "" {
|
2025-07-18 13:40:58 -05:00
|
|
|
rawHttpOptionsCopy.Proxy = options.Options.AliveHttpProxy
|
2025-07-09 14:47:26 -05:00
|
|
|
} else if options.Options.AliveSocksProxy != "" {
|
2025-07-18 13:40:58 -05:00
|
|
|
rawHttpOptionsCopy.Proxy = options.Options.AliveSocksProxy
|
2025-07-09 14:47:26 -05:00
|
|
|
} else if dialers.Fastdialer != nil {
|
2025-07-18 13:40:58 -05:00
|
|
|
rawHttpOptionsCopy.FastDialer = dialers.Fastdialer
|
2025-07-09 14:47:26 -05:00
|
|
|
}
|
2025-07-18 13:40:58 -05:00
|
|
|
rawHttpOptionsCopy.Timeout = options.Options.GetTimeouts().HttpTimeout
|
|
|
|
|
dialers.RawHTTPClient = rawhttp.NewClient(&rawHttpOptionsCopy)
|
2025-07-09 14:47:26 -05:00
|
|
|
return dialers.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() {
|
2025-07-09 14:47:26 -05:00
|
|
|
dialers := protocolstate.GetDialersWithId(options.ExecutionId)
|
|
|
|
|
if dialers == nil {
|
|
|
|
|
return nil, fmt.Errorf("dialers not initialized for %s", options.ExecutionId)
|
|
|
|
|
}
|
|
|
|
|
return dialers.DefaultHTTPClient, nil
|
2020-12-23 22:09:11 +05:30
|
|
|
}
|
2025-07-09 14:47:26 -05:00
|
|
|
|
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
|
|
|
|
|
|
2025-07-09 14:47:26 -05:00
|
|
|
dialers := protocolstate.GetDialersWithId(options.ExecutionId)
|
|
|
|
|
if dialers == nil {
|
|
|
|
|
return nil, fmt.Errorf("dialers not initialized for %s", options.ExecutionId)
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-23 20:46:19 +05:30
|
|
|
hash := configuration.Hash()
|
2025-07-09 14:47:26 -05:00
|
|
|
if client, ok := dialers.HTTPClientPool.Get(hash); ok {
|
2020-12-23 20:46:19 +05:30
|
|
|
return client, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
2024-05-25 00:29:04 +05:30
|
|
|
// do not split given timeout into chunks for retry
|
|
|
|
|
// because this won't work on slow hosts
|
|
|
|
|
retryableHttpOptions.NoAdjustTimeout = true
|
2020-12-23 20:46:19 +05:30
|
|
|
|
2023-09-04 10:24:34 +02:00
|
|
|
if configuration.Threads > 0 || options.ScanStrategy == scanstrategy.HostSpray.String() {
|
2020-12-23 20:46:19 +05:30
|
|
|
// 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
|
2022-09-29 00:41:28 +02:00
|
|
|
redirectFlow := configuration.RedirectFlow
|
2020-12-23 20:46:19 +05:30
|
|
|
maxRedirects := configuration.MaxRedirects
|
|
|
|
|
|
2022-03-15 18:10:05 +05:30
|
|
|
if forceMaxRedirects > 0 {
|
2022-09-29 00:41:28 +02:00
|
|
|
// by default we enable general redirects following
|
|
|
|
|
switch {
|
|
|
|
|
case options.FollowHostRedirects:
|
|
|
|
|
redirectFlow = FollowSameHostRedirect
|
|
|
|
|
default:
|
|
|
|
|
redirectFlow = FollowAllRedirect
|
|
|
|
|
}
|
2022-03-15 18:10:05 +05:30
|
|
|
maxRedirects = forceMaxRedirects
|
|
|
|
|
}
|
2022-04-27 11:19:44 -05:00
|
|
|
if options.DisableRedirects {
|
|
|
|
|
options.FollowRedirects = false
|
2022-09-29 00:41:28 +02:00
|
|
|
options.FollowHostRedirects = false
|
|
|
|
|
redirectFlow = DontFollowRedirect
|
2022-04-27 11:19:44 -05:00
|
|
|
maxRedirects = 0
|
|
|
|
|
}
|
2023-09-04 10:24:34 +02:00
|
|
|
|
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
|
|
|
|
2024-05-25 00:29:04 +05:30
|
|
|
// responseHeaderTimeout is max timeout for response headers to be read
|
2024-07-15 13:27:15 +03:00
|
|
|
responseHeaderTimeout := options.GetTimeouts().HttpResponseHeaderTimeout
|
2024-05-25 00:29:04 +05:30
|
|
|
if configuration.ResponseHeaderTimeout != 0 {
|
|
|
|
|
responseHeaderTimeout = configuration.ResponseHeaderTimeout
|
|
|
|
|
}
|
2024-11-19 11:51:32 +05:30
|
|
|
if configuration.Connection != nil && configuration.Connection.CustomMaxTimeout > 0 {
|
|
|
|
|
responseHeaderTimeout = configuration.Connection.CustomMaxTimeout
|
|
|
|
|
}
|
2024-05-25 00:29:04 +05:30
|
|
|
|
2020-12-23 20:46:19 +05:30
|
|
|
transport := &http.Transport{
|
2023-06-21 13:47:18 +02:00
|
|
|
ForceAttemptHTTP2: options.ForceAttemptHTTP2,
|
2025-07-09 14:47:26 -05:00
|
|
|
DialContext: dialers.Fastdialer.Dial,
|
2023-06-21 13:47:18 +02:00
|
|
|
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
|
if options.TlsImpersonate {
|
2025-07-09 14:47:26 -05:00
|
|
|
return dialers.Fastdialer.DialTLSWithConfigImpersonate(ctx, network, addr, tlsConfig, impersonate.Random, nil)
|
2023-06-21 13:47:18 +02:00
|
|
|
}
|
2023-10-13 18:12:28 +02:00
|
|
|
if options.HasClientCertificates() || options.ForceAttemptHTTP2 {
|
2025-07-09 14:47:26 -05:00
|
|
|
return dialers.Fastdialer.DialTLSWithConfig(ctx, network, addr, tlsConfig)
|
2023-10-13 18:12:28 +02:00
|
|
|
}
|
2025-07-09 14:47:26 -05:00
|
|
|
return dialers.Fastdialer.DialTLS(ctx, network, addr)
|
2023-06-21 13:47:18 +02:00
|
|
|
},
|
2024-04-25 15:37:56 +05:30
|
|
|
MaxIdleConns: maxIdleConns,
|
|
|
|
|
MaxIdleConnsPerHost: maxIdleConnsPerHost,
|
|
|
|
|
MaxConnsPerHost: maxConnsPerHost,
|
|
|
|
|
TLSClientConfig: tlsConfig,
|
|
|
|
|
DisableKeepAlives: disableKeepAlives,
|
2024-05-25 00:29:04 +05:30
|
|
|
ResponseHeaderTimeout: responseHeaderTimeout,
|
2020-12-23 20:46:19 +05:30
|
|
|
}
|
2022-12-04 18:02:01 +01:00
|
|
|
|
2024-12-13 04:23:27 +05:30
|
|
|
if options.AliveHttpProxy != "" {
|
|
|
|
|
if proxyURL, err := url.Parse(options.AliveHttpProxy); err == nil {
|
2021-11-10 10:00:03 -06:00
|
|
|
transport.Proxy = http.ProxyURL(proxyURL)
|
|
|
|
|
}
|
2024-12-13 04:23:27 +05:30
|
|
|
} else if options.AliveSocksProxy != "" {
|
|
|
|
|
socksURL, proxyErr := url.Parse(options.AliveSocksProxy)
|
2022-08-22 17:48:45 +08:00
|
|
|
if proxyErr != nil {
|
|
|
|
|
return nil, proxyErr
|
|
|
|
|
}
|
2023-06-21 13:47:18 +02:00
|
|
|
|
2022-08-22 17:48:45 +08:00
|
|
|
dialer, err := proxy.FromURL(socksURL, proxy.Direct)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2021-12-31 12:46:26 +01:00
|
|
|
}
|
2022-08-22 17:48:45 +08:00
|
|
|
|
2021-12-31 12:46:26 +01:00
|
|
|
dc := dialer.(interface {
|
|
|
|
|
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
|
|
})
|
2023-06-21 13:47:18 +02:00
|
|
|
|
|
|
|
|
transport.DialContext = dc.DialContext
|
|
|
|
|
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
|
2022-07-01 18:01:00 +08:00
|
|
|
}
|
2025-05-15 18:16:49 +07:00
|
|
|
if tlsConfig.ServerName == "" {
|
|
|
|
|
// addr should be in form of host:port already set from canonicalAddr
|
|
|
|
|
host, _, err := net.SplitHostPort(addr)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
tlsConfig.ServerName = host
|
|
|
|
|
}
|
2023-06-21 13:47:18 +02:00
|
|
|
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
|
2023-02-13 12:16:41 +01:00
|
|
|
if configuration.Connection != nil && configuration.Connection.HasCookieJar() {
|
|
|
|
|
jar = configuration.Connection.GetCookieJar()
|
2023-11-18 10:32:10 +03:00
|
|
|
} else if !configuration.DisableCookie {
|
2021-01-15 14:17:34 +05:30
|
|
|
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,
|
2022-09-29 00:41:28 +02:00
|
|
|
CheckRedirect: makeCheckRedirectFunc(redirectFlow, maxRedirects),
|
2022-06-27 18:36:46 +05:30
|
|
|
}
|
|
|
|
|
if !configuration.NoTimeout {
|
2024-07-15 13:27:15 +03:00
|
|
|
httpclient.Timeout = options.GetTimeouts().HttpTimeout
|
2024-11-19 11:51:32 +05:30
|
|
|
if configuration.Connection != nil && configuration.Connection.CustomMaxTimeout > 0 {
|
|
|
|
|
httpclient.Timeout = configuration.Connection.CustomMaxTimeout
|
|
|
|
|
}
|
2022-06-27 18:36:46 +05:30
|
|
|
}
|
|
|
|
|
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 {
|
2025-07-09 14:47:26 -05:00
|
|
|
if err := dialers.HTTPClientPool.Set(hash, client); err != nil {
|
2023-09-04 10:24:34 +02:00
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-01-15 14:17:34 +05:30
|
|
|
}
|
2020-12-23 20:46:19 +05:30
|
|
|
return client, nil
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 00:41:28 +02:00
|
|
|
type RedirectFlow uint8
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
DontFollowRedirect RedirectFlow = iota
|
|
|
|
|
FollowSameHostRedirect
|
|
|
|
|
FollowAllRedirect
|
|
|
|
|
)
|
|
|
|
|
|
2020-12-23 20:46:19 +05:30
|
|
|
const defaultMaxRedirects = 10
|
|
|
|
|
|
|
|
|
|
type checkRedirectFunc func(req *http.Request, via []*http.Request) error
|
|
|
|
|
|
2022-09-29 00:41:28 +02:00
|
|
|
func makeCheckRedirectFunc(redirectType RedirectFlow, maxRedirects int) checkRedirectFunc {
|
2020-12-23 20:46:19 +05:30
|
|
|
return func(req *http.Request, via []*http.Request) error {
|
2022-09-29 00:41:28 +02:00
|
|
|
switch redirectType {
|
|
|
|
|
case DontFollowRedirect:
|
2020-12-23 20:46:19 +05:30
|
|
|
return http.ErrUseLastResponse
|
2022-09-29 00:41:28 +02:00
|
|
|
case FollowSameHostRedirect:
|
|
|
|
|
var newHost = req.URL.Host
|
|
|
|
|
var oldHost = via[0].Host
|
|
|
|
|
if oldHost == "" {
|
|
|
|
|
oldHost = via[0].URL.Host
|
|
|
|
|
}
|
|
|
|
|
if newHost != oldHost {
|
|
|
|
|
// Tell the http client to not follow redirect
|
2020-12-23 20:46:19 +05:30
|
|
|
return http.ErrUseLastResponse
|
|
|
|
|
}
|
2022-09-29 00:41:28 +02:00
|
|
|
return checkMaxRedirects(req, via, maxRedirects)
|
|
|
|
|
case FollowAllRedirect:
|
|
|
|
|
return checkMaxRedirects(req, via, maxRedirects)
|
2020-12-23 20:46:19 +05:30
|
|
|
}
|
2022-09-29 00:41:28 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-12-23 20:46:19 +05:30
|
|
|
|
2024-12-19 22:01:41 +07:00
|
|
|
func checkMaxRedirects(req *http.Request, via []*http.Request, maxRedirects int) error {
|
2022-09-29 00:41:28 +02:00
|
|
|
if maxRedirects == 0 {
|
|
|
|
|
if len(via) > defaultMaxRedirects {
|
2020-12-23 20:46:19 +05:30
|
|
|
return http.ErrUseLastResponse
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2022-09-29 00:41:28 +02:00
|
|
|
|
|
|
|
|
if len(via) > maxRedirects {
|
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
|
}
|
2024-12-19 22:01:41 +07:00
|
|
|
|
|
|
|
|
// NOTE(dwisiswant0): rebuild request URL. See #5900.
|
|
|
|
|
if u := req.URL.String(); !isURLEncoded(u) {
|
|
|
|
|
parsed, err := urlutil.Parse(u)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("%w: %w", ErrRebuildURL, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req.URL = parsed.URL
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 00:41:28 +02:00
|
|
|
return nil
|
2020-12-23 20:46:19 +05:30
|
|
|
}
|
2024-12-19 22:01:41 +07:00
|
|
|
|
|
|
|
|
// isURLEncoded is an helper function to check if the URL is already encoded
|
|
|
|
|
//
|
|
|
|
|
// NOTE(dwisiswant0): shall we move this under `projectdiscovery/utils/urlutil`?
|
|
|
|
|
func isURLEncoded(s string) bool {
|
|
|
|
|
decoded, err := url.QueryUnescape(s)
|
|
|
|
|
if err != nil {
|
|
|
|
|
// If decoding fails, it may indicate a malformed URL/invalid encoding.
|
|
|
|
|
return false
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return decoded != s
|
|
|
|
|
}
|