Last active
April 7, 2023 12:00
-
-
Save log2c/6d2aab87dfe560fbcec3fd3b8c7830bd to your computer and use it in GitHub Desktop.
一些关于clash及surge 的脚本
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports.parse = ({ content, name, url }, { yaml, axios, notify }) => { | |
const remove_names = new Set(); | |
const allowList = [80, 8080, 443, 27833, 22]; // 待保留端口的节点 | |
const fallbackProxy = ["DIRECT", "REJECT"]; // proxies为空时自动追加__"自动选择" | |
if (Array.isArray(content.proxies)) { | |
content.proxies = content.proxies | |
.filter((proxy => { | |
if (allowList.indexOf(proxy.port) === -1) { | |
remove_names.add(proxy.name); | |
return false; | |
} else { | |
return true; | |
} | |
})); | |
} | |
const groups = content['proxy-groups'] || []; | |
if (Array.isArray(groups)) { | |
groups.forEach((group) => { | |
const proxies = group.proxies | |
.filter((proxy) => !remove_names.has(proxy)); | |
group.proxies = proxies; | |
// update 2022/4/8 | |
if ((group.proxies || []).length === 0) { | |
group.proxies = [...fallbackProxy]; | |
} | |
}); | |
} | |
return content | |
} |
有了ChatGPT之后代码水平一日千里,分享一下从ChatGPT那里学来的代码:
const portList = [22,80,443,1080,8080,]; // 设置端口号匹配规则
const fallbackProxy = ['DIRECT','REJECT',]; // 分组无匹配结果的默认设置
content.proxies = content.proxies?.filter(proxy => portList.includes(proxy.port));
content['proxy-groups']?.forEach(group => {
group.proxies = group.proxies.filter( proxy =>
['DIRECT', 'REJECT', ...content.proxies?.map(proxy => proxy.name), ...content['proxy-groups'].map(group => group.name)].includes(proxy));
group.proxies = group.proxies.length ? group.proxies : fallbackProxy;
});
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
感谢大神!试过了果然有效,不再报错了。想有没有办法可以直接删除没有节点的地区分组吗?比如韩国节点筛选完没有443端口的,这一组直接去掉?暂时我修改了一下仅保留DIRECT在正常使用,只不过有点强迫症感觉挂在那里占空间。。。