-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
455 lines (371 loc) · 10.7 KB
/
main.go
File metadata and controls
455 lines (371 loc) · 10.7 KB
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package main
import (
"bufio"
"errors"
"flag"
"fmt"
"io"
"math"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/PuerkitoBio/goquery"
)
var (
client = http.Client{}
ErrFailToParseHTML = errors.New("could not parse HTML")
)
// tryResume attempts to open dest and issue a Range GET request.
// Returns (nil, nil, nil) for any graceful failure — caller should do a fresh download.
// Returns (f, resp, nil) on success — caller owns both and must close them.
// Returns (nil, nil, err) for hard errors.
func tryResume(rawURL, dest string) (*os.File, *http.Response, error) {
f, err := os.OpenFile(dest, os.O_RDWR, 0666)
if err != nil {
return nil, nil, nil // file doesn't exist yet — start fresh
}
info, err := f.Stat()
if err != nil {
f.Close()
return nil, nil, nil
}
size := info.Size()
if _, err = f.Seek(size, io.SeekStart); err != nil {
f.Close()
return nil, nil, nil
}
req, err := http.NewRequest("GET", rawURL, nil)
if err != nil {
f.Close()
return nil, nil, fmt.Errorf("failed to create GET request: %w", err)
}
req.Header.Set("Range", fmt.Sprintf("bytes=%d-", size))
resp, err := client.Do(req)
if err != nil {
f.Close()
return nil, nil, fmt.Errorf("failed to do range GET request: %w", err)
}
switch resp.StatusCode {
case http.StatusPartialContent:
// happy path — append from where we left off
case http.StatusOK:
// server ignored Range header — reset and overwrite
if _, err = f.Seek(0, io.SeekStart); err != nil {
resp.Body.Close()
f.Close()
return nil, nil, fmt.Errorf("failed to seek to start of file: %w", err)
}
if err = f.Truncate(0); err != nil {
resp.Body.Close()
f.Close()
return nil, nil, fmt.Errorf("failed to truncate file: %w", err)
}
default:
// unexpected status — fall back to fresh download
resp.Body.Close()
f.Close()
return nil, nil, nil
}
return f, resp, nil
}
// fetch downloads url to dest, optionally resuming a partial download.
// If the content is HTML it scrapes and returns all href/img-src links.
// There are no retries.
func fetch(rawURL, dest string, resume bool) ([]string, error) {
var f *os.File
var resp *http.Response
if resume {
var err error
f, resp, err = tryResume(rawURL, dest)
if err != nil {
return nil, err
}
}
if f == nil { // resume wasn't attempted or fell back gracefully
var err error
resp, err = client.Get(rawURL)
if err != nil {
return nil, fmt.Errorf("failed to fetch URL: %w", err)
}
if resp.StatusCode != http.StatusOK {
resp.Body.Close()
return nil, fmt.Errorf("got bad http status %s", resp.Status)
}
destDir := filepath.Dir(dest)
if err = os.MkdirAll(destDir, 0755); err != nil {
resp.Body.Close()
return nil, fmt.Errorf("could not create destination directory %s: %v", destDir, err)
}
f, err = os.Create(dest)
if err != nil {
resp.Body.Close()
return nil, fmt.Errorf("could not create file %s: %v", dest, err)
}
}
defer resp.Body.Close()
defer f.Close()
if _, err := io.Copy(f, resp.Body); err != nil {
return nil, fmt.Errorf("error doing io copy: %w", err)
}
contentType := strings.ToLower(resp.Header.Get("Content-Type"))
if !strings.HasPrefix(contentType, "text/html") {
return nil, nil
}
if _, err := f.Seek(0, io.SeekStart); err != nil {
return nil, fmt.Errorf("could not reread file for parsing links: %w", err)
}
doc, err := goquery.NewDocumentFromReader(bufio.NewReader(f))
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrFailToParseHTML, err)
}
var urls []string
doc.Find("a[href]").Each(func(_ int, item *goquery.Selection) {
href, _ := item.Attr("href")
if !strings.HasPrefix(href, "mailto:") {
urls = append(urls, href)
}
})
doc.Find("img[src]").Each(func(_ int, item *goquery.Selection) {
src, _ := item.Attr("src")
urls = append(urls, src)
})
return urls, nil
}
func urlToPath(u string) (string, error) {
u2, err := url.Parse(u)
if err != nil {
return "", err
}
path := u2.Path
// detect if we're downloading from a root or a directory
// and if so, save contents as index.html
if len(path) == 0 || path[len(path)-1] == '/' {
path = filepath.Join(path, "index.html")
}
// rebase against a faux root directory to remove any relative paths
root := url.URL{Path: "/"}
canonical, err := root.Parse(path)
if err != nil {
fmt.Fprintf(os.Stderr, "could not canonicalise: %v\n", err)
return "", err
}
// we will treat query parameters as potential new files
// that can be fetched from the filesystem
if u2.RawQuery != "" {
return canonical.Path + "?" + u2.RawQuery, nil
}
return canonical.Path, nil
}
// listFlags is an implementation of the flag.Value interface
type listFlags []string
func (l *listFlags) String() string {
return fmt.Sprintf("%v", *l)
}
func (l *listFlags) Set(value string) error {
*l = append(*l, value)
return nil
}
func main() {
var resume bool
var depth uint
var includes listFlags
var excludes listFlags
var refresh listFlags
flag.BoolVar(&resume, "resume", false, "resume previously downloaded files")
flag.UintVar(&depth, "depth", math.MaxUint, "depth for recursion")
flag.Var(&includes, "include", `regex(es) of URLs limiting what to include when downloading, e.g. -include 'blog.cr.yp.to/(.*html|.*jpg)$' [default: ".*"]`)
flag.Var(&excludes, "exclude", "regex(es) of URLs of what not to include when downloading, e.g. -exclude 'blog.cr.yp.to/.*js$'")
flag.Var(&refresh, "refresh", "regex(es) of URLs of what should always be redownloaded, e.g. -refresh '\\.md5$'")
flag.Parse()
args := flag.Args()
if len(args) < 1 {
fmt.Fprintln(os.Stderr, "./mrdriller [-resume] [-depth #] [-include regex1 -include regex2 ...] [-exclude regex1 -exclude regex2 ...] [-refresh regex1 -refresh regex2 ...] URL")
os.Exit(1)
}
if len(includes) == 0 {
includes = []string{".*"}
}
includeRE := []*regexp.Regexp{}
excludeRE := []*regexp.Regexp{}
refreshRE := []*regexp.Regexp{}
for _, r := range includes {
c, err := regexp.Compile(r)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to compile regexp `%s`: %v", r, err)
os.Exit(1)
}
includeRE = append(includeRE, c)
}
for _, r := range excludes {
c, err := regexp.Compile(r)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to compile regexp `%s`: %v", r, err)
os.Exit(1)
}
excludeRE = append(excludeRE, c)
}
for _, r := range refresh {
c, err := regexp.Compile(r)
if err != nil {
fmt.Fprintf(os.Stderr, "failed to compile regexp `%s`: %v", r, err)
os.Exit(1)
}
refreshRE = append(refreshRE, c)
}
fmt.Printf("Depth is: %d\n", depth)
fmt.Printf("Includes is: %#v\n", includes)
fmt.Printf("Excludes is: %#v\n", excludes)
fmt.Printf("Refresh is: %#v\n", refresh)
u, err := url.Parse(args[0])
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing URL %s: %v", args[0], err)
os.Exit(1)
}
if !strings.HasPrefix(u.Scheme, "http") {
fmt.Fprintln(os.Stderr, "URL must be http or https")
os.Exit(1)
}
dir, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "unable to get working directory: %#v\n", err)
os.Exit(1)
}
type Item struct {
url string
depth uint
}
queue := []Item{{args[0], 0}}
host := strings.ToLower(u.Host)
scheme := u.Scheme
seen := map[string]struct{}{}
for len(queue) > 0 {
i := queue[0]
queue = queue[1:]
if i.depth > depth {
fmt.Printf("skipping %s exceeds depth limit\n", i.url)
continue
}
if _, ok := seen[i.url]; ok {
continue
}
// First we check excludes for any match to see if we shouldn't
// be downloading this URL, skip if we shouldn't.
// Then we check includes to see if any match, and if it does
// then we download the file, otherwise skip.
matched := false
for _, re := range excludeRE {
if re.MatchString(i.url) {
matched = true
break
}
}
if matched {
seen[i.url] = struct{}{}
continue
}
matched = false
for _, re := range includeRE {
if re.MatchString(i.url) {
matched = true
break
}
}
if !matched {
seen[i.url] = struct{}{}
continue
}
path, err := urlToPath(i.url)
if err != nil {
fmt.Fprintf(os.Stderr, "warning, could not convert url %s to local path: %v\n", i.url, err)
continue
}
// directories are laid out as "https:my.web.site:80"
// port is omitted if omitted in input URL
// (no credentials are stored in the name)
path = filepath.Join(dir, u.Scheme+":"+strings.ToLower(u.Host), path)
var info os.FileInfo
shouldResume := resume
skipStat := false
for _, re := range refreshRE {
if re.MatchString(i.url) {
shouldResume = false
skipStat = true
break
}
}
if !skipStat {
info, err = os.Stat(path)
if err == nil {
localSize := info.Size()
resp, err := client.Head(i.url)
if err != nil {
fmt.Fprintf(os.Stderr, "warning, could not HEAD url %s: %v", i.url, err)
continue
}
resp.Body.Close()
lengthStr := resp.Header.Get("Content-Length")
if lengthStr != "" {
l, err := strconv.Atoi(lengthStr)
if err != nil {
fmt.Fprintf(os.Stderr, "warning, content-length string is not an integer (got %s), force downloading", lengthStr)
shouldResume = false
} else if int64(l) == localSize {
// file on filesystem same size as remote,
// then assume we've already fetched correctly
continue
}
}
}
}
hrefs, err := fetch(i.url, path, shouldResume)
if err != nil {
fmt.Fprintf(os.Stderr, "warning, couldn't process URL %s: %v\n", i.url, err)
continue
}
for _, link := range hrefs {
u, err := url.Parse(link)
if err != nil {
fmt.Fprintf(os.Stderr, "(skipping) could not parse URL %s\n", link)
continue
}
if u.Host != "" && strings.ToLower(u.Host) != host {
continue
}
if u.Host == "" {
u.Host = host
u.Scheme = scheme
// Here's where it gets tricky, we need to join i.url
// with the relative path given by u.Path, for example:
// https://foo, bar.html -> https://foo/bar.html
// https://foo/index.html, bar.html -> https://foo/bar.html
// https://foo/a/index.html, bar.html -> https://foo/a/bar.html
// etc.
if u.Path != "" && u.Path[0] != '/' {
base, err := url.Parse(i.url)
if err != nil {
fmt.Fprintf(os.Stderr, "(skipping) could not parse base URL %s [%s]\n", i.url, link)
continue
}
base, err = base.Parse(u.Path)
if err != nil {
fmt.Fprintf(os.Stderr, "(skipping) failed to rebase URL %s [%s]\n", i.url, link)
}
u.Path = base.Path
}
}
// we want to collapse all urls with a '#' in it
u.Fragment = ""
u.RawFragment = ""
link = u.String()
if _, ok := seen[link]; !ok {
queue = append(queue, Item{link, i.depth + 1})
}
}
seen[i.url] = struct{}{}
fmt.Fprintf(os.Stderr, "Got %s -> %s\n", i.url, path)
}
}