2021-02-21 16:31:34 +05:30
|
|
|
package engine
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"crypto/tls"
|
2021-10-21 13:48:13 -04:00
|
|
|
"crypto/x509"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"log"
|
2021-02-21 16:31:34 +05:30
|
|
|
"net/http"
|
2021-10-18 09:32:38 +02:00
|
|
|
"net/url"
|
2021-02-21 16:31:34 +05:30
|
|
|
"time"
|
|
|
|
|
|
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"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// newhttpClient creates a new http client for headless communication with a timeout
|
2021-04-19 00:55:33 +05:30
|
|
|
func newhttpClient(options *types.Options) *http.Client {
|
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,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|
|
|
|
|
|
2021-02-21 16:31:34 +05:30
|
|
|
transport := &http.Transport{
|
|
|
|
|
DialContext: dialer.Dial,
|
|
|
|
|
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-10-18 09:32:38 +02:00
|
|
|
|
|
|
|
|
if options.ProxyURL != "" {
|
|
|
|
|
if proxyURL, err := url.Parse(options.ProxyURL); err == nil {
|
|
|
|
|
transport.Proxy = http.ProxyURL(proxyURL)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-19 00:55:33 +05:30
|
|
|
return &http.Client{Transport: transport, Timeout: time.Duration(options.Timeout*3) * time.Second}
|
2021-02-21 16:31:34 +05:30
|
|
|
}
|