42 lines
1.5 KiB
JavaScript
42 lines
1.5 KiB
JavaScript
|
// 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) {
|
||
|
const { axios } = utils
|
||
|
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
|
||
|
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) {
|
||
|
return obj['proxy-groups'].find(item => item.name.includes('节点选择')).name
|
||
|
}
|
||
|
|
||
|
// 获取直连组的名称
|
||
|
function getDirectGroupName(obj) {
|
||
|
return 'DIRECT'
|
||
|
}
|