package utils
import (
"fmt"
"net"
"net/http"
"strconv"
"github.com/projectdiscovery/httpx/common/httpx"
"github.com/projectdiscovery/nuclei/v3/pkg/input/types"
"github.com/projectdiscovery/useragent"
sliceutil "github.com/projectdiscovery/utils/slice"
)
var commonHttpPorts = []string{
"80",
"8080",
}
var defaultHttpSchemes = []string{
"https",
"http",
}
var httpFirstSchemes = []string{
"http",
"https",
}
func determineSchemeOrder(input string) []string {
if _, port, err := net.SplitHostPort(input); err == nil {
if sliceutil.Contains(commonHttpPorts, port) {
return httpFirstSchemes
}
if port, err := strconv.Atoi(port); err == nil && port > 1024 {
return httpFirstSchemes
}
}
return defaultHttpSchemes
}
func ProbeURL(input string, httpxclient *httpx.HTTPX) string {
schemes := determineSchemeOrder(input)
for _, scheme := range schemes {
formedURL := fmt.Sprintf("%s://%s", scheme, input)
req, err := httpxclient.NewRequest(http.MethodHead, formedURL)
if err != nil {
continue
}
userAgent := useragent.PickRandom()
req.Header.Set("User-Agent", userAgent.Raw)
if _, err = httpxclient.Do(req, httpx.UnsafeOptions{}); err != nil {
continue
}
return formedURL
}
return ""
}
type inputLivenessChecker struct {
client *httpx.HTTPX
}
func (i *inputLivenessChecker) ProbeURL(input string) (string, error) {
return ProbeURL(input, i.client), nil
}
func (i *inputLivenessChecker) Close() error {
if i.client.Dialer != nil {
i.client.Dialer.Close()
}
return nil
}
func GetInputLivenessChecker(client *httpx.HTTPX) types.InputLivenessProbe {
x := &inputLivenessChecker{client: client}
return x
}