mirror of
https://github.com/certd/certd.git
synced 2026-04-14 20:40:53 +08:00
perf: 从域名的soa获取主域名,子域名托管无需额外配置
This commit is contained in:
@@ -24,6 +24,7 @@
|
||||
"jszip": "^3.10.1",
|
||||
"lodash-es": "^4.17.21",
|
||||
"psl": "^1.9.0",
|
||||
"punycode": "^2.3.1",
|
||||
"rimraf": "^5.0.5"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -35,6 +35,8 @@ export interface IDnsProvider<T = any> {
|
||||
createRecord(options: CreateRecordOptions): Promise<T>;
|
||||
removeRecord(options: RemoveRecordOptions<T>): Promise<void>;
|
||||
setCtx(ctx: DnsProviderContext): void;
|
||||
//中文域名是否需要punycode转码,如果返回True,则使用punycode来添加解析记录,否则使用中文域名添加解析记录
|
||||
usePunyCode(): boolean;
|
||||
}
|
||||
|
||||
export interface ISubDomainsGetter {
|
||||
|
||||
@@ -8,6 +8,10 @@ export abstract class AbstractDnsProvider<T = any> implements IDnsProvider<T> {
|
||||
http!: HttpClient;
|
||||
logger!: ILogger;
|
||||
|
||||
usePunyCode(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
setCtx(ctx: DnsProviderContext) {
|
||||
this.ctx = ctx;
|
||||
this.logger = ctx.logger;
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { IDomainParser, ISubDomainsGetter } from "./api";
|
||||
//@ts-ignore
|
||||
import psl from "psl";
|
||||
import { resolveDomainBySoaRecord } from "@certd/acme-client";
|
||||
import { logger, utils } from "@certd/basic";
|
||||
|
||||
export class DomainParser implements IDomainParser {
|
||||
subDomainsGetter: ISubDomainsGetter;
|
||||
@@ -17,16 +19,38 @@ export class DomainParser implements IDomainParser {
|
||||
}
|
||||
|
||||
async parse(fullDomain: string) {
|
||||
const subDomains = await this.subDomainsGetter.getSubDomains();
|
||||
if (subDomains && subDomains.length > 0) {
|
||||
for (const subDomain of subDomains) {
|
||||
if (fullDomain.endsWith(subDomain)) {
|
||||
//找到子域名托管
|
||||
return subDomain;
|
||||
}
|
||||
logger.info(`查找主域名:${fullDomain}`);
|
||||
const cacheKey = `domain_parse:${fullDomain}`;
|
||||
const value = utils.cache.get(cacheKey);
|
||||
if (value) {
|
||||
logger.info(`从缓存获取到主域名:${fullDomain}->${value}`);
|
||||
return value;
|
||||
}
|
||||
try {
|
||||
const mainDomain = await resolveDomainBySoaRecord(fullDomain);
|
||||
if (mainDomain) {
|
||||
utils.cache.set(cacheKey, mainDomain, {
|
||||
ttl: 2 * 60 * 1000,
|
||||
});
|
||||
logger.info(`获取到主域名:${fullDomain}->${mainDomain}`);
|
||||
return mainDomain;
|
||||
}
|
||||
} catch (e) {
|
||||
logger.error("从SOA获取主域名失败", e.message);
|
||||
}
|
||||
|
||||
return this.parseDomain(fullDomain);
|
||||
// const subDomains = await this.subDomainsGetter.getSubDomains();
|
||||
// if (subDomains && subDomains.length > 0) {
|
||||
// for (const subDomain of subDomains) {
|
||||
// if (fullDomain.endsWith(subDomain)) {
|
||||
// //找到子域名托管
|
||||
// return subDomain;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
const res = this.parseDomain(fullDomain);
|
||||
logger.info(`从psl获取主域名:${fullDomain}->${res}`);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user