mirror of https://github.com/certd/certd
chore: parseDomain优化
parent
11801d8e2e
commit
32be489136
|
@ -1,12 +1,15 @@
|
||||||
import { IDomainParser, ISubDomainsGetter } from "./api";
|
import { IDomainParser, ISubDomainsGetter } from "./api";
|
||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
import psl from "psl";
|
import psl from "psl";
|
||||||
import { logger, utils } from "@certd/basic";
|
import { ILogger, utils, logger as globalLogger } from "@certd/basic";
|
||||||
|
import { resolveDomainBySoaRecord } from "@certd/acme-client";
|
||||||
|
|
||||||
export class DomainParser implements IDomainParser {
|
export class DomainParser implements IDomainParser {
|
||||||
subDomainsGetter: ISubDomainsGetter;
|
subDomainsGetter: ISubDomainsGetter;
|
||||||
constructor(subDomainsGetter: ISubDomainsGetter) {
|
logger: ILogger;
|
||||||
|
constructor(subDomainsGetter: ISubDomainsGetter, logger?: ILogger) {
|
||||||
this.subDomainsGetter = subDomainsGetter;
|
this.subDomainsGetter = subDomainsGetter;
|
||||||
|
this.logger = logger || globalLogger;
|
||||||
}
|
}
|
||||||
|
|
||||||
parseDomainByPsl(fullDomain: string) {
|
parseDomainByPsl(fullDomain: string) {
|
||||||
|
@ -18,42 +21,46 @@ export class DomainParser implements IDomainParser {
|
||||||
}
|
}
|
||||||
|
|
||||||
async parse(fullDomain: string) {
|
async parse(fullDomain: string) {
|
||||||
logger.info(`查找主域名:${fullDomain}`);
|
this.logger.info(`查找主域名:${fullDomain}`);
|
||||||
const cacheKey = `domain_parse:${fullDomain}`;
|
const cacheKey = `domain_parse:${fullDomain}`;
|
||||||
const value = utils.cache.get(cacheKey);
|
const value = utils.cache.get(cacheKey);
|
||||||
if (value) {
|
if (value) {
|
||||||
logger.info(`从缓存获取到主域名:${fullDomain}->${value}`);
|
this.logger.info(`从缓存获取到主域名:${fullDomain}->${value}`);
|
||||||
return 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);
|
|
||||||
// }
|
|
||||||
|
|
||||||
const subDomains = await this.subDomainsGetter.getSubDomains();
|
const subDomains = await this.subDomainsGetter.getSubDomains();
|
||||||
if (subDomains && subDomains.length > 0) {
|
if (subDomains && subDomains.length > 0) {
|
||||||
|
const fullDomainDot = "." + fullDomain;
|
||||||
for (const subDomain of subDomains) {
|
for (const subDomain of subDomains) {
|
||||||
if (fullDomain.endsWith(subDomain)) {
|
if (fullDomainDot.endsWith("." + subDomain)) {
|
||||||
//找到子域名托管
|
//找到子域名托管
|
||||||
utils.cache.set(cacheKey, subDomain, {
|
utils.cache.set(cacheKey, subDomain, {
|
||||||
ttl: 2 * 60 * 1000,
|
ttl: 60 * 1000,
|
||||||
});
|
});
|
||||||
logger.info(`获取到子域名托管域名:${fullDomain}->${subDomain}`);
|
this.logger.info(`获取到子域名托管域名:${fullDomain}->${subDomain}`);
|
||||||
return subDomain;
|
return subDomain;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const res = this.parseDomainByPsl(fullDomain);
|
const res = this.parseDomainByPsl(fullDomain);
|
||||||
logger.info(`从psl获取主域名:${fullDomain}->${res}`);
|
this.logger.info(`从psl获取主域名:${fullDomain}->${res}`);
|
||||||
|
|
||||||
|
let soaManDomain = null;
|
||||||
|
try {
|
||||||
|
const mainDomain = await resolveDomainBySoaRecord(fullDomain);
|
||||||
|
if (mainDomain) {
|
||||||
|
this.logger.info(`从SOA获取到主域名:${fullDomain}->${mainDomain}`);
|
||||||
|
soaManDomain = mainDomain;
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
this.logger.error("从SOA获取主域名失败", e.message);
|
||||||
|
}
|
||||||
|
if (soaManDomain && soaManDomain !== res) {
|
||||||
|
this.logger.warn(`SOA获取的主域名(${soaManDomain})和psl获取的主域名(${res})不一致,请确认是否有设置子域名托管`);
|
||||||
|
}
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,7 +28,7 @@ export abstract class CertApplyBaseConvertPlugin extends AbstractTaskPlugin {
|
||||||
"2、子域名被通配符包含的不要填写,例如:www.foo.com已经被*.foo.com包含,不要填写www.foo.com\n" +
|
"2、子域名被通配符包含的不要填写,例如:www.foo.com已经被*.foo.com包含,不要填写www.foo.com\n" +
|
||||||
"3、泛域名只能通配*号那一级(*.foo.com的证书不能用于xxx.yyy.foo.com、不能用于foo.com)\n" +
|
"3、泛域名只能通配*号那一级(*.foo.com的证书不能用于xxx.yyy.foo.com、不能用于foo.com)\n" +
|
||||||
"4、输入一个,空格之后,再输入下一个 \n" +
|
"4、输入一个,空格之后,再输入下一个 \n" +
|
||||||
"5、如果你配置了子域托管解析,请先[设置托管子域名](#/certd/pipeline/subDomain)",
|
"5、如果您配置了子域托管解析,请先[设置托管子域名](#/certd/pipeline/subDomain)",
|
||||||
})
|
})
|
||||||
domains!: string[];
|
domains!: string[];
|
||||||
|
|
||||||
|
|
|
@ -327,7 +327,7 @@ HTTP文件验证:不支持泛域名,需要配置网站文件上传`,
|
||||||
}
|
}
|
||||||
this.eab = eab;
|
this.eab = eab;
|
||||||
const subDomainsGetter = await this.ctx.serviceGetter.get<ISubDomainsGetter>("subDomainsGetter");
|
const subDomainsGetter = await this.ctx.serviceGetter.get<ISubDomainsGetter>("subDomainsGetter");
|
||||||
const domainParser = new DomainParser(subDomainsGetter);
|
const domainParser = new DomainParser(subDomainsGetter, this.logger);
|
||||||
this.acme = new AcmeService({
|
this.acme = new AcmeService({
|
||||||
userId: this.ctx.user.id,
|
userId: this.ctx.user.id,
|
||||||
userContext: this.userContext,
|
userContext: this.userContext,
|
||||||
|
|
|
@ -61,7 +61,7 @@ export class CommonDnsProvider implements IDnsProvider {
|
||||||
domain: options.recordReq.domain,
|
domain: options.recordReq.domain,
|
||||||
hostRecord: options.recordReq.hostRecord,
|
hostRecord: options.recordReq.hostRecord,
|
||||||
recordValue: options.recordReq.value,
|
recordValue: options.recordReq.value,
|
||||||
recordId: options.recordRes.recordId,
|
recordId: options.recordRes?.recordId,
|
||||||
providerId: this.config.id,
|
providerId: this.config.id,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue