-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
196 lines (164 loc) · 4.06 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
package main
import (
"bufio"
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"sync"
"github.com/schollz/progressbar/v3"
)
func main() {
versionsLimit := flag.Int("limit", 50, "limit the number crawled snapshots. Use -1 for unlimited")
recent := flag.Bool("recent", false, "use the most recent snapshots without evenly distributing them")
flag.Parse()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
url, err := cleanURL(scanner.Text())
if err != nil {
continue
}
versions, err := GetRobotsTxtVersions(url, *versionsLimit, *recent)
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting versions: %v\n", err)
os.Exit(1)
}
numThreads := 10
jobCh := make(chan string, numThreads)
pathCh := make(chan []string)
progressbarMessage := fmt.Sprintf("Enumerating %s/robots.txt versions...", url)
bar := progressbar.Default(int64(len(versions)), progressbarMessage)
var wg sync.WaitGroup
wg.Add(numThreads)
for i := 0; i < numThreads; i++ {
go func() {
defer wg.Done()
for version := range jobCh {
GetRobotsTxtPaths(version, url, pathCh, bar)
}
}()
}
go func() {
for _, version := range versions {
jobCh <- version
}
close(jobCh)
}()
go func() {
wg.Wait()
close(pathCh)
}()
allPaths := make(map[string]bool)
for pathsBatch := range pathCh {
for _, path := range pathsBatch {
allPaths[path] = true
}
}
for path := range allPaths {
fmt.Println(path)
}
}
if err := scanner.Err(); err != nil {
fmt.Fprintf(os.Stderr, "Error reading URLs from stdin: %v\n", err)
os.Exit(1)
}
}
func GetRobotsTxtVersions(url string, limit int, recent bool) ([]string, error) {
requestURL := fmt.Sprintf("https://web.archive.org/cdx/search/cdx?url=%s/robots.txt&output=json&fl=timestamp&filter=statuscode:200&collapse=digest", url)
if limit != -1 && recent {
requestURL += "&limit=-" + strconv.Itoa(limit)
}
res, err := http.Get(requestURL)
if err != nil {
return nil, err
}
raw, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
return nil, err
}
var versions [][]string
err = json.Unmarshal(raw, &versions)
if err != nil {
return nil, err
}
if len(versions) == 0 {
return []string{}, nil
}
versions = versions[1:]
selectedVersions := make([]string, 0)
length := len(versions)
if recent || limit == -1 || length <= limit {
for _, version := range versions {
selectedVersions = append(selectedVersions, version...)
}
} else {
interval := length / (limit - 1)
for i := 0; i < limit; i++ {
index := i * interval
if index >= length {
index = length - (limit - i)
}
selectedVersions = append(selectedVersions, versions[index]...)
}
}
return selectedVersions, nil
}
func GetRobotsTxtPaths(version string, url string, pathCh chan []string, bar *progressbar.ProgressBar) {
requestURL := fmt.Sprintf("https://web.archive.org/web/%sif_/%s/robots.txt", version, url)
res, err := http.Get(requestURL)
bar.Add(1)
if err != nil || res.StatusCode != 200 {
return
}
outputURLs := make([]string, 0)
scanner := bufio.NewScanner(res.Body)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if strings.HasPrefix(line, "Disallow:") || strings.HasPrefix(line, "Allow:") {
fields := strings.Fields(line)
if len(fields) < 2 {
continue
}
path := strings.TrimSpace(fields[1])
if path != "" {
fullURL, err := mergeURLPath(url, path)
if err != nil {
continue
}
outputURLs = append(outputURLs, fullURL)
}
}
}
if err := scanner.Err(); err != nil {
return
}
pathCh <- outputURLs
}
func mergeURLPath(baseURL, path string) (string, error) {
host, err := cleanURL(baseURL)
if err != nil {
return "", err
}
if !strings.HasPrefix(path, "/") {
path = "/" + path
}
url := fmt.Sprintf(host + path)
return url, nil
}
func cleanURL(baseURL string) (string, error) {
u, err := url.Parse(baseURL)
if err != nil {
return "", err
}
if u.Scheme == "" {
u.Scheme = "https"
u.Host = baseURL
}
return fmt.Sprintf("%s://%s", u.Scheme, u.Host), nil
}