-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhois.go
76 lines (63 loc) · 1.8 KB
/
whois.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
package main
import (
"encoding/xml"
"io"
"net/http"
"net/netip"
"strings"
)
// Makes a GET request to the whois database API and returns the unmashalled result
func getWhoisData(net netip.Prefix, url string) (WhoisResult, error) {
var whoisResult WhoisResult
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return whoisResult, err
}
req.Header.Set("Accept", "application/xml")
req.Header.Set("User-Agent", "go-geofeed/contact "+cfg.Email)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return whoisResult, err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return whoisResult, err
}
err = xml.Unmarshal(body, &whoisResult)
if err != nil {
return whoisResult, err
}
return whoisResult, nil
}
// Loops through the whois result and returns a slice of subnets
func parseWhoisResult(whoisResult WhoisResult) ([]Subnet, error) {
var subnets []Subnet
for _, object := range whoisResult.Objects {
var subnet Subnet
for _, attribute := range object.Attributes {
if attribute.Name == "inetnum" {
// The inetnum attribute contains the start and end IP address of the subnet
// in the format of "192.0.2.0 - 192.0.2.255", but we need it in CIDR notation
inetnum := strings.Split(attribute.Value, " - ")
start, err := netip.ParseAddr(inetnum[0])
if err != nil {
return subnets, err
}
end, err := netip.ParseAddr(inetnum[1])
if err != nil {
return subnets, err
}
subnet.Prefix = calcIPv4Cidr(start, end)
} else if attribute.Name == "inet6num" {
// inet6num always uses CIDR notation
subnet.Prefix = netip.MustParsePrefix(attribute.Value)
}
if attribute.Name == "country" {
subnet.Country = attribute.Value
}
}
subnets = append(subnets, subnet)
}
return subnets, nil
}