public-data/clash.js

47 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-01-10 15:53:31 +08:00
// https://docs.cfw.lbyczf.com/contents/parser.html#%E8%BF%9B%E9%98%B6%E6%96%B9%E6%B3%95-javascript
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) {
2023-01-10 16:43:45 +08:00
const { axios } = utils
2023-01-10 15:53:31 +08:00
const proxyGroupName = getProxyGroupName(obj)
const directGroupName = getDirectGroupName(obj)
try {
2023-01-10 16:43:45 +08:00
const noCacheDate = '?nocachekey=' + Date.now()
const directResponse = await axios.get('https://git.yevpt.com/vpt/public-data/raw/branch/master/direct' + noCacheDate)
2023-01-10 15:53:31 +08:00
const directs = directResponse.data
const prependDirectRules = directs.split(`\n`).map(item => `DOMAIN-SUFFIX,${item},${directGroupName}`)
2023-01-10 16:43:45 +08:00
const proxyResponse = await axios.get('https://git.yevpt.com/vpt/public-data/raw/branch/master/proxy' + noCacheDate)
2023-01-10 15:53:31 +08:00
const proxys = proxyResponse.data
const prependProxyRules = proxys.split(`\n`).map(item => `DOMAIN-SUFFIX,${item},${proxyGroupName}`)
const prependRules = [...prependDirectRules, ...prependProxyRules]
obj.rules = [...prependRules, ...obj.rules]
} catch (e) {
throw new Error(e)
}
}
// 获取代理组的名称
function getProxyGroupName(obj) {
2023-01-18 00:25:39 +08:00
const nodeSelectName = obj['proxy-groups'].find(item => ['节点选择','Proxies'].some(name => item.name.includes(name)))
2023-01-10 15:58:47 +08:00
if (nodeSelectName) {
return nodeSelectName.name
}
return obj['proxy-groups'].find(item => item.name.includes('手动切换')).name
2023-01-10 15:53:31 +08:00
}
// 获取直连组的名称
function getDirectGroupName(obj) {
return 'DIRECT'
}