-
Notifications
You must be signed in to change notification settings - Fork 486
/
findsubdomains.go
117 lines (90 loc) · 2.4 KB
/
findsubdomains.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
package main
import (
"fmt"
"os"
)
var apiToken = os.Getenv("SPYSE_API_TOKEN")
func callSubdomainsAggregateEndpoint(domain string) []string {
out := make([]string, 0)
fetchURL := fmt.Sprintf(
"https://api.spyse.com/v1/subdomains-aggregate?api_token=%s&domain=%s",
apiToken, domain,
)
type Cidr struct {
Results []struct {
Data struct {
Domains []string `json:"domains"`
} `json:"data"`
} `json:"results"`
}
type Cidrs struct {
Cidr16, Cidr24 Cidr
}
wrapper := struct {
Cidrs Cidrs `json:"cidr"`
}{}
err := fetchJSON(fetchURL, &wrapper)
if err != nil {
// Fail silently
return []string{}
}
for _, result := range wrapper.Cidrs.Cidr16.Results {
for _, domain := range result.Data.Domains {
out = append(out, domain)
}
}
for _, result := range wrapper.Cidrs.Cidr24.Results {
for _, domain := range result.Data.Domains {
out = append(out, domain)
}
}
return out
}
/**
*/
func callSubdomainsEndpoint(domain string) []string {
out := make([]string, 0)
// Start querying the Spyse API from page 1
page := 1
for {
wrapper := struct {
Records []struct {
Domain string `json:"domain"`
} `json:"records"`
}{}
fetchURL := fmt.Sprintf(
"https://api.spyse.com/v1/subdomains?api_token=%s&domain=%s&page=%d",
apiToken, domain, page,
)
err := fetchJSON(fetchURL, &wrapper)
if err != nil {
// Fail silently, by returning what we got so far
return out
}
// The API does not respond with any paging, nor does it give us any idea of
// the total amount of domains, so we just have to keep asking for a new page until
// the returned `records` array is empty
// NOTE: The free tier always gives you the first page for free, and you get "25 unlimited search requests"
if len(wrapper.Records) == 0 {
break
}
for _, record := range wrapper.Records {
out = append(out, record.Domain)
}
page++
}
return out
}
func fetchFindSubDomains(domain string) ([]string, error) {
out := make([]string, 0)
apiToken := os.Getenv("SPYSE_API_TOKEN")
if apiToken == "" {
// Must have an API token
return []string{}, nil
}
// The Subdomains-Aggregate endpoint returns some, but not all available domains
out = append(out, callSubdomainsAggregateEndpoint(domain)...)
// The Subdomains endpoint only guarantees the first 30 domains, the rest needs credit at Spyze
out = append(out, callSubdomainsEndpoint(domain)...)
return out, nil
}