-
Notifications
You must be signed in to change notification settings - Fork 3
/
utils.go
75 lines (66 loc) · 1.5 KB
/
utils.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"net/http"
"net/url"
"os"
"strings"
"time"
)
func readDomainsFromFile(filename string) ([]string, error) {
file, err := os.Open(filename)
if err != nil {
return nil, fmt.Errorf("error opening file: %v", err)
}
defer file.Close()
var domains []string
scanner := bufio.NewScanner(file)
for scanner.Scan() {
domain := strings.TrimSpace(scanner.Text())
if domain != "" {
domains = append(domains, domain)
}
}
if err := scanner.Err(); err != nil {
return nil, fmt.Errorf("error reading file: %v", err)
}
return domains, nil
}
func isSubdomain(rawURL, domain string) bool {
u, err := url.Parse(rawURL)
if err != nil {
return false
}
return strings.ToLower(u.Hostname()) != strings.ToLower(domain)
}
func getVersions(u string) ([]string, error) {
client := &http.Client{Timeout: 30 * time.Second}
resp, err := client.Get(fmt.Sprintf(
"http://web.archive.org/cdx/search/cdx?url=%s&output=json", u,
))
if err != nil {
return nil, err
}
defer resp.Body.Close()
var r [][]string
dec := json.NewDecoder(resp.Body)
if err := dec.Decode(&r); err != nil {
return nil, err
}
var out []string
seen := make(map[string]bool)
for i, s := range r {
if i == 0 {
continue // Skip the header row
}
// fields: urlkey, timestamp, original, mimetype, statuscode, digest, length
if seen[s[5]] {
continue
}
seen[s[5]] = true
out = append(out, fmt.Sprintf("https://web.archive.org/web/%sif_/%s", s[1], s[2]))
}
return out, nil
}