-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrdap-domain.go
240 lines (197 loc) · 6.2 KB
/
rdap-domain.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package adapters
import (
"context"
"fmt"
"strings"
"github.com/openrdap/rdap"
"github.com/overmindtech/cli/sdp-go"
"github.com/overmindtech/cli/sdpcache"
)
type RdapDomainAdapter struct {
ClientFac func() *rdap.Client
Cache *sdpcache.Cache
}
// Type is the type of items that this returns
func (s *RdapDomainAdapter) Type() string {
return "rdap-domain"
}
// Name Returns the name of the backend
func (s *RdapDomainAdapter) Name() string {
return "rdap"
}
func (s *RdapDomainAdapter) Metadata() *sdp.AdapterMetadata {
return rdapDomainMetadata
}
var rdapDomainMetadata = Metadata.Register(&sdp.AdapterMetadata{
DescriptiveName: "RDAP Domain",
Type: "rdap-domain",
SupportedQueryMethods: &sdp.AdapterSupportedQueryMethods{
SearchDescription: "Search for a domain record by the domain name e.g. \"www.google.com\"",
Search: true,
},
PotentialLinks: []string{"dns", "rdap-nameserver", "rdap-entity", "rdap-ip-network"},
Category: sdp.AdapterCategory_ADAPTER_CATEGORY_NETWORK,
})
// Weighting of duplicate adapters
func (s *RdapDomainAdapter) Weight() int {
return 100
}
func (s *RdapDomainAdapter) Scopes() []string {
return []string{
"global",
}
}
func (s *RdapDomainAdapter) Get(ctx context.Context, scope string, query string, ignoreCache bool) (*sdp.Item, error) {
// While we can't actually run GET queries, we can return them if they are
// cached
hit, _, items, sdpErr := s.Cache.Lookup(ctx, s.Name(), sdp.QueryMethod_GET, scope, s.Type(), query, ignoreCache)
if sdpErr != nil {
return nil, sdpErr
}
if hit {
if len(items) > 0 {
return items[0], nil
}
}
return nil, &sdp.QueryError{
ErrorType: sdp.QueryError_NOTFOUND,
Scope: scope,
ErrorString: "Domains can't be queried by handle, use the SEARCH method instead",
}
}
func (s *RdapDomainAdapter) List(ctx context.Context, scope string, ignoreCache bool) ([]*sdp.Item, error) {
return nil, &sdp.QueryError{
ErrorType: sdp.QueryError_NOTFOUND,
Scope: scope,
ErrorString: "Domains listed, use the SEARCH method instead",
}
}
// Search for the most specific domain that contains the specified domain. The
// input should be something like "www.google.com". This will first search for
// "www.google.com", then "google.com", then "com"
func (s *RdapDomainAdapter) Search(ctx context.Context, scope string, query string, ignoreCache bool) ([]*sdp.Item, error) {
// Strip the trailing dot if it exists
query = strings.TrimSuffix(query, ".")
hit, ck, items, sdpErr := s.Cache.Lookup(ctx, s.Name(), sdp.QueryMethod_SEARCH, scope, s.Type(), query, ignoreCache)
if sdpErr != nil {
return nil, sdpErr
}
if hit {
return items, nil
}
// Split the query into subdomains
sections := strings.Split(query, ".")
// Start by querying the whole domain, then go down from there, however
// don't query for the top-level domain as it won't return anything useful
for i := 0; i < len(sections)-1; i++ {
domainName := strings.Join(sections[i:], ".")
request := &rdap.Request{
Type: rdap.DomainRequest,
Query: domainName,
}
request = request.WithContext(ctx)
response, err := s.ClientFac().Do(request)
if err != nil {
// If there was an error, continue to the next domain
continue
}
if response.Object == nil {
return nil, &sdp.QueryError{
ErrorType: sdp.QueryError_NOTFOUND,
Scope: scope,
ErrorString: "Empty domain response",
}
}
domain, ok := response.Object.(*rdap.Domain)
if !ok {
return nil, fmt.Errorf("Unexpected response type %T", response.Object)
}
attributes, err := sdp.ToAttributesCustom(map[string]interface{}{
"conformance": domain.Conformance,
"events": domain.Events,
"handle": domain.Handle,
"ldhName": domain.LDHName,
"links": domain.Links,
"notices": domain.Notices,
"objectClassName": domain.ObjectClassName,
"port43": domain.Port43,
"publicIDs": domain.PublicIDs,
"remarks": domain.Remarks,
"secureDNS": domain.SecureDNS,
"status": domain.Status,
"unicodeName": domain.UnicodeName,
"variants": domain.Variants,
}, true, RDAPTransforms)
if err != nil {
return nil, err
}
item := &sdp.Item{
Type: s.Type(),
UniqueAttribute: "handle",
Attributes: attributes,
Scope: scope,
}
// Link to nameservers
for _, nameServer := range domain.Nameservers {
// Look through the HTTP responses until we find one
var parsed *RDAPUrl
for _, httpResponse := range response.HTTP {
if httpResponse.URL != "" {
parsed, err = parseRdapUrl(httpResponse.URL)
if err == nil {
break
}
}
}
// Reconstruct the required query URL
if parsed != nil {
newURL := parsed.ServerRoot.JoinPath("/nameserver/" + nameServer.LDHName)
item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{
Query: &sdp.Query{
Type: "rdap-nameserver",
Method: sdp.QueryMethod_SEARCH,
Query: newURL.String(),
Scope: "global",
},
BlastPropagation: &sdp.BlastPropagation{
// A change in a name server could affect the domains
In: true,
// Domains won't affect the name server
Out: false,
},
})
}
}
// Link to entities
item.LinkedItemQueries = append(item.LinkedItemQueries, extractEntityLinks(domain.Entities)...)
// Link to IP Network
if network := domain.Network; network != nil {
item.LinkedItemQueries = append(item.LinkedItemQueries, &sdp.LinkedItemQuery{
Query: &sdp.Query{
Type: "rdap-ip-network",
Method: sdp.QueryMethod_SEARCH,
Query: network.StartAddress,
Scope: "global",
},
BlastPropagation: &sdp.BlastPropagation{
// Changes to the network could affect the domain presumably
In: true,
// The domain won't affect the network
Out: false,
},
})
}
if err != nil {
return nil, err
}
s.Cache.StoreItem(item, RdapCacheDuration, ck)
return []*sdp.Item{item}, nil
}
err := &sdp.QueryError{
ErrorType: sdp.QueryError_NOTFOUND,
Scope: scope,
ErrorString: fmt.Sprintf("No domain found for %s", query),
}
s.Cache.StoreError(err, RdapCacheDuration, ck)
return nil, err
}