2021-07-09 16:56:01 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2021-10-30 13:46:07 +03:00
|
|
|
"errors"
|
2024-06-27 07:44:43 +00:00
|
|
|
"fmt"
|
2022-02-23 13:54:46 +01:00
|
|
|
"io"
|
2022-01-12 18:33:17 +05:30
|
|
|
"net/url"
|
2021-07-09 16:56:01 +03:00
|
|
|
"strings"
|
2022-08-10 23:35:58 +05:30
|
|
|
|
2024-06-27 07:44:43 +00:00
|
|
|
"github.com/cespare/xxhash"
|
2023-10-17 17:44:13 +05:30
|
|
|
"github.com/projectdiscovery/nuclei/v3/pkg/catalog"
|
2023-03-02 14:54:01 +01:00
|
|
|
"github.com/projectdiscovery/retryablehttp-go"
|
2024-06-27 07:44:43 +00:00
|
|
|
mapsutil "github.com/projectdiscovery/utils/maps"
|
|
|
|
|
"golang.org/x/exp/constraints"
|
2021-07-09 16:56:01 +03:00
|
|
|
)
|
|
|
|
|
|
2021-08-03 14:51:34 +03:00
|
|
|
func IsBlank(value string) bool {
|
|
|
|
|
return strings.TrimSpace(value) == ""
|
2021-07-12 17:20:01 +03:00
|
|
|
}
|
|
|
|
|
|
2021-10-30 13:46:07 +03:00
|
|
|
func UnwrapError(err error) error {
|
|
|
|
|
for { // get the last wrapped error
|
|
|
|
|
unwrapped := errors.Unwrap(err)
|
|
|
|
|
if unwrapped == nil {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
err = unwrapped
|
|
|
|
|
}
|
|
|
|
|
return err
|
|
|
|
|
}
|
2022-01-07 13:00:20 +01:00
|
|
|
|
2022-01-12 18:33:17 +05:30
|
|
|
// IsURL tests a string to determine if it is a well-structured url or not.
|
|
|
|
|
func IsURL(input string) bool {
|
|
|
|
|
u, err := url.Parse(input)
|
2022-12-13 20:35:14 +01:00
|
|
|
return err == nil && u.Scheme != "" && u.Host != ""
|
2022-01-12 18:33:17 +05:30
|
|
|
}
|
2022-01-24 16:48:12 +05:30
|
|
|
|
|
|
|
|
// ReadFromPathOrURL reads and returns the contents of a file or url.
|
2024-03-13 21:02:36 +01:00
|
|
|
func ReaderFromPathOrURL(templatePath string, catalog catalog.Catalog) (io.ReadCloser, error) {
|
2022-01-24 16:48:12 +05:30
|
|
|
if IsURL(templatePath) {
|
2023-03-02 14:54:01 +01:00
|
|
|
resp, err := retryablehttp.DefaultClient().Get(templatePath)
|
2022-01-24 16:48:12 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-03-13 21:02:36 +01:00
|
|
|
return resp.Body, nil
|
2022-01-24 16:48:12 +05:30
|
|
|
} else {
|
2022-08-10 23:35:58 +05:30
|
|
|
f, err := catalog.OpenFile(templatePath)
|
2022-01-24 16:48:12 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2024-03-13 21:02:36 +01:00
|
|
|
return f, nil
|
2022-12-13 20:35:14 +01:00
|
|
|
}
|
2022-01-24 16:48:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// StringSliceContains checks if a string slice contains a string.
|
|
|
|
|
func StringSliceContains(slice []string, item string) bool {
|
|
|
|
|
for _, i := range slice {
|
|
|
|
|
if strings.EqualFold(i, item) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false
|
|
|
|
|
}
|
2024-06-27 07:44:43 +00:00
|
|
|
|
|
|
|
|
// MapHash generates a hash for any give map
|
|
|
|
|
func MapHash[K constraints.Ordered, V any](m map[K]V) uint64 {
|
|
|
|
|
keys := mapsutil.GetSortedKeys(m)
|
|
|
|
|
var sb strings.Builder
|
|
|
|
|
for _, k := range keys {
|
|
|
|
|
sb.WriteString(fmt.Sprintf("%v:%v\n", k, m[k]))
|
|
|
|
|
}
|
|
|
|
|
return xxhash.Sum64([]byte(sb.String()))
|
|
|
|
|
}
|