-
Notifications
You must be signed in to change notification settings - Fork 13
/
page.go
192 lines (171 loc) · 4.2 KB
/
page.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
package core
import (
"crypto/sha1"
"fmt"
"io"
"net"
"net/url"
"strings"
"sync"
"github.com/google/uuid"
)
type Header struct {
Name string `json:"name"`
Value string `json:"value"`
DecreasesSecurity bool `json:"decreasesSecurity"`
IncreasesSecurity bool `json:"increasesSecurity"`
}
func (h *Header) SetSecurityFlags() {
if h.decreasesSecurity() {
h.DecreasesSecurity = true
h.IncreasesSecurity = false
} else if h.increasesSecurity() {
h.DecreasesSecurity = false
h.IncreasesSecurity = true
} else {
h.DecreasesSecurity = false
h.IncreasesSecurity = false
}
}
func (h Header) decreasesSecurity() bool {
switch strings.ToLower(h.Name) {
case "server", "wpe-Backend", "x-powered-by", "x-cf-powered-by", "x-pingback", "via":
return true
case "access-control-allow-origin":
if h.Value == "*" {
return true
}
case "x-xss-protection":
if !strings.HasPrefix(h.Value, "1") {
return true
}
case "cache-control":
if !strings.Contains(h.Value, "max-age=0") && !strings.Contains(h.Value, "private") && !strings.Contains(h.Value, "no-store") {
return true
}
}
return false
}
func (h Header) increasesSecurity() bool {
switch strings.ToLower(h.Name) {
case "content-security-policy", "content-security-policy-report-only":
return true
case "strict-transport-security":
return true
case "referrer-policy":
return true
case "public-key-pins":
return true
case "x-frame-options":
return true
case "x-download-options":
return true
case "x-permitted-cross-domain-policies":
if strings.ToLower(h.Value) == "master-only" {
return true
}
case "x-content-type-options":
if strings.ToLower(h.Value) == "nosniff" {
return true
}
case "x-xss-protection":
if strings.HasPrefix(h.Value, "1") {
return true
}
case "cache-control":
if strings.Contains(h.Value, "max-age=0") || strings.Contains(h.Value, "no-store") {
return true
}
}
return false
}
type Tag struct {
Text string `json:"text"`
Type string `json:"type"`
Link string `json:"link"`
Hash string `json:"hash"`
}
func (t Tag) HasLink() bool {
return t.Link != ""
}
type Note struct {
Text string `json:"text"`
Type string `json:"type"`
}
type Page struct {
sync.Mutex
UUID string `json:"uuid"`
URL string `json:"url"`
Hostname string `json:"hostname"`
Addrs []string `json:"addrs"`
Status string `json:"status"`
PageTitle string `json:"pageTitle"`
PageStructure []string `json:"-"`
HeadersPath string `json:"headersPath"`
BodyPath string `json:"bodyPath"`
ScreenshotPath string `json:"screenshotPath"`
HasScreenshot bool `json:"hasScreenshot"`
Headers []Header `json:"headers"`
Tags []Tag `json:"tags"`
Notes []Note `json:"notes"`
}
func (p *Page) AddHeader(name string, value string) {
p.Lock()
defer p.Unlock()
header := Header{
Name: name,
Value: value,
}
header.SetSecurityFlags()
p.Headers = append(p.Headers, header)
}
func (p *Page) AddTag(text string, tagType string, link string) {
p.Lock()
defer p.Unlock()
h := sha1.New()
io.WriteString(h, text)
io.WriteString(h, tagType)
io.WriteString(h, link)
p.Tags = append(p.Tags, Tag{
Text: text,
Type: tagType,
Link: link,
Hash: fmt.Sprintf("%x", h.Sum(nil)),
})
}
func (p *Page) AddNote(text string, noteType string) {
p.Lock()
defer p.Unlock()
p.Notes = append(p.Notes, Note{
Text: text,
Type: noteType,
})
}
func (p *Page) BaseFilename() string {
u := p.ParsedURL()
h := sha1.New()
io.WriteString(h, u.Path)
io.WriteString(h, u.Fragment)
pathHash := fmt.Sprintf("%x", h.Sum(nil))[0:16]
host := strings.Replace(u.Host, ":", "__", 1)
filename := fmt.Sprintf("%s__%s__%s", u.Scheme, strings.Replace(host, ".", "_", -1), pathHash)
return strings.ToLower(filename)
}
func (p *Page) ParsedURL() *url.URL {
parsedURL, _ := url.Parse(p.URL)
return parsedURL
}
func (p *Page) IsIPHost() bool {
return net.ParseIP(p.ParsedURL().Hostname()) != nil
}
func NewPage(pageURL string) (*Page, error) {
u, err := url.Parse(pageURL)
if err != nil {
return nil, err
}
return &Page{
UUID: uuid.New().String(),
URL: pageURL,
Hostname: u.Hostname(),
}, nil
}