1// Package rdap is a basic client for checking the age of domains through RDAP.
15 "github.com/mjl-/mox/dns"
18var ErrNoRegistration = errors.New("registration date not found")
19var ErrNoRDAP = errors.New("rdap not available for top-level domain")
20var ErrNoDomain = errors.New("domain not found in registry")
21var ErrSyntax = errors.New("bad rdap response syntax")
23// https://www.iana.org/assignments/rdap-dns/rdap-dns.xhtml
25const rdapBoostrapDNSURL = "https://data.iana.org/rdap/dns.json"
29// Bootstrap data, parsed from JSON at the IANA DNS bootstrap URL.
30type Bootstrap struct {
31 Version string `json:"version"` // Should be "1.0".
32 Description string `json:"description"`
33 Publication time.Time `json:"publication"` // RFC3339
35 // Each entry has two elements: First a list of TLDs, then a list of RDAP service
36 // base URLs ending with a slash.
37 Services [][2][]string `json:"services"`
40// todo: when using this more regularly in the admin web interface, store the iana bootstrap response in a database file, including cache-controle results (max-age it seems) and the etag, and do conditional requests when asking for a new version. same for lookups of domains at registries.
42// LookupLastDomainRegistration looks up the most recent (re)registration of a
43// domain through RDAP.
45// Not all TLDs have RDAP services yet at the time of writing.
46func LookupLastDomainRegistration(ctx context.Context, dom dns.Domain) (time.Time, error) {
48 // currently used by the quickstart, which is run once, or run from the cli without
49 // a place to keep state.
50 req, err := http.NewRequestWithContext(ctx, "GET", rdapBoostrapDNSURL, nil)
52 return time.Time{}, fmt.Errorf("new request for iana dns bootstrap data: %v", err)
55 req.Header.Add("Accept", "application/json")
56 resp, err := http.DefaultClient.Do(req)
58 return time.Time{}, fmt.Errorf("http get of iana dns bootstrap data: %v", err)
60 defer resp.Body.Close()
61 if resp.StatusCode/100 != 2 {
62 return time.Time{}, fmt.Errorf("http get resulted in status %q, expected 200 ok", resp.Status)
64 var bootstrap Bootstrap
65 if err := json.NewDecoder(resp.Body).Decode(&bootstrap); err != nil {
66 return time.Time{}, fmt.Errorf("%w: parsing iana dns bootstrap data: %v", ErrSyntax, err)
69 // Note: We don't verify version numbers. If the format change incompatibly,
70 // decoding above would have failed. We'll try to work with what we got.
76 for _, svc := range bootstrap.Services {
77 for _, s := range svc[0] {
79 // currently only single labels, top level domains, in the bootstrap database.
80 if len(s) > len(tldmatch) && (s == dom.ASCII || strings.HasSuffix(dom.ASCII, "."+s)) {
88 return time.Time{}, ErrNoRDAP
90 //
../rfc/9224:172 We must try secure transports before insecure (https before http). In practice, there is just a single https URL.
91 sort.Slice(urls, func(i, j int) bool {
92 return strings.HasPrefix(urls[i], "https://")
95 for _, u := range urls {
97 reg, lastErr = rdapDomainRequest(ctx, u, dom)
102 return time.Time{}, lastErr
108// Domain is the RDAP response for a domain request.
110// More fields are available in RDAP responses, we only parse the one(s) a few.
114 RDAPConformance []string `json:"rdapConformance"` // E.g. "rdap_level_0"
115 LDHName string `json:"ldhName"` // Domain.
116 Events []Event `json:"events"`
119// Event is a historic or future change to the domain.
123 EventAction string `json:"eventAction"` // Required. See https://www.iana.org/assignments/rdap-json-values/rdap-json-values.xhtml.
124 EventDate time.Time `json:"eventDate"` // Required. RFC3339. May be in the future, e.g. date of expiry.
127// rdapDomainRequest looks up a the most recent registration time of a at an RDAP
129func rdapDomainRequest(ctx context.Context, rdapURL string, dom dns.Domain) (time.Time, error) {
132 rdapURL += "domain/" + dom.ASCII
133 req, err := http.NewRequestWithContext(ctx, "GET", rdapURL, nil)
135 return time.Time{}, fmt.Errorf("making http request for rdap service: %v", err)
138 req.Header.Add("Accept", "application/rdap+json")
140 resp, err := http.DefaultClient.Do(req)
142 return time.Time{}, fmt.Errorf("http domain rdap get request: %v", err)
144 defer resp.Body.Close()
147 case resp.StatusCode == http.StatusNotFound:
149 return time.Time{}, ErrNoDomain
151 case resp.StatusCode/100 != 2:
152 // We try to read an error message, perhaps a bit too hard, but we may still
153 // truncate utf-8 in the middle of a rune...
155 var response struct {
156 // For errors, optional fields.
157 Title string `json:"title"`
158 Description []string `json:"description"`
161 buf, err := io.ReadAll(io.LimitReader(resp.Body, 100*1024))
163 msg = fmt.Sprintf("(error reading response: %v)", err)
164 } else if err := json.Unmarshal(buf, &response); err == nil && (response.Title != "" || len(response.Description) > 0) {
166 if s != "" && len(response.Description) > 0 {
169 s += strings.Join(response.Description, " ")
173 msg = fmt.Sprintf("message from remote: %q", s)
177 s = string(buf[:150]) + "..."
181 msg = fmt.Sprintf("raw response: %q", s)
183 return time.Time{}, fmt.Errorf("status %q, expected 200 ok: %s", resp.Status, msg)
187 if err := json.NewDecoder(resp.Body).Decode(&domain); err != nil {
188 return time.Time{}, fmt.Errorf("parse domain rdap response: %v", err)
191 sort.Slice(domain.Events, func(i, j int) bool {
192 return domain.Events[i].EventDate.Before(domain.Events[j].EventDate)
196 for i := len(domain.Events) - 1; i >= 0; i-- {
197 ev := domain.Events[i]
198 if ev.EventDate.After(now) {
201 switch ev.EventAction {
203 case "registration", "reregistration", "reinstantiation":
204 return ev.EventDate, nil
207 return time.Time{}, ErrNoRegistration