tencentcloud-edgeone-migration-nodejs-v2
Version:
tencentcloud cdn config copy to edgeone
263 lines (248 loc) • 9.28 kB
JavaScript
const _ = require("lodash");
const {
ActionEnum,
standardMatchFromEnum,
enterpriseMatchFromEnum,
operatorEnum,
standardKeys,
enterpriseKeys,
MatchToOperation,
} = require("../getPlansUtils/const");
const { t } = require("../../i18n/trans");
const genLog = require("../../logGenerator");
function RateLimitConfig(domain, domainConfig, eoPlanType, ruleTransferLog) {
try {
if (
!domainConfig ||
domainConfig.Switch === "off" ||
!domainConfig.AdvancedRules
) {
ruleTransferLog.push({
config: t("DDoS防护-单节点限频(RateLimitConfig)"),
result: t("未配置"),
detail: "",
});
genLog.defaultLog(
`${t("未配置")}${t("DDoS防护-单节点限频(RateLimitConfig)")}`
);
return null;
}
const nonsupportFieldInfo = {
RuleName: "",
nonsupportFieldList: [],
};
// 筛选出整条不匹配规则
const targetList = domainConfig.AdvancedRules.filter(
(AdvancedScriptDataItem) => {
const temp = AdvancedScriptDataItem.Configure.map((item) => {
if (eoPlanType !== "enterprise") {
if (!standardKeys.includes(item.RuleType)) {
nonsupportFieldInfo.nonsupportFieldList.push(item.RuleType);
nonsupportFieldInfo.RuleName = AdvancedScriptDataItem.RuleName;
return false;
}
} else {
if (!enterpriseKeys.includes(item.RuleType)) {
nonsupportFieldInfo.nonsupportFieldList.push(item.RuleType);
nonsupportFieldInfo.RuleName = AdvancedScriptDataItem.RuleName;
return false;
}
}
});
return !temp.includes(false);
}
);
if (nonsupportFieldInfo.nonsupportFieldList.length !== 0) {
const nonsupportFields = _.uniq(
nonsupportFieldInfo.nonsupportFieldList
).join();
const unSupportFieldsMsg = t(
`匹配类型 {{nonsupportFields}} 不支持,未完成迁移,请人工协助`,
{ nonsupportFields }
);
genLog.warnLog(unSupportFieldsMsg);
ruleTransferLog.push({
config: t(`DDoS防护-单节点限频(RateLimitConfig): {{RuleName}}`, {
RuleName: nonsupportFieldInfo.RuleName,
}),
result: t("失败"),
detail: unSupportFieldsMsg,
});
}
// 筛选出整条不匹配条件
const targetListWithFilterOptions = targetList.filter(
(AdvancedScriptDataItem) => {
const messageInfo = {
RuleName: "",
messageList: [],
};
const temp = AdvancedScriptDataItem.Configure.map((item) => {
if (
item.RuleType === "method" &&
item?.LogicOperator === "notequal"
) {
const unSupportOptionsMsg = [
t(`单节点限频:EdgeOne 内的「请求方式」仅支持等于,不支持迁移`),
t(`EdgeOne 内的「请求方式」仅支持等于,不支持迁移`),
];
messageInfo.RuleName = AdvancedScriptDataItem.RuleName;
messageInfo.messageList.push(unSupportOptionsMsg[1]);
genLog.warnLog(unSupportOptionsMsg);
return false;
}
if (
item.RuleType === "protocol" &&
item?.LogicOperator === "notequal"
) {
const unSupportOptionsMsg = [
t(`单节点限频:EdgeOne 内的「应用层协议」仅支持等于,不支持迁移`),
t(`EdgeOne 内的「应用层协议」仅支持等于,不支持迁移`),
];
messageInfo.RuleName = AdvancedScriptDataItem.RuleName;
messageInfo.messageList.push(unSupportOptionsMsg[1]);
genLog.warnLog(unSupportOptionsMsg);
return false;
}
if (
(item.RuleType === "referer" || item.RuleType === "directory") &&
item?.LogicOperator === "matching"
) {
const unSupportOptionsMsg = [
t(
`单节点限频:EdgeOne 内的「referer」或「请求路径」(Path)不支持前缀匹配,不支持迁移。`
),
t(
`EdgeOne 内的「referer」或「请求路径」(Path)不支持前缀匹配,不支持迁移`
),
];
messageInfo.RuleName = AdvancedScriptDataItem.RuleName;
messageInfo.messageList.push(unSupportOptionsMsg[1]);
genLog.warnLog(unSupportOptionsMsg);
return false;
}
if (item.RuleType === "head" && item?.LogicOperator === "null") {
const unSupportOptionsMsg = [
t(
`单节点限频:EdgeOne 内的「自定义请求头」不支持同时匹配「存在或不存在」,仅支持其中一种,请人工迁移`
),
t(
`EdgeOne 内的「自定义请求头」不支持同时匹配「存在或不存在」,仅支持其中一种,请人工迁移`
),
];
messageInfo.RuleName = AdvancedScriptDataItem.RuleName;
messageInfo.messageList.push(unSupportOptionsMsg[1]);
genLog.warnLog(unSupportOptionsMsg);
return false;
}
return item;
});
if (messageInfo.messageList.length > 0) {
ruleTransferLog.push({
config: t(`DDoS防护-单节点限频(RateLimitConfig):{{RuleName}}`, {
RuleName: messageInfo.RuleName,
}),
result: t("失败"),
detail: messageInfo.messageList.join("; "),
});
genLog.errorLog(
`${t(`DDoS防护-单节点限频(RateLimitConfig):{{RuleName}}`, {
RuleName: messageInfo.RuleName,
})}${t("失败")}`
);
}
return !temp.includes(false);
}
);
if (targetListWithFilterOptions.length === 0) {
return null;
}
const RateLimitUserRules = targetListWithFilterOptions
.map((AdvancedRulesItem) => {
if (AdvancedRulesItem.Action === "redirect") {
genLog.warnLog(
t(`{{RuleName}} 未完成迁移`, {
RuleName: AdvancedRulesItem.RuleName,
})
);
ruleTransferLog.push({
config: t(`DDoS防护-单节点限频(RateLimitConfig):{{RuleName}}`, {
RuleName: AdvancedRulesItem.RuleName,
}),
result: t("失败"),
detail: t(`{{RuleName}} 未完成迁移`, {
RuleName: AdvancedRulesItem.RuleName,
}),
});
return;
}
ruleTransferLog.push({
config: t(`DDoS防护-单节点限频(RateLimitConfig):{{RuleName}}`, {
RuleName: AdvancedRulesItem.RuleName,
}),
result: t("成功"),
detail: "",
});
return {
AclConditions: AdvancedRulesItem.Configure.map((item) => {
let matchContent =
item.RuleValue instanceof Array
? item.RuleValue.join().toLowerCase()
: (item.RuleValue || "").toLowerCase();
const operator =
MatchToOperation[item?.RuleType]?.[item?.LogicOperator] ||
operatorEnum[item.LogicOperator];
if (operator === "file_suffix_match") {
if (matchContent[0] !== ".") {
matchContent = `.${matchContent}`;
}
}
return {
MatchContent: matchContent,
MatchFrom:
(eoPlanType === "enterprise"
? enterpriseMatchFromEnum[item.RuleType]
: standardMatchFromEnum[item.RuleType]) || "",
MatchParam:
item.RuleType === "cookie" ? "" : item.RuleParam || "",
Operator: operator,
};
}),
RuleName: AdvancedRulesItem.RuleName,
Action: ActionEnum[AdvancedRulesItem.Action],
RuleStatus: AdvancedRulesItem.Switch,
Threshold: AdvancedRulesItem.FrequencyLimit,
FreqScope: ["source_to_eo"],
Period:
AdvancedRulesItem.DetectionTime >= 60
? 60
: Math.ceil(AdvancedRulesItem.DetectionTime / 10) * 10,
PunishTime: !AdvancedRulesItem.PunishmentTime
? 0
: AdvancedRulesItem.PunishmentTime >= 120
? 120
: AdvancedRulesItem.PunishmentTime,
PunishTimeUnit: "second",
RulePriority: 50,
};
})
.filter((item) => !!item);
if (RateLimitUserRules.filter((item) => !!item).length) {
return {
Switch: "on",
RateLimitUserRules,
};
}
throw new Error("单节点限频配置均不符合 EO 要求,该配置迁移失败");
} catch (e) {
ruleTransferLog.push({
config: t("DDoS防护-单节点限频(RateLimitConfig)"),
result: t("失败"),
detail: `${e.toString()}`,
});
genLog.errorLog(
t(`DDoS防护-单节点限频迁移失败: {{result}}`, { result: e.toString() })
);
return null;
}
}
module.exports = RateLimitConfig;