forked from raviqqe/muffet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecker.go
133 lines (101 loc) · 2.3 KB
/
checker.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
package main
import (
"crypto/tls"
"errors"
"sync"
"github.com/fatih/color"
"github.com/valyala/fasthttp"
)
type checker struct {
fetcher
daemons daemons
urlInspector urlInspector
results chan pageResult
donePages concurrentStringSet
}
func newChecker(s string, o checkerOptions) (checker, error) {
o.Initialize()
c := &fasthttp.Client{
MaxConnsPerHost: o.Concurrency,
TLSConfig: &tls.Config{
InsecureSkipVerify: o.SkipTLSVerification,
},
}
f := newFetcher(c, o.fetcherOptions)
r, err := f.Fetch(s)
if err != nil {
return checker{}, err
}
p, ok := r.Page()
if !ok {
return checker{}, errors.New("non-HTML page")
}
ui, err := newURLInspector(c, p.URL().String(), o.FollowRobotsTxt, o.FollowSitemapXML)
if err != nil {
return checker{}, err
}
ch := checker{
f,
newDaemons(o.Concurrency),
ui,
make(chan pageResult, o.Concurrency),
newConcurrentStringSet(),
}
ch.addPage(p)
return ch, nil
}
func (c checker) Results() <-chan pageResult {
return c.results
}
func (c checker) Check() {
c.daemons.Run()
close(c.results)
}
func (c checker) checkPage(p *page) {
us := p.Links()
sc := make(chan string, len(us))
ec := make(chan string, len(us))
w := sync.WaitGroup{}
for u, err := range us {
if err != nil {
ec <- formatLinkError(u, err)
continue
}
w.Add(1)
go func(u string) {
defer w.Done()
r, err := c.fetcher.Fetch(u)
if err == nil {
sc <- formatLinkSuccess(u, r.StatusCode())
} else {
ec <- formatLinkError(u, err)
}
// only consider adding the page to the list if we're recursing
if !c.fetcher.options.OnePageOnly {
if p, ok := r.Page(); ok && c.urlInspector.Inspect(p.URL()) {
c.addPage(p)
}
}
}(u)
}
w.Wait()
c.results <- newPageResult(p.URL().String(), stringChannelToSlice(sc), stringChannelToSlice(ec))
}
func (c checker) addPage(p *page) {
if !c.donePages.Add(p.URL().String()) {
c.daemons.Add(func() { c.checkPage(p) })
}
}
func stringChannelToSlice(sc <-chan string) []string {
ss := make([]string, 0, len(sc))
for i := 0; i < cap(ss); i++ {
ss = append(ss, <-sc)
}
return ss
}
func formatLinkSuccess(u string, s int) string {
return color.GreenString("%v", s) + "\t" + u
}
func formatLinkError(u string, err error) string {
return color.RedString(err.Error()) + "\t" + u
}