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:33:59 +08:00
const { axios,console } = utils
2023-01-10 15:53:31 +08:00
const proxyGroupName = getProxyGroupName(obj)
const directGroupName = getDirectGroupName(obj)
try {
const directResponse = await axios.get('https://git.yevpt.com/vpt/public-data/raw/branch/master/direct')
const directs = directResponse.data
const prependDirectRules = directs.split(`\n`).map(item => `DOMAIN-SUFFIX,${item},${directGroupName}`)
const proxyResponse = await axios.get('https://git.yevpt.com/vpt/public-data/raw/branch/master/proxy')
const proxys = proxyResponse.data
2023-01-10 16:33:59 +08:00
console.log('proxys',proxys.split(`\n`))
2023-01-10 15:53:31 +08:00
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-10 15:58:47 +08:00
const nodeSelectName = obj['proxy-groups'].find(item => item.name.includes('节点选择'))
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'
}