Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/utils/utils.go
2070 views
1
package utils
2
3
import (
4
"errors"
5
"fmt"
6
"io"
7
"net/url"
8
"strings"
9
10
"github.com/cespare/xxhash"
11
"github.com/projectdiscovery/nuclei/v3/pkg/catalog"
12
"github.com/projectdiscovery/retryablehttp-go"
13
mapsutil "github.com/projectdiscovery/utils/maps"
14
"golang.org/x/exp/constraints"
15
)
16
17
func IsBlank(value string) bool {
18
return strings.TrimSpace(value) == ""
19
}
20
21
func UnwrapError(err error) error {
22
for { // get the last wrapped error
23
unwrapped := errors.Unwrap(err)
24
if unwrapped == nil {
25
break
26
}
27
err = unwrapped
28
}
29
return err
30
}
31
32
// IsURL tests a string to determine if it is a well-structured url or not.
33
func IsURL(input string) bool {
34
u, err := url.Parse(input)
35
return err == nil && u.Scheme != "" && u.Host != ""
36
}
37
38
// ReaderFromPathOrURL reads and returns the contents of a file or url.
39
func ReaderFromPathOrURL(templatePath string, catalog catalog.Catalog) (io.ReadCloser, error) {
40
if IsURL(templatePath) {
41
resp, err := retryablehttp.DefaultClient().Get(templatePath)
42
if err != nil {
43
return nil, err
44
}
45
return resp.Body, nil
46
} else {
47
f, err := catalog.OpenFile(templatePath)
48
if err != nil {
49
return nil, err
50
}
51
return f, nil
52
}
53
}
54
55
// StringSliceContains checks if a string slice contains a string.
56
func StringSliceContains(slice []string, item string) bool {
57
for _, i := range slice {
58
if strings.EqualFold(i, item) {
59
return true
60
}
61
}
62
return false
63
}
64
65
// MapHash generates a hash for any give map
66
func MapHash[K constraints.Ordered, V any](m map[K]V) uint64 {
67
keys := mapsutil.GetSortedKeys(m)
68
var sb strings.Builder
69
for _, k := range keys {
70
sb.WriteString(fmt.Sprintf("%v:%v\n", k, m[k]))
71
}
72
return xxhash.Sum64([]byte(sb.String()))
73
}
74
75