-
Notifications
You must be signed in to change notification settings - Fork 3
/
fetcher.go
174 lines (143 loc) · 3.85 KB
/
fetcher.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"sync"
"time"
)
type WebURL struct {
Date string
URL string
}
type fetchFunction func(string, bool, string) ([]WebURL, error)
func fetchURLs(domains []string, noSubdomains bool, virusTotalAPIKey string) ([]WebURL, []error) {
fetchFunctions := []fetchFunction{
getWaybackURLs,
getCommonCrawlURLs,
getVirusTotalURLs,
}
var results []WebURL
var errs []error
var wg sync.WaitGroup
resultsChan := make(chan []WebURL, len(domains)*len(fetchFunctions))
errorsChan := make(chan error, len(domains)*len(fetchFunctions))
for _, domain := range domains {
for _, fn := range fetchFunctions {
wg.Add(1)
go func(d string, f fetchFunction) {
defer wg.Done()
resp, err := f(d, noSubdomains, virusTotalAPIKey)
if err != nil {
errorsChan <- fmt.Errorf("error fetching URLs for %s: %v", d, err)
return
}
resultsChan <- resp
}(domain, fn)
}
}
go func() {
wg.Wait()
close(resultsChan)
close(errorsChan)
}()
for resp := range resultsChan {
results = append(results, resp...)
}
for err := range errorsChan {
errs = append(errs, err)
}
return results, errs
}
func getWaybackURLs(domain string, noSubdomains bool, _ string) ([]WebURL, error) {
subsWildcard := "*."
if noSubdomains {
subsWildcard = ""
}
client := &http.Client{Timeout: 30 * time.Second}
res, err := client.Get(
fmt.Sprintf("http://web.archive.org/cdx/search/cdx?url=%s%s/*&output=json&collapse=urlkey", subsWildcard, domain),
)
if err != nil {
return nil, fmt.Errorf("error fetching from Wayback Machine: %v", err)
}
defer res.Body.Close()
raw, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("error reading Wayback Machine response: %v", err)
}
var wrapper [][]string
if err := json.Unmarshal(raw, &wrapper); err != nil {
return nil, fmt.Errorf("error parsing Wayback Machine JSON: %v", err)
}
out := make([]WebURL, 0, len(wrapper))
for i, urls := range wrapper {
if i == 0 {
continue // Skip the header row
}
out = append(out, WebURL{Date: urls[1], URL: urls[2]})
}
return out, nil
}
func getCommonCrawlURLs(domain string, noSubdomains bool, _ string) ([]WebURL, error) {
subsWildcard := "*."
if noSubdomains {
subsWildcard = ""
}
client := &http.Client{Timeout: 30 * time.Second}
res, err := client.Get(
fmt.Sprintf("http://index.commoncrawl.org/CC-MAIN-2018-22-index?url=%s%s/*&output=json", subsWildcard, domain),
)
if err != nil {
return nil, fmt.Errorf("error fetching from Common Crawl: %v", err)
}
defer res.Body.Close()
sc := bufio.NewScanner(res.Body)
var out []WebURL
for sc.Scan() {
wrapper := struct {
URL string `json:"url"`
Timestamp string `json:"timestamp"`
}{}
if err := json.Unmarshal([]byte(sc.Text()), &wrapper); err != nil {
continue
}
out = append(out, WebURL{Date: wrapper.Timestamp, URL: wrapper.URL})
}
if err := sc.Err(); err != nil {
return nil, fmt.Errorf("error reading Common Crawl response: %v", err)
}
return out, nil
}
func getVirusTotalURLs(domain string, noSubdomains bool, apiKey string) ([]WebURL, error) {
if apiKey == "" {
return nil, nil
}
fetchURL := fmt.Sprintf(
"https://www.virustotal.com/vtapi/v2/domain/report?apikey=%s&domain=%s",
apiKey,
domain,
)
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Get(fetchURL)
if err != nil {
return nil, fmt.Errorf("error fetching from VirusTotal: %v", err)
}
defer resp.Body.Close()
wrapper := struct {
URLs []struct {
URL string `json:"url"`
} `json:"detected_urls"`
}{}
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&wrapper); err != nil {
return nil, fmt.Errorf("error parsing VirusTotal JSON: %v", err)
}
out := make([]WebURL, 0, len(wrapper.URLs))
for _, u := range wrapper.URLs {
out = append(out, WebURL{URL: u.URL})
}
return out, nil
}