expiration-check/pkg/rdap/rdap.go

75 lines
1.4 KiB
Go

package rdap
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)
type RdapResponseData struct {
Events []struct {
EventAction string `json:"eventAction"`
EventDate string `json:"eventDate"`
} `json:"events"`
}
type RdapServices struct {
Services [][][]string `json:"services"`
}
func GetRdapServices() (map[string]string, error) {
response, err := http.Get("https://data.iana.org/rdap/dns.json")
if err != nil {
return nil, err
}
values := make(map[string]string)
defer response.Body.Close()
var data RdapServices
json.NewDecoder(response.Body).Decode(&data)
for _, value := range data.Services {
for _, tld := range value[0] {
values[tld] = value[1][0]
}
}
return values, nil
}
func GetExpiration(domain, service string) (*time.Time, error) {
url := fmt.Sprintf("%sdomain/%s?jscard=1", service, domain)
response, err := http.Get(url)
if err != nil {
return nil, err
}
defer response.Body.Close()
var data RdapResponseData
json.NewDecoder(response.Body).Decode(&data)
actions := []string{"expiration", "Record expires"}
for _, event := range data.Events {
for _, action := range actions {
if action == event.EventAction {
date, err := time.Parse(time.RFC3339, event.EventDate)
if err != nil {
return nil, err
}
return &date, nil
}
}
}
return nil, errors.New("Expiration date not found")
}