-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathutil.go
More file actions
90 lines (80 loc) · 2.51 KB
/
util.go
File metadata and controls
90 lines (80 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package alterx
import (
"fmt"
"regexp"
"strings"
"unsafe"
"golang.org/x/net/publicsuffix"
)
var varRegex = regexp.MustCompile(`\{\{([a-zA-Z0-9]+)\}\}`)
// returns no of variables present in statement
func getVarCount(data string) int {
return len(varRegex.FindAllStringSubmatch(data, -1))
}
// returns names of all variables
func getAllVars(data string) []string {
var values []string
for _, v := range varRegex.FindAllStringSubmatch(data, -1) {
if len(v) >= 2 {
values = append(values, v[1])
}
}
return values
}
// getSampleMap returns a sample map containing input variables and payload variable
func getSampleMap(inputVars map[string]interface{}, payloadVars map[string][]string) map[string]interface{} {
sMap := map[string]interface{}{}
for k, v := range inputVars {
sMap[k] = v
}
for k, v := range payloadVars {
if k != "" && len(v) > 0 {
sMap[k] = "temp"
}
}
return sMap
}
// checkMissing checks if all variables/placeholders are successfully replaced
// if not error is thrown with description
func checkMissing(template string, data map[string]interface{}) error {
got := Replace(template, data)
if res := varRegex.FindAllString(got, -1); len(res) > 0 {
return fmt.Errorf("values of `%v` variables not found", strings.Join(res, ","))
}
return nil
}
// TODO: add this to utils
// unsafeToBytes converts a string to byte slice and does it with
// zero allocations.
//
// Reference - https://stackoverflow.com/questions/59209493/how-to-use-unsafe-get-a-byte-slice-from-a-string-without-memory-copy
func unsafeToBytes(data string) []byte {
return unsafe.Slice(unsafe.StringData(data), len(data))
}
func getNValidateRootDomain(domains []string) (string, error) {
if len(domains) == 0 {
return "", fmt.Errorf("no domains provided")
}
var rootDomain string
// parse root domain from publicsuffix for first entry
for _, domain := range domains {
if strings.TrimSpace(domain) == "" {
continue
}
if rootDomain == "" {
root, err := publicsuffix.EffectiveTLDPlusOne(domain)
if err != nil || root == "" {
return "", fmt.Errorf("failed to derive root domain from %v: %v", domain, err)
}
rootDomain = root
} else {
if domain != rootDomain && !strings.HasSuffix(domain, "."+rootDomain) {
return "", fmt.Errorf("domain %v does not have the same root domain as %v, only homogeneous domains are supported in discover mode", domain, rootDomain)
}
}
}
if rootDomain == "" {
return "", fmt.Errorf("no valid domains found after filtering empty entries")
}
return rootDomain, nil
}