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
|
|
|
"fmt"
|
|
|
|
|
"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-10 10:00:03 -06:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/utils"
|
|
|
|
|
|
2021-11-25 17:09:20 +02:00
|
|
|
"golang.org/x/net/proxy"
|
|
|
|
|
|
2021-04-18 11:57:43 +02:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/protocols/common/protocolstate"
|
2021-02-21 16:31:34 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/types"
|
|
|
|
|
)
|
|
|
|
|
|
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{
|
|
|
|
|
DialContext: dialer.Dial,
|
2022-05-12 13:13:56 +02:00
|
|
|
DialTLSContext: dialer.DialTLS,
|
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
|
|
|
}
|
2021-11-10 10:00:03 -06:00
|
|
|
if types.ProxyURL != "" {
|
|
|
|
|
if proxyURL, err := url.Parse(types.ProxyURL); err == nil {
|
2021-10-18 09:32:38 +02:00
|
|
|
transport.Proxy = http.ProxyURL(proxyURL)
|
|
|
|
|
}
|
2021-11-10 10:00:03 -06:00
|
|
|
} else if types.ProxySocksURL != "" {
|
2021-10-20 00:02:06 +02:00
|
|
|
var proxyAuth *proxy.Auth
|
2021-11-10 10:00:03 -06:00
|
|
|
socksURL, proxyErr := url.Parse(types.ProxySocksURL)
|
2021-10-20 00:02:06 +02:00
|
|
|
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
|
|
|
|
|
}
|
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
|
|
|
}
|