-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrdap.go
76 lines (68 loc) · 1.71 KB
/
rdap.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 whois
import (
"encoding/json"
"errors"
"io/ioutil"
"math/rand"
"net/http"
"strings"
"time"
)
type rdapBootstrap struct {
Version string `json:"version"`
Description string `json:"description"`
Publication time.Time `json:"publication"`
Services [][][]string `json:"services"`
}
var (
rdapDns = map[string][]string{}
rdapClient = &http.Client{}
)
func GetRdapClient() *http.Client {
return rdapClient
}
func SetRdapClient(client *http.Client) {
rdapClient = client
}
func RefreshMap() {
response, err := http.Get("http://data.iana.org/rdap/dns.json")
if err == nil {
body, err := ioutil.ReadAll(response.Body)
if err == nil {
bootstrap := rdapBootstrap{}
err := json.Unmarshal(body, &bootstrap)
if err == nil {
rdapDns = map[string][]string{}
for _, svc := range bootstrap.Services {
for _, tld := range svc[0] {
for _, endpoint := range svc[1] {
rdapDns[tld] = append(rdapDns[tld], endpoint)
}
}
}
}
}
}
}
func IsAvailableFromRdap(domain string) (bool, error) {
split := strings.SplitN(domain, ".", 2)
if services, ok := rdapDns[split[1]]; ok && len(services) > 0 {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(services), func(i, j int) { services[i], services[j] = services[j], services[i] })
for len(services) > 0 {
service := services[0]
services = services[1:]
response, err := rdapClient.Get(service + "domain/" + domain)
if err == nil {
if response.StatusCode == 404 {
return true, nil
}
if response.StatusCode == 200 {
return false, nil
}
}
}
return false, errors.New("no valid response from rdap endpoint")
}
return false, errors.New("not an rdap enabled tld")
}