Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/utils/http_probe.go
2070 views
1
package utils
2
3
import (
4
"fmt"
5
"net/http"
6
7
"github.com/projectdiscovery/httpx/common/httpx"
8
"github.com/projectdiscovery/nuclei/v3/pkg/input/types"
9
"github.com/projectdiscovery/useragent"
10
)
11
12
var (
13
HttpSchemes = []string{"https", "http"}
14
)
15
16
// ProbeURL probes the scheme for a URL. first HTTPS is tried
17
// and if any errors occur http is tried. If none succeeds, probing
18
// is abandoned for such URLs.
19
func ProbeURL(input string, httpxclient *httpx.HTTPX) string {
20
for _, scheme := range HttpSchemes {
21
formedURL := fmt.Sprintf("%s://%s", scheme, input)
22
req, err := httpxclient.NewRequest(http.MethodHead, formedURL)
23
if err != nil {
24
continue
25
}
26
userAgent := useragent.PickRandom()
27
req.Header.Set("User-Agent", userAgent.Raw)
28
29
if _, err = httpxclient.Do(req, httpx.UnsafeOptions{}); err != nil {
30
continue
31
}
32
33
return formedURL
34
}
35
return ""
36
}
37
38
type inputLivenessChecker struct {
39
client *httpx.HTTPX
40
}
41
42
// ProbeURL probes the scheme for a URL. first HTTPS is tried
43
func (i *inputLivenessChecker) ProbeURL(input string) (string, error) {
44
return ProbeURL(input, i.client), nil
45
}
46
47
func (i *inputLivenessChecker) Close() error {
48
if i.client.Dialer != nil {
49
i.client.Dialer.Close()
50
}
51
return nil
52
}
53
54
// GetInputLivenessChecker returns a new input liveness checker using provided httpx client
55
func GetInputLivenessChecker(client *httpx.HTTPX) types.InputLivenessProbe {
56
x := &inputLivenessChecker{client: client}
57
return x
58
}
59
60