public-data/clash.js

68 lines
2.8 KiB
JavaScript
Raw Permalink 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
2023-12-06 10:47:29 +08:00
let proxyGroupName = ''
let directGroupName = ''
2023-01-10 15:53:31 +08:00
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-12-06 10:47:29 +08:00
proxyGroupName = getProxyGroupName(obj)
directGroupName = getDirectGroupName(obj)
2023-01-10 15:53:31 +08:00
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
2023-12-06 10:47:29 +08:00
const prependDirectRules = directs.split('\n').map(item => getRuleItem(item, true))
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
2023-12-06 10:47:29 +08:00
const prependProxyRules = proxys.split('\n').map(item => getRuleItem(item, false))
2023-01-10 15:53:31 +08:00
const prependRules = [...prependDirectRules, ...prependProxyRules]
obj.rules = [...prependRules, ...obj.rules]
} catch (e) {
throw new Error(e)
}
}
2023-12-06 10:47:29 +08:00
function getRuleItem(match, direct) {
const groupName = direct ? directGroupName : proxyGroupName
2023-12-06 10:56:01 +08:00
const matchName = match.split('#')[0].trim()
2023-12-06 11:00:06 +08:00
if (isIPAddress(matchName)) {
2023-12-06 10:56:01 +08:00
return `IP-CIDR,${matchName}/24,${groupName},no-resolve`
2023-12-06 10:47:29 +08:00
}
2023-12-06 10:56:01 +08:00
return `DOMAIN-SUFFIX,${matchName},${groupName}`
2023-12-06 10:47:29 +08:00
}
2023-01-10 15:53:31 +08:00
// 获取代理组的名称
function getProxyGroupName(obj) {
2023-12-06 10:47:29 +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) {
2023-12-06 10:47:29 +08:00
const nodeSelectName = obj['proxy-groups'].find(item => ['全球直连'].some(name => item.name.includes(name)))
if (nodeSelectName) {
return nodeSelectName.name
}
2023-01-10 15:53:31 +08:00
return 'DIRECT'
}
2023-12-06 10:47:29 +08:00
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)
}