forked from registrobr/rdap
/
bootstrap.go
177 lines (138 loc) · 3.26 KB
/
bootstrap.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
package rdap
import (
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"sort"
)
type kind string
const (
dns kind = "dns"
asn kind = "asn"
ipv4 kind = "ipv4"
ipv6 kind = "ipv6"
RDAPBootstrap = "https://data.iana.org/rdap/%s.json"
)
type Bootstrap struct {
httpClient *http.Client
cacheKey string
Bootstrap string
reloadCache bool
IsFromCache func(*http.Response) bool
}
func NewBootstrap(httpClient *http.Client) *Bootstrap {
return &Bootstrap{
Bootstrap: RDAPBootstrap,
httpClient: httpClient,
}
}
func (c *Bootstrap) Domain(fqdn string) ([]string, error) {
return c.query(dns, fqdn)
}
func (c *Bootstrap) ASN(as uint64) ([]string, error) {
return c.query(asn, as)
}
func (c *Bootstrap) IPNetwork(ipnet *net.IPNet) ([]string, error) {
kind := ipv4
if ipnet.IP.To4() == nil {
kind = ipv6
}
return c.query(kind, ipnet)
}
func (c *Bootstrap) IP(ip net.IP) ([]string, error) {
kind := ipv4
if ip.To4() == nil {
kind = ipv6
}
return c.query(kind, ip)
}
func (c *Bootstrap) checkDomain(fqdn string, cached bool, r serviceRegistry) (uris []string, err error) {
uris, err = r.matchDomain(fqdn)
if err != nil {
return
}
if len(uris) > 0 || !cached {
return
}
if nsSet, err := net.LookupNS(fqdn); err != nil || len(nsSet) == 0 {
return nil, nil
}
c.reloadCache = true
body, cached, err := c.fetch(fmt.Sprintf(c.Bootstrap, dns))
if err != nil {
return nil, err
}
defer body.Close()
if err := json.NewDecoder(body).Decode(&r); err != nil {
return nil, err
}
return r.matchDomain(fqdn)
}
func (c *Bootstrap) query(kind kind, identifier interface{}) ([]string, error) {
uris := []string{}
r := serviceRegistry{}
uri := fmt.Sprintf(c.Bootstrap, kind)
body, cached, err := c.fetch(uri)
if err != nil {
return nil, err
}
defer body.Close()
if err := json.NewDecoder(body).Decode(&r); err != nil {
return nil, err
}
if r.Version != version {
return nil, fmt.Errorf("incompatible bootstrap specification version: %s (expecting %s)", r.Version, version)
}
switch kind {
case dns:
uris, err = c.checkDomain(identifier.(string), cached, r)
case asn:
uris, err = r.matchAS(identifier.(uint64))
case ipv4, ipv6:
if ip, ok := identifier.(net.IP); ok {
uris, err = r.matchIP(ip)
break
}
if ipNet, ok := identifier.(*net.IPNet); ok {
uris, err = r.matchIPNetwork(ipNet)
break
}
}
if err != nil {
return nil, err
}
if len(uris) == 0 {
return nil, fmt.Errorf("no matches for %v", identifier)
}
sort.Sort(prioritizeHTTPS(uris))
return uris, nil
}
func (c *Bootstrap) fetch(uri string) (_ io.ReadCloser, cached bool, err error) {
req, err := http.NewRequest("GET", uri, nil)
if err != nil {
return nil, cached, err
}
if c.reloadCache {
req.Header.Add("Cache-Control", "max-age=0")
}
c.cacheKey = req.URL.String()
if c.httpClient == nil {
c.httpClient = &http.Client{}
}
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, cached, err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNotModified {
return nil, false, fmt.Errorf("unexpected status code %d %s",
resp.StatusCode,
http.StatusText(resp.StatusCode),
)
}
if c.IsFromCache != nil {
cached = c.IsFromCache(resp)
}
return resp.Body, cached, nil
}