UNPKG

tencentcloud-edgeone-migration-nodejs-v2

Version:

tencentcloud cdn config copy to edgeone

227 lines (213 loc) 7.92 kB
const _ = require("lodash"); const { ActionEnum, standardMatchFromEnum, enterpriseMatchFromEnum, operatorEnum, standardKeys, enterpriseKeys, MatchToOperation, } = require("../getPlansUtils/const"); const { CountryRegionId, AreaRegionId } = require("../regionId"); const { t } = require("../../i18n/trans"); const genLog = require("../../logGenerator"); function AclConfig(domain, domainConfig, eoPlanType, ruleTransferLog) { if ( !domainConfig || domainConfig.Switch === "off" || !domainConfig.AdvancedScriptData ) { ruleTransferLog.push({ config: t("防火墙规则(AclConfig)"), result: t("未配置"), detail: "", }); genLog.defaultLog(`${t("未配置")}${t("防火墙规则(AclConfig)")}`); return null; } try { const nonsupportFieldInfo = { RuleName: "", nonsupportFieldList: [], }; // 筛选出整条不匹配规则 const targetList = domainConfig.AdvancedScriptData.filter( (AdvancedScriptDataItem) => { const temp = AdvancedScriptDataItem.Configure.map((item) => { if (eoPlanType !== "enterprise") { if (!standardKeys.includes(item.MatchKey)) { nonsupportFieldInfo.nonsupportFieldList.push(item.MatchKey); nonsupportFieldInfo.RuleName = AdvancedScriptDataItem.RuleName; return false; } } else { if (!enterpriseKeys.includes(item.MatchKey)) { nonsupportFieldInfo.nonsupportFieldList.push(item.MatchKey); nonsupportFieldInfo.RuleName = AdvancedScriptDataItem.RuleName; return false; } } }); return !temp.includes(false); } ); if (nonsupportFieldInfo.nonsupportFieldList.length !== 0) { const nonsupportFields = _.uniq( nonsupportFieldInfo.nonsupportFieldList ).join(); const unSupportFiledMsg = t( `匹配类型 {{nonsupportFields}} 不支持,未完成迁移,请人工协助`, { nonsupportFields } ); genLog.warnLog(unSupportFiledMsg); ruleTransferLog.push({ config: t(`防火墙规则(AclConfig): {{RuleName}}`, { RuleName: nonsupportFieldInfo.RuleName, }), result: t("失败"), detail: unSupportFiledMsg, }); } // 筛选出整条不匹配条件 const targetListWithFilterOptions = targetList.filter( (AdvancedScriptDataItem) => { const messageInfo = { RuleName: "", messageList: [], }; const temp = AdvancedScriptDataItem.Configure.map((item) => { if ( (item.MatchKey === "protocol" || item.MatchKey === "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.MatchKey === "referer" || item.MatchKey === "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.MatchKey === "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(`防火墙规则(AclConfig): {{RuleName}}`, { RuleName: messageInfo.RuleName, }), result: t("失败"), detail: messageInfo.messageList.join("; "), }); } return !temp.includes(false); } ); if (targetListWithFilterOptions.length === 0) { return null; } const AclUserRules = targetListWithFilterOptions.map( (AdvancedScriptDataItem) => { ruleTransferLog.push({ config: t(`防火墙规则(AclConfig): {{RuleName}}`, { RuleName: AdvancedScriptDataItem, }), result: t("成功"), detail: "", }); return { AclConditions: AdvancedScriptDataItem.Configure.map((item) => { const operator = MatchToOperation[item?.MatchKey]?.[item?.LogicOperator] || operatorEnum[item.LogicOperator]; let matchContent = ""; if (item.MatchValue instanceof Array) { if (item.MatchKey === "ipArea") { matchContent = ( item.MatchValue.map((item) => AreaRegionId[item]) || [] ).join(); } else if (item.MatchKey === "ipCountry") { matchContent = ( item.MatchValue.map((item) => CountryRegionId[item]) || [] ).join(); } else { matchContent = item.MatchValue.join().toLowerCase(); } } else { matchContent = (item.MatchValue || "").toLowerCase(); } if (operator === "file_suffix_match") { if (matchContent[0] !== ".") { matchContent = `.${matchContent}`; } } return { MatchContent: matchContent, MatchFrom: (eoPlanType === "enterprise" ? enterpriseMatchFromEnum[item.MatchKey] : standardMatchFromEnum[item.MatchKey]) || "", MatchParam: item.MatchKey === "cookie" ? "" : item.MatchKeyParam || "", Operator: operator, }; }), RuleName: AdvancedScriptDataItem.RuleName, Action: ActionEnum[AdvancedScriptDataItem.Result], RuleStatus: AdvancedScriptDataItem.Status === "active" ? "on" : "off", RulePriority: 50, ...(AdvancedScriptDataItem.Result !== "intercept" && { RedirectUrl: AdvancedScriptDataItem.ErrorPage.RedirectUrl, }), }; } ); return { AclUserRules }; } catch (e) { genLog.warnLog( t(`防火墙规则迁移失败: {{result}}`, { result: e.toString() }) ); ruleTransferLog.push({ config: t(`防火墙规则(AclConfig)`), result: t("失败"), detail: `${e.toString()}`, }); return null; } } module.exports = AclConfig;