-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
244 lines (210 loc) · 5.77 KB
/
main.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
241
242
243
244
package adapters
import (
"errors"
"net/url"
"reflect"
"regexp"
"time"
"github.com/openrdap/rdap"
"github.com/overmindtech/cli/discovery"
"github.com/overmindtech/cli/sdp-go"
"github.com/overmindtech/cli/sdpcache"
"github.com/overmindtech/cli/stdlib-source/adapters/test"
log "github.com/sirupsen/logrus"
"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
_ "embed"
)
var Metadata = sdp.AdapterMetadataList{}
// Cache duration for RDAP adapters, these things shouldn't change very often
const RdapCacheDuration = 30 * time.Minute
func InitializeEngine(ec *discovery.EngineConfig, reverseDNS bool) (*discovery.Engine, error) {
e, err := discovery.NewEngine(ec)
if err != nil {
log.WithFields(log.Fields{
"error": err.Error(),
}).Fatal("Error initializing Engine")
}
if ec.HeartbeatOptions != nil {
ec.HeartbeatOptions.HealthCheck = func() error {
// This can't fail, it's always healthy
return nil
}
}
// Add the base adapters
adapters := []discovery.Adapter{
&CertificateAdapter{},
&DNSAdapter{
ReverseLookup: reverseDNS,
},
&HTTPAdapter{},
&IPAdapter{},
&test.TestDogAdapter{},
&test.TestFoodAdapter{},
&test.TestGroupAdapter{},
&test.TestHobbyAdapter{},
&test.TestLocationAdapter{},
&test.TestPersonAdapter{},
&test.TestRegionAdapter{},
&RdapIPNetworkAdapter{
ClientFac: newRdapClient,
Cache: sdpcache.NewCache(),
IPCache: NewIPCache[*rdap.IPNetwork](),
},
&RdapASNAdapter{
ClientFac: newRdapClient,
Cache: sdpcache.NewCache(),
},
&RdapDomainAdapter{
ClientFac: newRdapClient,
Cache: sdpcache.NewCache(),
},
&RdapEntityAdapter{
ClientFac: newRdapClient,
Cache: sdpcache.NewCache(),
},
&RdapNameserverAdapter{
ClientFac: newRdapClient,
Cache: sdpcache.NewCache(),
},
}
err = e.AddAdapters(adapters...)
return e, err
}
// newRdapClient Creates a new RDAP client using otelhttp.DefaultClient. rdap is suspected to not be thread safe, so we create a new client for each request
func newRdapClient() *rdap.Client {
return &rdap.Client{
HTTP: otelhttp.DefaultClient,
}
}
// Wraps an RDAP error in an SDP error, correctly checking for things like 404s
func wrapRdapError(err error) error {
if err == nil {
return nil
}
var rdapError *rdap.ClientError
if ok := errors.As(err, &rdapError); ok {
if rdapError.Type == rdap.ObjectDoesNotExist {
return &sdp.QueryError{
ErrorType: sdp.QueryError_NOTFOUND,
ErrorString: err.Error(),
}
}
}
return err
}
// Extracts SDP queries from a list of entities
func extractEntityLinks(entities []rdap.Entity) []*sdp.LinkedItemQuery {
queries := make([]*sdp.LinkedItemQuery, 0)
for _, entity := range entities {
var selfLink string
// Loop over the links until you find the self link
for _, link := range entity.Links {
if link.Rel == "self" {
selfLink = link.Href
break
}
}
if selfLink != "" {
queries = append(queries, &sdp.LinkedItemQuery{
Query: &sdp.Query{
Type: "rdap-entity",
Method: sdp.QueryMethod_SEARCH,
Query: selfLink,
Scope: "global",
},
BlastPropagation: &sdp.BlastPropagation{
// The Entity isn't a "real" component, so no matter what
// changes it won't actually "affect" anything
In: false,
Out: false,
},
})
}
}
return queries
}
var rdapUrlRegex = regexp.MustCompile(`^(https?:\/\/.+)\/(ip|nameserver|entity|autnum|domain)\/([^\/]+)$`)
type RDAPUrl struct {
// The path to the root where queries should be run i.e.
// https://rdap.apnic.net
ServerRoot *url.URL
// The type of query to run i.e. ip, nameserver, entity, autnum, domain
Type string
// The query to run i.e. 1.1.1.1
Query string
}
// Parses an RDAP URL and returns the important components
func parseRdapUrl(rdapUrl string) (*RDAPUrl, error) {
matches := rdapUrlRegex.FindStringSubmatch(rdapUrl)
if len(matches) != 4 {
return nil, errors.New("Invalid RDAP URL")
}
serverRoot, err := url.Parse(matches[1])
if err != nil {
return nil, err
}
return &RDAPUrl{
ServerRoot: serverRoot,
Type: matches[2],
Query: matches[3],
}, nil
}
var RDAPTransforms = sdp.AddDefaultTransforms(sdp.TransformMap{
reflect.TypeOf(rdap.Link{}): func(i interface{}) interface{} {
// We only want to return the href for links
link, ok := i.(rdap.Link)
if ok {
return link.Href
}
return ""
},
reflect.TypeOf(rdap.VCard{}): func(i interface{}) interface{} {
vcard, ok := i.(rdap.VCard)
if ok {
// Convert a vCard to a map as it's much more readable
vCardDetails := make(map[string]string)
if name := vcard.Name(); name != "" {
vCardDetails["Name"] = name
}
if pOBox := vcard.POBox(); pOBox != "" {
vCardDetails["POBox"] = pOBox
}
if extendedAddress := vcard.ExtendedAddress(); extendedAddress != "" {
vCardDetails["ExtendedAddress"] = extendedAddress
}
if streetAddress := vcard.StreetAddress(); streetAddress != "" {
vCardDetails["StreetAddress"] = streetAddress
}
if locality := vcard.Locality(); locality != "" {
vCardDetails["Locality"] = locality
}
if region := vcard.Region(); region != "" {
vCardDetails["Region"] = region
}
if postalCode := vcard.PostalCode(); postalCode != "" {
vCardDetails["PostalCode"] = postalCode
}
if country := vcard.Country(); country != "" {
vCardDetails["Country"] = country
}
if tel := vcard.Tel(); tel != "" {
vCardDetails["Tel"] = tel
}
if fax := vcard.Fax(); fax != "" {
vCardDetails["Fax"] = fax
}
if email := vcard.Email(); email != "" {
vCardDetails["Email"] = email
}
if org := vcard.Org(); org != "" {
vCardDetails["Org"] = org
}
return vCardDetails
}
return nil
},
reflect.TypeOf(&rdap.DecodeData{}): func(i interface{}) interface{} {
// Exclude these
return nil
},
})