chore: 优化域名match

pull/229/head
xiaojunnuo 2024-10-25 17:47:39 +08:00
parent b421798a1b
commit 1cc1d1c03c
5 changed files with 116 additions and 4 deletions

View File

@ -0,0 +1,11 @@
# 阿里云相关
## 阿里云客户端请求超时配置
配置环境变量
```shell
ALIYUN_CLIENT_CONNECT_TIMEOUT=10000 # 连接超时,单位毫秒
ALIYUN_CLIENT_READ_TIMEOUT=10000 #读取数据超时,单位毫秒
```

View File

@ -17,6 +17,8 @@ import { fileUtils } from './util.file.js';
import * as _ from 'lodash-es'; import * as _ from 'lodash-es';
import { cache } from './util.cache.js'; import { cache } from './util.cache.js';
import dayjs from 'dayjs'; import dayjs from 'dayjs';
import { domainUtils } from './util.domain.js';
import { optionsUtils } from './util.options.js';
export const utils = { export const utils = {
sleep, sleep,
@ -30,4 +32,6 @@ export const utils = {
cache, cache,
nanoid, nanoid,
dayjs, dayjs,
domain: domainUtils,
options: optionsUtils,
}; };

View File

@ -0,0 +1,51 @@
//域名是否匹配,支持通配符
function match(targetDomains: string | string[], inDomains: string[]) {
if (!targetDomains || targetDomains.length == 0) {
return false;
}
if (!inDomains || inDomains.length == 0) {
return false;
}
if (typeof targetDomains === 'string') {
targetDomains = [targetDomains];
}
for (let targetDomain of targetDomains) {
let matched = false;
if (targetDomain.startsWith('.')) {
targetDomain = '*' + targetDomain;
}
for (let inDomain of inDomains) {
if (inDomain.startsWith('.')) {
inDomain = '*' + inDomain;
}
if (targetDomain === inDomain) {
matched = true;
break;
}
if (!inDomain.startsWith('*.')) {
//不可能匹配
break;
}
//子域名匹配通配符即可
if (targetDomain === inDomain.substring(2)) {
matched = true;
break;
}
}
//有一个没有匹配上,就失败
if (matched) {
//这个匹配上了,检查下一个
break;
} else {
return false;
}
}
//没有提前return 全部匹配上了
return true;
}
export const domainUtils = {
match,
};

View File

@ -0,0 +1,42 @@
import { domainUtils } from './util.domain.js';
function groupByDomain(options: any[], inDomains: string[]) {
const matched = [];
const notMatched = [];
for (const item of options) {
if (domainUtils.match(item.domain, inDomains)) {
matched.push(item);
} else {
notMatched.push(item);
}
}
return {
matched,
notMatched,
};
}
function buildGroupOptions(options: any[], inDomains: string[]) {
const grouped = groupByDomain(options, inDomains);
const groupOptions = [];
groupOptions.push({ value: '', disabled: true, label: '----已匹配----' });
if (grouped.matched.length === 0) {
options.push({ value: '', disabled: true, label: '没有可以匹配的域名' });
} else {
for (const matched of grouped.matched) {
groupOptions.push(matched);
}
}
if (grouped.notMatched.length > 0) {
groupOptions.push({ value: '', disabled: true, label: '----未匹配----' });
for (const notMatched of grouped.notMatched) {
groupOptions.push(notMatched);
}
}
return groupOptions;
}
export const optionsUtils = {
groupByDomain,
buildGroupOptions,
};

View File

@ -38,19 +38,23 @@ function findStepFromPipeline(targetStepId: string) {
} }
const errorRef = ref(""); const errorRef = ref("");
function getDomainFromPipeline(inputKey: string) { function getStepIdFromInputKey(inputKey: string) {
if (!inputKey) { if (!inputKey) {
errorRef.value = "请先选择域名证书"; errorRef.value = "请先选择域名证书";
return; return;
} }
const targetStepId = inputKey.split(".")[1]; return inputKey.split(".")[1];
}
function getDomainFromPipeline(inputKey: string) {
let targetStepId = getStepIdFromInputKey(inputKey);
let certStep = findStepFromPipeline(targetStepId); let certStep = findStepFromPipeline(targetStepId);
if (!certStep) { if (!certStep) {
errorRef.value = "找不到目标步骤,请先选择域名证书"; errorRef.value = "找不到目标步骤,请先选择域名证书";
return; return;
} }
if (certStep.type !== "CertApply" || certStep.type !== "CertApplyLego") { if (certStep.type !== "CertApply" && certStep.type !== "CertApplyLego") {
certStep = findStepFromPipeline(certStep.input?.cert); targetStepId = getStepIdFromInputKey(certStep.input?.cert);
certStep = findStepFromPipeline(targetStepId);
if (!certStep) { if (!certStep) {
errorRef.value = "找不到目标步骤,请先选择域名证书"; errorRef.value = "找不到目标步骤,请先选择域名证书";
return; return;