-
Notifications
You must be signed in to change notification settings - Fork 0
/
urls.go
136 lines (126 loc) · 3.2 KB
/
urls.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
package squidr
import (
"bytes"
"errors"
"net/url"
"sort"
"strconv"
"strings"
"time"
"mvdan.cc/xurls/v2"
)
func reportURL(targetURL string) error {
targetURL = _https + targetURL
var buf bytes.Buffer
reportURLHead(targetURL, &buf)
reportURLBody(targetURL, &buf)
reportURLFooter(&buf)
return writeReport(buf.Bytes(), _servURL)
}
func reportURLHead(targetURL string, buf *bytes.Buffer) {
ts := time.Now()
buf.WriteString(_head + "[url]</title><pre>\n")
buf.WriteString("[ -= SQUIDR URL REPORT =- ]\n")
buf.WriteString("Report Timestamp: " + strconv.FormatInt(ts.UnixNano(), 10) + _linefeed)
buf.WriteString("Report Created : " + ts.Format(time.RFC3339) + _linefeed)
buf.WriteString("Target URL : " + targetURL + _linefeed)
}
func reportURLFooter(buf *bytes.Buffer) {
buf.WriteString("</pre></html>\n")
}
func reportURLBody(targetURL string, buf *bytes.Buffer) {
body, err := getUrlsFromPageViaProxy(targetURL)
if err != nil {
buf.WriteString("[error] [unable to fetch]: " + targetURL + _sep + err.Error())
return
}
buf.WriteString("Target PageSize : " + strconv.Itoa(len(body)) + " bytes" + _linefeed + _linefeed)
buf.WriteString(urls(string(body), false, false))
}
func getUrlsFromPageViaProxy(targetURL string) ([]byte, error) {
// sanitize url
_, err := url.Parse(targetURL)
if err != nil {
return nil, errors.New("invalid url, unable to parse url: " + err.Error())
}
// setup
client := getClient(getTransportProxy(_proxyURL, getTlsConf(proxyTrust)))
request, err := getRequest(targetURL, _userAgent)
if err != nil {
return nil, errors.New("unable to fetch via proxy: " + err.Error())
}
// get
request.Method = "GET"
client.Timeout = time.Duration(5 * time.Second)
resp, err := client.Do(request)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New("invalid response: ")
}
return decodeResponse(resp)
}
func urls(in string, head, css bool) string {
var s strings.Builder
parse := xurls.Strict()
array := parse.FindAllString(in, -1)
uMap := make(map[string]bool)
for _, v := range array {
uMap[v] = true
}
uniq := make([]string, len(uMap))
for k := range uMap {
uniq = append(uniq, k)
}
sort.Strings(uniq)
if len(uniq) > 0 {
switch head {
case true:
s.WriteString("\n<br>\n[url list]")
default:
}
switch css {
case true:
s.WriteString("\n\t<ol>")
default:
}
for _, e := range uniq {
if strings.Contains(e, "w3.org") || strings.Contains(e, "W3.org") || strings.Contains(e, "schema.org") {
continue
}
if len(e) < 4 {
continue
}
if e[:4] != "http" {
e = "https://" + e
}
if len(e) < 5 {
continue
}
if e[:5] == "http:" {
e = "https:" + e[5:]
}
switch css {
case true:
s.WriteString("\n\t\t<li><a style=\"color:#AAA;text-decoration:none;\"} href=\"")
s.WriteString(e)
s.WriteString("\" target=\"_blank\" rel=\"noreferrer\">")
s.WriteString(e)
s.WriteString("</a></li><br>")
case false:
s.WriteString("<a href=\"")
s.WriteString(e)
s.WriteString("\" target=\"_blank\" rel=\"noreferrer\">")
s.WriteString(e)
s.WriteString("</a><br>")
}
}
switch css {
case true:
s.WriteString("\n\t</ol>")
default:
}
}
return s.String()
}