2026-05-05 21:56:08 +08:00
|
|
|
import { http, logger } from "@certd/basic";
|
2026-04-06 00:33:08 +08:00
|
|
|
import { parseDomainByPsl } from "@certd/plugin-lib";
|
2026-05-05 21:56:08 +08:00
|
|
|
import dayjs from "dayjs";
|
|
|
|
|
import customParseFormat from "dayjs/plugin/customParseFormat.js";
|
|
|
|
|
|
|
|
|
|
dayjs.extend(customParseFormat);
|
2026-04-06 00:33:08 +08:00
|
|
|
|
|
|
|
|
export interface DomainInfo {
|
|
|
|
|
expirationDate?: number;
|
|
|
|
|
registrationDate?: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export class TldClient {
|
2026-05-05 21:56:08 +08:00
|
|
|
private static readonly RDAP_SS_API = "https://rdap.ss/api/query";
|
|
|
|
|
private static readonly RDAP_SS_RATE_LIMITS = [
|
|
|
|
|
{ windowMs: 60 * 1000, max: 30 },
|
|
|
|
|
{ windowMs: 60 * 60 * 1000, max: 1200 },
|
|
|
|
|
{ windowMs: 24 * 60 * 60 * 1000, max: 12000 },
|
|
|
|
|
];
|
|
|
|
|
private static readonly RDAP_SS_MAX_WAIT_MS = 3 * 60 * 1000;
|
|
|
|
|
private static rdapSsRequestTimes: number[] = [];
|
|
|
|
|
|
|
|
|
|
private rdapMap: Record<string, string> = {};
|
2026-04-06 00:33:08 +08:00
|
|
|
private isInitialized = false;
|
|
|
|
|
|
2026-05-05 21:56:08 +08:00
|
|
|
constructor() {}
|
2026-04-06 00:33:08 +08:00
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
|
if (this.isInitialized) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
const dnsJson = await http.request({
|
|
|
|
|
url: "https://data.iana.org/rdap/dns.json",
|
|
|
|
|
method: "GET",
|
2026-05-05 21:56:08 +08:00
|
|
|
});
|
2026-04-06 00:33:08 +08:00
|
|
|
for (const item of dnsJson.services) {
|
2026-05-05 21:56:08 +08:00
|
|
|
const suffixes = item[0];
|
|
|
|
|
const urls = item[1];
|
2026-04-06 00:33:08 +08:00
|
|
|
for (const suffix of suffixes) {
|
2026-05-05 21:56:08 +08:00
|
|
|
this.rdapMap[suffix] = urls[0];
|
2026-04-06 00:33:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.isInitialized = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getDomainExpirationDate(domain: string): Promise<DomainInfo> {
|
|
|
|
|
await this.init();
|
2026-05-05 21:56:08 +08:00
|
|
|
|
|
|
|
|
const parsed = parseDomainByPsl(domain);
|
|
|
|
|
const mainDomain = parsed.domain || "";
|
2026-04-06 00:33:08 +08:00
|
|
|
if (mainDomain !== domain) {
|
2026-05-05 21:56:08 +08:00
|
|
|
const message = `【${domain}】为子域名,无法获取过期时间`;
|
|
|
|
|
logger.warn(message);
|
|
|
|
|
throw new Error(message);
|
2026-04-06 00:33:08 +08:00
|
|
|
}
|
2026-05-05 21:56:08 +08:00
|
|
|
|
2026-04-06 00:33:08 +08:00
|
|
|
try {
|
2026-05-05 21:56:08 +08:00
|
|
|
return await this.getDomainExpirationByRdap(domain, parsed.tld || "");
|
2026-04-06 00:33:08 +08:00
|
|
|
} catch (error) {
|
2026-05-05 21:56:08 +08:00
|
|
|
logger.error(this.getErrorMessage(error));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
return await this.getDomainExpirationByWhoiser(domain);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
logger.error(this.getErrorMessage(error));
|
|
|
|
|
return await this.getDomainExpirationByRdapSs(domain);
|
2026-04-06 00:33:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async getDomainExpirationByRdap(domain: string, suffix: string): Promise<DomainInfo> {
|
2026-05-05 21:56:08 +08:00
|
|
|
const rdapUrl = this.rdapMap[suffix];
|
2026-04-06 00:33:08 +08:00
|
|
|
if (!rdapUrl) {
|
2026-05-05 21:56:08 +08:00
|
|
|
throw new Error(`【${domain}】未找到${suffix}的rdap地址`);
|
2026-04-06 00:33:08 +08:00
|
|
|
}
|
2026-05-05 21:56:08 +08:00
|
|
|
|
2026-04-06 00:33:08 +08:00
|
|
|
const rdap = await http.request({
|
|
|
|
|
url: `${rdapUrl}domain/${domain}`,
|
|
|
|
|
method: "GET",
|
2026-05-05 21:56:08 +08:00
|
|
|
});
|
2026-04-06 00:33:08 +08:00
|
|
|
|
2026-05-05 21:56:08 +08:00
|
|
|
let res: DomainInfo = {};
|
|
|
|
|
const events = rdap.events || [];
|
2026-04-06 00:33:08 +08:00
|
|
|
for (const item of events) {
|
2026-05-05 21:56:08 +08:00
|
|
|
if (item.eventAction === "expiration") {
|
|
|
|
|
res.expirationDate = dayjs(item.eventDate).valueOf();
|
|
|
|
|
} else if (item.eventAction === "registration") {
|
|
|
|
|
res.registrationDate = dayjs(item.eventDate).valueOf();
|
2026-04-06 00:33:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-05-05 21:56:08 +08:00
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async getDomainExpirationByRdapSs(domain: string): Promise<DomainInfo> {
|
|
|
|
|
await this.waitRdapSsRateLimit();
|
|
|
|
|
|
|
|
|
|
const result = await http.request({
|
|
|
|
|
url: `${TldClient.RDAP_SS_API}?q=${encodeURIComponent(domain)}`,
|
|
|
|
|
method: "GET",
|
|
|
|
|
logRes: false,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (!result?.success || !result?.data) {
|
|
|
|
|
throw new Error(`【${domain}】rdap.ss查询失败`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data = result.data?.whoisData || result.data?.rawData || {};
|
|
|
|
|
const res: DomainInfo = {};
|
|
|
|
|
const expirationDate = this.parseFirstDate(data, ["Expiry Date", "Expiration Date", "Registry Expiry Date", "expires"]);
|
|
|
|
|
const registrationDate = this.parseFirstDate(data, ["Domain Name Commencement Date", "Created Date", "Creation Date", "Registration Date", "Registered On"]);
|
|
|
|
|
|
|
|
|
|
if (expirationDate) {
|
|
|
|
|
res.expirationDate = expirationDate;
|
|
|
|
|
}
|
|
|
|
|
if (registrationDate) {
|
|
|
|
|
res.registrationDate = registrationDate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!res.expirationDate) {
|
|
|
|
|
throw new Error(`【${domain}】rdap.ss查询未找到过期时间`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return res;
|
2026-04-06 00:33:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async getDomainExpirationByWhoiser(domain: string): Promise<DomainInfo> {
|
2026-05-05 21:56:08 +08:00
|
|
|
const whoiser = await import("whoiser");
|
2026-04-06 00:33:08 +08:00
|
|
|
const result = await whoiser.whoisDomain(domain, {
|
|
|
|
|
follow: 2,
|
2026-05-05 21:56:08 +08:00
|
|
|
timeout: 5000,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let res: DomainInfo = {};
|
2026-04-06 00:33:08 +08:00
|
|
|
/**
|
|
|
|
|
* {
|
|
|
|
|
"Domain Status": [
|
|
|
|
|
"ok",
|
|
|
|
|
],
|
|
|
|
|
"Name Server": [
|
|
|
|
|
"dns21.hichina.com",
|
|
|
|
|
"dns22.hichina.com",
|
|
|
|
|
],
|
|
|
|
|
text: [
|
|
|
|
|
"",
|
|
|
|
|
],
|
|
|
|
|
"Domain Name": "docmirror.cn",
|
|
|
|
|
ROID: "20200907s10001s31265717-cn",
|
|
|
|
|
"Registrant Name": "肖君诺",
|
|
|
|
|
"Registrant Email": "252959493@qq.com",
|
|
|
|
|
Registrar: "阿里巴巴云计算(北京)有限公司",
|
|
|
|
|
"Created Date": "2020-09-07 09:22:54",
|
|
|
|
|
"Expiry Date": "2026-09-07 09:22:54",
|
|
|
|
|
DNSSEC: "unsigned",
|
|
|
|
|
}
|
|
|
|
|
*/
|
2026-05-05 21:56:08 +08:00
|
|
|
|
2026-04-06 00:33:08 +08:00
|
|
|
for (const server in result) {
|
2026-05-05 21:56:08 +08:00
|
|
|
const data = result[server] as any;
|
|
|
|
|
if (data["Expiry Date"]) {
|
|
|
|
|
res.expirationDate = dayjs(data["Expiry Date"]).valueOf();
|
2026-04-06 00:33:08 +08:00
|
|
|
}
|
2026-05-05 21:56:08 +08:00
|
|
|
if (data["Created Date"]) {
|
|
|
|
|
res.registrationDate = dayjs(data["Created Date"]).valueOf();
|
2026-04-06 00:33:08 +08:00
|
|
|
}
|
|
|
|
|
if (res.expirationDate && res.registrationDate) {
|
2026-05-05 21:56:08 +08:00
|
|
|
break;
|
2026-04-06 00:33:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
2026-05-05 21:56:08 +08:00
|
|
|
|
2026-04-06 00:33:08 +08:00
|
|
|
if (!res.expirationDate) {
|
2026-05-05 21:56:08 +08:00
|
|
|
throw new Error(`【${domain}】whois查询未找到过期时间`);
|
2026-04-06 00:33:08 +08:00
|
|
|
}
|
2026-05-05 21:56:08 +08:00
|
|
|
|
|
|
|
|
return res;
|
2026-04-06 00:33:08 +08:00
|
|
|
}
|
2026-05-05 21:56:08 +08:00
|
|
|
|
|
|
|
|
private async waitRdapSsRateLimit() {
|
|
|
|
|
while (true) {
|
|
|
|
|
const now = Date.now();
|
|
|
|
|
const maxWindowMs = Math.max(...TldClient.RDAP_SS_RATE_LIMITS.map(item => item.windowMs));
|
|
|
|
|
TldClient.rdapSsRequestTimes = TldClient.rdapSsRequestTimes.filter(time => now - time < maxWindowMs);
|
|
|
|
|
|
|
|
|
|
const waitMs = TldClient.RDAP_SS_RATE_LIMITS.reduce((maxWaitMs, limit) => {
|
|
|
|
|
const times = TldClient.rdapSsRequestTimes.filter(time => now - time < limit.windowMs);
|
|
|
|
|
if (times.length < limit.max) {
|
|
|
|
|
return maxWaitMs;
|
|
|
|
|
}
|
|
|
|
|
const earliestTime = Math.min(...times);
|
|
|
|
|
return Math.max(maxWaitMs, earliestTime + limit.windowMs - now);
|
|
|
|
|
}, 0);
|
|
|
|
|
|
|
|
|
|
if (waitMs <= 0) {
|
|
|
|
|
TldClient.rdapSsRequestTimes.push(now);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (waitMs > TldClient.RDAP_SS_MAX_WAIT_MS) {
|
|
|
|
|
throw new Error(`rdap.ss查询达到速率限制,等待时间超过3分钟`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
logger.warn(`rdap.ss查询达到速率限制,将在${waitMs}ms后重试`);
|
|
|
|
|
await this.sleep(waitMs);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private parseFirstDate(data: Record<string, any>, keys: string[]) {
|
|
|
|
|
for (const key of keys) {
|
|
|
|
|
const value = data[key];
|
|
|
|
|
const values = Array.isArray(value) ? value : [value];
|
|
|
|
|
for (const item of values) {
|
|
|
|
|
const timestamp = this.parseDateValue(item);
|
|
|
|
|
if (timestamp) {
|
|
|
|
|
return timestamp;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private parseDateValue(value: any) {
|
|
|
|
|
if (!value || typeof value !== "string") {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const formats = ["DD-MM-YYYY", "YYYY-MM-DD", "YYYY-MM-DD HH:mm:ss", "YYYY/MM/DD", "YYYY/MM/DD HH:mm:ss", "MMM D YYYY", "MMM D, YYYY"];
|
|
|
|
|
for (const format of formats) {
|
|
|
|
|
const parsed = dayjs(value, format, true);
|
|
|
|
|
if (parsed.isValid()) {
|
|
|
|
|
return parsed.valueOf();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const parsed = dayjs(value);
|
|
|
|
|
if (parsed.isValid()) {
|
|
|
|
|
return parsed.valueOf();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private sleep(ms: number) {
|
|
|
|
|
return new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getErrorMessage(error: unknown) {
|
|
|
|
|
if (error instanceof Error) {
|
|
|
|
|
return error.message;
|
|
|
|
|
}
|
|
|
|
|
return String(error);
|
|
|
|
|
}
|
|
|
|
|
}
|