forked from jaeles-project/gospider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
212 lines (189 loc) · 4.78 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package core
import (
"bufio"
"fmt"
"net/http"
"net/url"
"os"
"path"
"regexp"
"strings"
"github.com/mitchellh/go-homedir"
"golang.org/x/net/publicsuffix"
)
var nameStripRE = regexp.MustCompile("(?i)^((20)|(25)|(2b)|(2f)|(3d)|(3a)|(40))+")
func GetRawCookie(cookies []*http.Cookie) string {
var rawCookies []string
for _, c := range cookies {
e := fmt.Sprintf("%s=%s", c.Name, c.Value)
rawCookies = append(rawCookies, e)
}
return strings.Join(rawCookies, "; ")
}
func GetDomain(site *url.URL) string {
domain, err := publicsuffix.EffectiveTLDPlusOne(site.Hostname())
if err != nil {
return ""
}
return domain
}
// func FixUrl(site *url.URL, nextLoc string) string {
// var newUrl string
// if strings.HasPrefix(nextLoc, "//") {
// // //google.com/example.php
// newUrl = site.Scheme + ":" + nextLoc
//
// } else if strings.HasPrefix(nextLoc, "http") {
// // http://google.com || https://google.com
// newUrl = nextLoc
//
// } else if !strings.HasPrefix(nextLoc, "//") {
// // if strings.HasPrefix(nextLoc, "/") {
// // // Ex: /?thread=10
// // newUrl = site.Scheme + "://" + site.Host + nextLoc
// //
// // } else {
// // if strings.HasPrefix(nextLoc, ".") {
// // if strings.HasPrefix(nextLoc, "..") {
// // newUrl = site.Scheme + "://" + site.Host + nextLoc[2:]
// // } else {
// // newUrl = site.Scheme + "://" + site.Host + nextLoc[1:]
// // }
// // } else {
// // // "console/test.php"
// // newUrl = site.Scheme + "://" + site.Host + "/" + nextLoc
// // }
// // }
// nextLocUrl, err := url.Parse(nextLoc)
// if err != nil {
// return ""
// }
// newUrl = site.ResolveReference(nextLocUrl).String()
// }
// return newUrl
// }
func FixUrl(mainSite *url.URL, nextLoc string) string {
nextLocUrl, err := url.Parse(nextLoc)
if err != nil {
return ""
}
return mainSite.ResolveReference(nextLocUrl).String()
}
func Unique(intSlice []string) []string {
keys := make(map[string]bool)
var list []string
for _, entry := range intSlice {
if _, value := keys[entry]; !value {
keys[entry] = true
list = append(list, entry)
}
}
return list
}
func LoadCookies(rawCookie string) []*http.Cookie {
httpCookies := []*http.Cookie{}
cookies := strings.Split(rawCookie, ";")
for _, cookie := range cookies {
cookieArgs := strings.SplitN(cookie, "=", 2)
if len(cookieArgs) > 2 {
continue
}
ck := &http.Cookie{Name: strings.TrimSpace(cookieArgs[0]), Value: strings.TrimSpace(cookieArgs[1])}
httpCookies = append(httpCookies, ck)
}
return httpCookies
}
func GetExtType(rawUrl string) string {
u, err := url.Parse(rawUrl)
if err != nil {
return ""
}
return path.Ext(u.Path)
}
func CleanSubdomain(s string) string {
s = strings.TrimSpace(strings.ToLower(s))
s = strings.TrimPrefix(s, "*.")
// s = strings.Trim("u00","")
s = cleanName(s)
return s
}
// Clean up the names scraped from the web.
// Get from Amass
func cleanName(name string) string {
for {
if i := nameStripRE.FindStringIndex(name); i != nil {
name = name[i[1]:]
} else {
break
}
}
name = strings.Trim(name, "-")
// Remove dots at the beginning of names
if len(name) > 1 && name[0] == '.' {
name = name[1:]
}
return name
}
func FilterNewLines(s string) string {
return regexp.MustCompile(`[\t\r\n]+`).ReplaceAllString(strings.TrimSpace(s), " ")
}
func DecodeChars(s string) string {
source, err := url.QueryUnescape(s)
if err == nil {
s = source
}
// In case json encoded chars
replacer := strings.NewReplacer(
`\u002f`, "/",
`\u0026`, "&",
)
s = replacer.Replace(s)
return s
}
func InScope(u *url.URL, regexps []*regexp.Regexp) bool {
for _, r := range regexps {
if r.MatchString(u.String()) {
return true
}
}
return false
}
// NormalizePath the path
func NormalizePath(path string) string {
if strings.HasPrefix(path, "~") {
path, _ = homedir.Expand(path)
}
return path
}
// ReadingLines Reading file and return content as []string
func ReadingLines(filename string) []string {
var result []string
if strings.HasPrefix(filename, "~") {
filename, _ = homedir.Expand(filename)
}
file, err := os.Open(filename)
if err != nil {
return result
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
val := strings.TrimSpace(scanner.Text())
if val == "" {
continue
}
result = append(result, val)
}
if err := scanner.Err(); err != nil {
return result
}
return result
}
func contains(i []int,j int) bool {
for _, value := range i {
if value == j {
return true
}
}
return false
}