2021-02-21 16:31:34 +05:30
|
|
|
package engine
|
|
|
|
|
|
|
|
|
|
import (
|
2021-10-20 00:02:06 +02:00
|
|
|
"context"
|
2021-02-21 16:31:34 +05:30
|
|
|
"crypto/tls"
|
2021-10-20 00:02:06 +02:00
|
|
|
"net"
|
2021-02-21 16:31:34 +05:30
|
|
|
"net/http"
|
2021-10-20 13:26:47 +02:00
|
|
|
"net/http/cookiejar"
|
2021-10-18 09:32:38 +02:00
|
|
|
"net/url"
|
2021-02-21 16:31:34 +05:30
|
|
|
"time"
|
|
|
|
|
|
2021-11-25 17:09:20 +02:00
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
|
|
2023-06-21 13:47:18 +02:00
|
|
|
"github.com/projectdiscovery/fastdialer/fastdialer/ja3/impersonate"
|
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"
|
2021-02-21 16:31:34 +05:30
|
|
|
)
|
|
|
|
|
|
2021-11-25 18:54:16 +02:00
|
|
|
// newHttpClient creates a new http client for headless communication with a timeout
|
|
|
|
|
func newHttpClient(options *types.Options) (*http.Client, error) {
|
2021-04-18 11:57:43 +02:00
|
|
|
dialer := protocolstate.Dialer
|
2021-10-21 13:48:13 -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:48:13 -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
|
|
|
var err error
|
|
|
|
|
tlsConfig, err = utils.AddConfiguredClientCertToRequest(tlsConfig, options)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2021-10-21 13:48:13 -04:00
|
|
|
|
2021-02-21 16:31:34 +05:30
|
|
|
transport := &http.Transport{
|
2023-06-21 13:47:18 +02:00
|
|
|
ForceAttemptHTTP2: options.ForceAttemptHTTP2,
|
|
|
|
|
DialContext: dialer.Dial,
|
|
|
|
|
DialTLSContext: func(ctx context.Context, network, addr string) (net.Conn, error) {
|
|
|
|
|
if options.TlsImpersonate {
|
|
|
|
|
return dialer.DialTLSWithConfigImpersonate(ctx, network, addr, tlsConfig, impersonate.Random, nil)
|
|
|
|
|
}
|
2023-10-13 18:12:28 +02:00
|
|
|
if options.HasClientCertificates() || options.ForceAttemptHTTP2 {
|
|
|
|
|
return dialer.DialTLSWithConfig(ctx, network, addr, tlsConfig)
|
|
|
|
|
}
|
2023-06-21 13:47:18 +02:00
|
|
|
return dialer.DialTLS(ctx, network, addr)
|
|
|
|
|
},
|
2021-02-21 16:31:34 +05:30
|
|
|
MaxIdleConns: 500,
|
|
|
|
|
MaxIdleConnsPerHost: 500,
|
|
|
|
|
MaxConnsPerHost: 500,
|
2021-10-21 13:48:13 -04:00
|
|
|
TLSClientConfig: tlsConfig,
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|
2024-12-13 04:23:27 +05:30
|
|
|
if options.AliveHttpProxy != "" {
|
|
|
|
|
if proxyURL, err := url.Parse(options.AliveHttpProxy); err == nil {
|
2021-10-18 09:32:38 +02: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, err
|
|
|
|
|
}
|
|
|
|
|
dialer, err := proxy.FromURL(socksURL, proxy.Direct)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2021-10-20 00:02:06 +02:00
|
|
|
}
|
2022-08-22 17:48:45 +08:00
|
|
|
|
2021-10-20 00:02:06 +02:00
|
|
|
dc := dialer.(interface {
|
|
|
|
|
DialContext(ctx context.Context, network, addr string) (net.Conn, error)
|
|
|
|
|
})
|
2023-05-25 18:32:35 +02:00
|
|
|
transport.DialContext = dc.DialContext
|
2021-10-18 09:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
2021-10-20 13:26:47 +02:00
|
|
|
jar, _ := cookiejar.New(nil)
|
|
|
|
|
|
|
|
|
|
httpclient := &http.Client{
|
|
|
|
|
Transport: transport,
|
|
|
|
|
Timeout: time.Duration(options.Timeout*3) * time.Second,
|
|
|
|
|
Jar: jar,
|
|
|
|
|
CheckRedirect: func(req *http.Request, via []*http.Request) error {
|
|
|
|
|
// the browser should follow redirects not us
|
|
|
|
|
return http.ErrUseLastResponse
|
|
|
|
|
},
|
2021-10-18 09:32:38 +02:00
|
|
|
}
|
|
|
|
|
|
2021-11-10 18:12:49 +01:00
|
|
|
return httpclient, nil
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|