From 9fd3af86b0f49f6e25b5244b13990bc86c61a26c Mon Sep 17 00:00:00 2001 From: vpt Date: Wed, 6 Dec 2023 10:47:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20clash.js?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- clash.js | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/clash.js b/clash.js index 0373367..b390edf 100644 --- a/clash.js +++ b/clash.js @@ -1,4 +1,6 @@ // https://docs.cfw.lbyczf.com/contents/parser.html#%E8%BF%9B%E9%98%B6%E6%96%B9%E6%B3%95-javascript +let proxyGroupName = '' +let directGroupName = '' module.exports.parse = async(raw, utils, meta) => { const { axios, yaml, notify, console } = utils const { name, url, interval, selected } = meta @@ -14,16 +16,16 @@ module.exports.parse = async(raw, utils, meta) => { // 配置预先规则 async function configPrependRules(utils, obj) { const { axios } = utils - const proxyGroupName = getProxyGroupName(obj) - const directGroupName = getDirectGroupName(obj) + proxyGroupName = getProxyGroupName(obj) + directGroupName = getDirectGroupName(obj) try { const noCacheDate = '?nocachekey=' + Date.now() const directResponse = await axios.get('https://git.yevpt.com/vpt/public-data/raw/branch/master/direct' + noCacheDate) const directs = directResponse.data - const prependDirectRules = directs.split(`\n`).map(item => `DOMAIN-SUFFIX,${item},${directGroupName}`) + const prependDirectRules = directs.split('\n').map(item => getRuleItem(item, true)) const proxyResponse = await axios.get('https://git.yevpt.com/vpt/public-data/raw/branch/master/proxy' + noCacheDate) const proxys = proxyResponse.data - const prependProxyRules = proxys.split(`\n`).map(item => `DOMAIN-SUFFIX,${item},${proxyGroupName}`) + const prependProxyRules = proxys.split('\n').map(item => getRuleItem(item, false)) const prependRules = [...prependDirectRules, ...prependProxyRules] obj.rules = [...prependRules, ...obj.rules] } catch (e) { @@ -31,9 +33,18 @@ async function configPrependRules(utils, obj) { } } +function getRuleItem(match, direct) { + const groupName = direct ? directGroupName : proxyGroupName + if (isIPAddress(match)) { + return `IP-CIDR,${match}/24,${groupName},no-resolve` + } else { + return `DOMAIN-SUFFIX,${match},${proxyGroupName}` + } +} + // 获取代理组的名称 function getProxyGroupName(obj) { - const nodeSelectName = obj['proxy-groups'].find(item => ['节点选择','Proxies'].some(name => item.name.includes(name))) + const nodeSelectName = obj['proxy-groups'].find(item => ['节点选择', 'Proxies'].some(name => item.name.includes(name))) if (nodeSelectName) { return nodeSelectName.name } @@ -42,5 +53,15 @@ function getProxyGroupName(obj) { // 获取直连组的名称 function getDirectGroupName(obj) { + const nodeSelectName = obj['proxy-groups'].find(item => ['全球直连'].some(name => item.name.includes(name))) + if (nodeSelectName) { + return nodeSelectName.name + } return 'DIRECT' } + +function isIPAddress(str) { + const ipv4Regex = /^(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.((25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)\.){2}(25[0-5]|2[0-4][0-9]|[0-1]?[0-9][0-9]?)$/ + const ipv6Regex = /^([0-9a-fA-F]{1,4}:){7,7}[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,7}:$|^([0-9a-fA-F]{1,4}:){1,6}:[0-9a-fA-F]{1,4}$|^([0-9a-fA-F]{1,4}:){1,5}(:[0-9a-fA-F]{1,4}){1,2}$|^([0-9a-fA-F]{1,4}:){1,4}(:[0-9a-fA-F]{1,4}){1,3}$|^([0-9a-fA-F]{1,4}:){1,3}(:[0-9a-fA-F]{1,4}){1,4}$|^([0-9a-fA-F]{1,4}:){1,2}(:[0-9a-fA-F]{1,4}){1,5}$|^[0-9a-fA-F]{1,4}:((:[0-9a-fA-F]{1,4}){1,6})?$/ + return ipv4Regex.test(str) || ipv6Regex.test(str) +}