-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
241 lines (188 loc) · 4.44 KB
/
main.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package main
import (
"bytes"
"flag"
"io"
"net/http"
"net/url"
"sync"
"crypto/tls"
"fmt"
"time"
//"log"
"math"
"github.com/lucas-clemente/quic-go/h2quic"
"github.com/lucas-clemente/quic-go/utils"
"golang.org/x/net/html"
)
import _ "net/http/pprof"
func follow(addr string, buf io.Reader, urls chan string) {
tokenizer := html.NewTokenizer(buf)
base, err := url.Parse(addr)
if err != nil {
utils.Infof("Error: %s", err.Error())
return
}
for {
tokenType := tokenizer.Next()
switch tokenType {
case html.ErrorToken:
return
case html.StartTagToken:
token := tokenizer.Token()
if token.Data == "a" {
for _, attr := range token.Attr {
if attr.Key == "href" {
utils.Infof("href := %s", attr.Val)
ref, err := url.Parse(attr.Val)
if err != nil {
utils.Infof("Error: %s", err.Error())
continue
}
abs := base.ResolveReference(ref)
nurl := abs.String()
utils.Infof("Queueing %s", nurl)
select {
case urls <- nurl:
default:
utils.Infof("TOO MUCH STUFF")
return
}
utils.Infof("Queued %s", nurl)
}
}
}
}
}
}
type stats struct {
max_speed float64
min_speed float64
speed_sum float64
size_sum uint64
n uint32
mutex *sync.Mutex
}
func crawl(url string, urls chan string, stats *stats) {
utils.Infof("Crawling %s", url)
hclient := &http.Client{
Transport: &h2quic.QuicRoundTripper{TLSClientConfig: &tls.Config{InsecureSkipVerify:true}},
}
start := time.Now()
rsp, err := hclient.Get(url)
if err != nil {
utils.Infof("Error %s while crawling %s!", err.Error(), url)
return
}
utils.Infof("Status code: %d", rsp.StatusCode)
body := &bytes.Buffer{}
_, err = io.Copy(body, rsp.Body)
if err != nil {
utils.Infof("Error %s while crawling %s!", err.Error(), url)
return
}
end := time.Now()
elapsed := end.Sub(start).Seconds()
speed := float64(body.Len())/elapsed
stats.mutex.Lock()
if stats.max_speed < speed {
stats.max_speed = speed
}
if stats.min_speed > speed {
stats.min_speed = speed
}
stats.speed_sum += speed
stats.size_sum += uint64(body.Len())
stats.n += 1
stats.mutex.Unlock()
utils.Infof("Speed: %f, elapsed: %f", speed / (1024.0*1024.0), elapsed)
rsp.Body.Close()
if rsp.StatusCode == 200 {
follow(url, body, urls)
}
}
func crawlWorker(urls chan string, stats *stats) {
for {
}
}
func crawlLoop(urls chan string, stats *stats, maxOutstanding int) {
var wg sync.WaitGroup
outstanding := 0
started := false
done := make(chan bool)
for {
utils.Infof("Wait for next url... (%d)", maxOutstanding)
select {
case url, ok := <- urls:
started = true
if ok {
if outstanding >= maxOutstanding {
utils.Infof("Too many open requests.... waiting for slot.")
_ = <- done
outstanding -= 1
}
outstanding += 1
wg.Add(1)
go func() {
crawl(url, urls, stats)
wg.Done()
done <- true
}()
} else {
return
}
default:
if !started {
continue
}
if outstanding == 0 {
utils.Infof("...")
return
} else {
utils.Infof("Waiting for new data...")
_ = <- done
outstanding -= 1
}
}
}
}
func main() {
//go func() {
// log.Println(http.ListenAndServe("localhost:9090", nil))
//}()
verbose := flag.Bool("v", false, "verbose")
maxOutstanding := flag.Int("o", 1, "Max outstanding (concurrent) requests")
flag.Parse()
urls := flag.Args()
if *verbose {
utils.SetLogLevel(utils.LogLevelDebug)
} else {
utils.SetLogLevel(utils.LogLevelInfo)
}
stats := &stats{}
stats.max_speed = 0.0
stats.min_speed = math.MaxFloat64
stats.mutex = &sync.Mutex{}
var wg sync.WaitGroup
wg.Add(len(urls))
for _, addr := range urls {
utils.Infof("GET %s", addr)
ch := make(chan string, 8192)
go func() {
crawlLoop(ch, stats, *maxOutstanding)
wg.Done()
}()
ch <- addr
}
wg.Wait()
fmt.Printf("Done...\n\n")
fmt.Printf("Stats:\n")
fmt.Printf(" Links crawled: %d\n", stats.n)
total_mb := float64(stats.size_sum)/(1024.0*1024.0)
speed_mb := float64(stats.speed_sum)/(1024.0*1024.0)
fmt.Printf(" Total MiB downloaded: %f\n", total_mb)
fmt.Printf(" Average file size (MiB): %f\n", total_mb / float64(stats.n))
fmt.Printf(" Average download speed (MiB/s): %f\n", speed_mb / float64(stats.n))
fmt.Printf(" Max. speed (MiB/s): %.6f\n", stats.max_speed / (1024.0*1024.0))
fmt.Printf(" Min. speed (MiB/s): %.6f\n", stats.min_speed / (1024.0*1024.0))
}