2021-07-09 16:56:01 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
|
|
import (
|
2021-10-30 13:46:07 +03:00
|
|
|
"errors"
|
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
|
|
|
|
|
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/catalog"
|
2022-12-13 20:35:14 +01:00
|
|
|
"github.com/projectdiscovery/nuclei/v2/pkg/utils/yaml"
|
2023-03-02 14:54:01 +01:00
|
|
|
"github.com/projectdiscovery/retryablehttp-go"
|
2022-12-13 20:35:14 +01:00
|
|
|
fileutil "github.com/projectdiscovery/utils/file"
|
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.
|
2022-08-10 23:35:58 +05:30
|
|
|
func ReadFromPathOrURL(templatePath string, catalog catalog.Catalog) (data []byte, err error) {
|
2022-12-13 20:35:14 +01:00
|
|
|
var reader io.Reader
|
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
|
|
|
|
|
}
|
|
|
|
|
defer resp.Body.Close()
|
2022-12-13 20:35:14 +01:00
|
|
|
reader = resp.Body
|
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
|
|
|
|
|
}
|
|
|
|
|
defer f.Close()
|
2022-12-13 20:35:14 +01:00
|
|
|
reader = f
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data, err = io.ReadAll(reader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pre-process directives only for local files
|
|
|
|
|
if fileutil.FileExists(templatePath) {
|
|
|
|
|
data, err = yaml.PreProcess(data)
|
2022-01-24 16:48:12 +05:30
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-12-13 20:35:14 +01:00
|
|
|
|
2022-01-24 16:48:12 +05:30
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
}
|