-
Notifications
You must be signed in to change notification settings - Fork 4
/
utils.go
100 lines (80 loc) · 2.06 KB
/
utils.go
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
91
92
93
94
95
96
97
98
99
100
package main
import (
"errors"
"fmt"
"strings"
"github.com/PuerkitoBio/goquery"
)
func isUrl(targetPath string) bool {
return strings.Contains(targetPath, "http:") || strings.Contains(targetPath, "https:")
}
func findHref(document, targetStr, finalUrl string) ([]string, error) {
doc, err := goquery.NewDocumentFromReader(strings.NewReader(document))
if err != nil {
return strings.Fields(""), err
} else {
queryList := getQueryList(targetStr)
debug("Queries: %v", queryList)
foundUrls := make([]string, 0, len(queryList))
for _, query := range queryList {
href, attrOk := doc.Find(query).Attr("href")
debug("Get href from query '%s': %v", query, attrOk)
if attrOk {
foundUrls = append(foundUrls, href)
}
}
if len(foundUrls) > 0 {
return foundUrls, nil
} else {
return strings.Fields(finalUrl), errors.New(fmt.Sprintf("No link is found in targets: %s", queryList))
}
}
}
// Returns queries of found targets
func getQueryList(s string) []string {
split := strings.Split(s, ",")
m := getNameToIdTargets(availableTargets)
found := make([]string, 0, len(split))
for _, val := range split {
key := strings.TrimSpace(val)
query, mapOk := m[key]
if mapOk {
debug("Query for '%s' is found: %s", key, query)
found = append(found, query)
}
}
return found
}
// Create a nice select list
func genSelectText(inputMode bool) string {
text := ""
if inputMode {
text += "Available targets:\n\n"
} else {
text += "Select a target:\n\n"
}
text += " > [i]mgops\n"
for _, target := range availableTargets {
text += " > " + strings.Replace(target.Name, string(target.Key), "["+string(target.Key)+"]", 1) + "\n"
}
if inputMode {
text += "\nType initials: "
} else {
text += "\n(Press ESC to cancel)"
}
return text
}
// Initials to targets with comma
func initialsToTargets(initials string) string {
targets := ""
m := getKeyToNameTargets(availableTargets)
for i := range initials {
if val, ok := m[rune(initials[i])]; ok {
targets += val
if len(initials)-1 != i {
targets += ","
}
}
}
return targets
}