2023-07-22 00:49:52 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/httpx/common/httpx"
|
2024-02-01 00:42:38 +03:00
|
|
|
"github.com/projectdiscovery/useragent"
|
2023-07-22 00:49:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
HttpSchemes = []string{"https", "http"}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// probeURL probes the scheme for a URL. first HTTPS is tried
|
|
|
|
|
// and if any errors occur http is tried. If none succeeds, probing
|
|
|
|
|
// is abandoned for such URLs.
|
|
|
|
|
func ProbeURL(input string, httpxclient *httpx.HTTPX) string {
|
|
|
|
|
for _, scheme := range HttpSchemes {
|
|
|
|
|
formedURL := fmt.Sprintf("%s://%s", scheme, input)
|
|
|
|
|
req, err := httpxclient.NewRequest(http.MethodHead, formedURL)
|
|
|
|
|
if err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
2024-02-01 00:42:38 +03:00
|
|
|
userAgent := useragent.PickRandom()
|
|
|
|
|
req.Header.Set("User-Agent", userAgent.Raw)
|
2023-07-22 00:49:52 +02:00
|
|
|
|
|
|
|
|
if _, err = httpxclient.Do(req, httpx.UnsafeOptions{}); err != nil {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
return formedURL
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|