// 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 const obj = yaml.parse(raw) try { await configPrependRules(utils, obj) } catch (e) { throw new Error(e) } return yaml.stringify(obj) } // 配置预先规则 async function configPrependRules(utils, obj) { const { axios } = utils 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 => 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 => getRuleItem(item, false)) const prependRules = [...prependDirectRules, ...prependProxyRules] obj.rules = [...prependRules, ...obj.rules] } catch (e) { throw new Error(e) } } 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))) if (nodeSelectName) { return nodeSelectName.name } return obj['proxy-groups'].find(item => item.name.includes('手动切换')).name } // 获取直连组的名称 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) }