1// Package rdap is a basic client for checking the age of domains through RDAP.
15 "github.com/mjl-/mox/dns"
16 "github.com/mjl-/mox/mlog"
19var ErrNoRegistration = errors.New("registration date not found")
20var ErrNoRDAP = errors.New("rdap not available for top-level domain")
21var ErrNoDomain = errors.New("domain not found in registry")
22var ErrSyntax = errors.New("bad rdap response syntax")
24// https://www.iana.org/assignments/rdap-dns/rdap-dns.xhtml
26const rdapBoostrapDNSURL = "https://data.iana.org/rdap/dns.json"
30// Bootstrap data, parsed from JSON at the IANA DNS bootstrap URL.
31type Bootstrap struct {
32 Version string `json:"version"` // Should be "1.0".
33 Description string `json:"description"`
34 Publication time.Time `json:"publication"` // RFC3339
36 // Each entry has two elements: First a list of TLDs, then a list of RDAP service
37 // base URLs ending with a slash.
38 Services [][2][]string `json:"services"`
41// 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.
43// LookupLastDomainRegistration looks up the most recent (re)registration of a
44// domain through RDAP.
46// Not all TLDs have RDAP services yet at the time of writing.
47func LookupLastDomainRegistration(ctx context.Context, log mlog.Log, dom dns.Domain) (time.Time, error) {
49 // currently used by the quickstart, which is run once, or run from the cli without
50 // a place to keep state.
51 req, err := http.NewRequestWithContext(ctx, "GET", rdapBoostrapDNSURL, nil)
53 return time.Time{}, fmt.Errorf("new request for iana dns bootstrap data: %v", err)
56 req.Header.Add("Accept", "application/json")
57 resp, err := http.DefaultClient.Do(req)
59 return time.Time{}, fmt.Errorf("http get of iana dns bootstrap data: %v", err)
62 err := resp.Body.Close()
63 log.Check(err, "closing http response body")
65 if resp.StatusCode/100 != 2 {
66 return time.Time{}, fmt.Errorf("http get resulted in status %q, expected 200 ok", resp.Status)
68 var bootstrap Bootstrap
69 if err := json.NewDecoder(resp.Body).Decode(&bootstrap); err != nil {
70 return time.Time{}, fmt.Errorf("%w: parsing iana dns bootstrap data: %v", ErrSyntax, err)
73 // Note: We don't verify version numbers. If the format change incompatibly,
74 // decoding above would have failed. We'll try to work with what we got.
80 for _, svc := range bootstrap.Services {
81 for _, s := range svc[0] {
83 // currently only single labels, top level domains, in the bootstrap database.
84 if len(s) > len(tldmatch) && (s == dom.ASCII || strings.HasSuffix(dom.ASCII, "."+s)) {
92 return time.Time{}, ErrNoRDAP
94 //
../rfc/9224:172 We must try secure transports before insecure (https before http). In practice, there is just a single https URL.
95 sort.Slice(urls, func(i, j int) bool {
96 return strings.HasPrefix(urls[i], "https://")
99 for _, u := range urls {
101 reg, lastErr = rdapDomainRequest(ctx, log, u, dom)
106 return time.Time{}, lastErr
112// Domain is the RDAP response for a domain request.
114// More fields are available in RDAP responses, we only parse the one(s) a few.
118 RDAPConformance []string `json:"rdapConformance"` // E.g. "rdap_level_0"
119 LDHName string `json:"ldhName"` // Domain.
120 Events []Event `json:"events"`
123// Event is a historic or future change to the domain.
127 EventAction string `json:"eventAction"` // Required. See https://www.iana.org/assignments/rdap-json-values/rdap-json-values.xhtml.
128 EventDate time.Time `json:"eventDate"` // Required. RFC3339. May be in the future, e.g. date of expiry.
131// rdapDomainRequest looks up a the most recent registration time of a at an RDAP
133func rdapDomainRequest(ctx context.Context, log mlog.Log, rdapURL string, dom dns.Domain) (time.Time, error) {
136 rdapURL += "domain/" + dom.ASCII
137 req, err := http.NewRequestWithContext(ctx, "GET", rdapURL, nil)
139 return time.Time{}, fmt.Errorf("making http request for rdap service: %v", err)
142 req.Header.Add("Accept", "application/rdap+json")
144 resp, err := http.DefaultClient.Do(req)
146 return time.Time{}, fmt.Errorf("http domain rdap get request: %v", err)
149 err := resp.Body.Close()
150 log.Check(err, "closing http response body")
154 case resp.StatusCode == http.StatusNotFound:
156 return time.Time{}, ErrNoDomain
158 case resp.StatusCode/100 != 2:
159 // We try to read an error message, perhaps a bit too hard, but we may still
160 // truncate utf-8 in the middle of a rune...
162 var response struct {
163 // For errors, optional fields.
164 Title string `json:"title"`
165 Description []string `json:"description"`
168 buf, err := io.ReadAll(io.LimitReader(resp.Body, 100*1024))
170 msg = fmt.Sprintf("(error reading response: %v)", err)
171 } else if err := json.Unmarshal(buf, &response); err == nil && (response.Title != "" || len(response.Description) > 0) {
173 if s != "" && len(response.Description) > 0 {
176 s += strings.Join(response.Description, " ")
180 msg = fmt.Sprintf("message from remote: %q", s)
184 s = string(buf[:150]) + "..."
188 msg = fmt.Sprintf("raw response: %q", s)
190 return time.Time{}, fmt.Errorf("status %q, expected 200 ok: %s", resp.Status, msg)
194 if err := json.NewDecoder(resp.Body).Decode(&domain); err != nil {
195 return time.Time{}, fmt.Errorf("parse domain rdap response: %v", err)
198 sort.Slice(domain.Events, func(i, j int) bool {
199 return domain.Events[i].EventDate.Before(domain.Events[j].EventDate)
203 for i := len(domain.Events) - 1; i >= 0; i-- {
204 ev := domain.Events[i]
205 if ev.EventDate.After(now) {
208 switch ev.EventAction {
210 case "registration", "reregistration", "reinstantiation":
211 return ev.EventDate, nil
214 return time.Time{}, ErrNoRegistration