All checks were successful
ci/woodpecker/push/build Pipeline was successful
91 lines
1.8 KiB
Go
91 lines
1.8 KiB
Go
package checker
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"math"
|
|
"net/http"
|
|
"strings"
|
|
"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 {
|
|
response, _ := http.Get("https://data.iana.org/rdap/dns.json")
|
|
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
|
|
}
|
|
|
|
func ExtractTld(domain string) string {
|
|
elements := strings.Split(domain, ".")
|
|
|
|
if len(elements) == 1 {
|
|
return elements[0]
|
|
}
|
|
|
|
return strings.Join(elements[1:], ".")
|
|
}
|
|
|
|
func RdapCheck(domain, service string) Domain {
|
|
url := fmt.Sprintf("%sdomain/%s?jscard=1", service, domain)
|
|
response, _ := http.Get(url)
|
|
now := time.Now()
|
|
|
|
defer response.Body.Close()
|
|
var data RdapResponseData
|
|
json.NewDecoder(response.Body).Decode(&data)
|
|
|
|
for _, event := range data.Events {
|
|
if event.EventAction == "expiration" {
|
|
date, _ := time.Parse(time.RFC3339, event.EventDate)
|
|
daysLeft := date.Sub(now).Hours() / 24
|
|
|
|
return Domain{
|
|
Name: domain,
|
|
DaysLeft: math.Floor(daysLeft),
|
|
Date: date.Format(time.DateTime),
|
|
}
|
|
}
|
|
}
|
|
|
|
return Domain{Name: domain, Failed: true}
|
|
}
|
|
|
|
func CheckDomains(domains []string) []Domain {
|
|
values := []Domain{}
|
|
services := GetRdapServices()
|
|
|
|
for _, domain := range domains {
|
|
tld := ExtractTld(domain)
|
|
service := services[tld]
|
|
|
|
if service != "" {
|
|
values = append(values, RdapCheck(domain, service))
|
|
} else {
|
|
values = append(values, Domain{Name: domain, Failed: true})
|
|
}
|
|
}
|
|
|
|
return values
|
|
}
|