Skip to content

Instantly share code, notes, and snippets.

@log2c
Last active April 7, 2023 12:00
Show Gist options
  • Save log2c/6d2aab87dfe560fbcec3fd3b8c7830bd to your computer and use it in GitHub Desktop.
Save log2c/6d2aab87dfe560fbcec3fd3b8c7830bd to your computer and use it in GitHub Desktop.
一些关于clash及surge 的脚本
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
}
@PaTTeeL
Copy link

PaTTeeL commented Apr 6, 2022

好像的确是因为过滤了443端口,导致过滤后的规则中如果有按地区分类的节点组(比如香港节点、台湾节点、狮城节点之类的)是空白,又被其他分组引用时(比如group[2]->自动选择),clash发现引用组的服务器是空白,所以报错了,而且因为clash for windows看不到内存中mixin后的配置文件,所以之前的报错也是要研究很久才能找到问题的。。。请教一下能否改进脚本避免出现报错的问题呢?
btw,查了一下函数indexOf,说是顺序检索,如果我仅填了443端口,类似4438和8443这样端口的节点是不是过滤之后还是会被保留在列表中呢?

@log2c
Copy link
Author

log2c commented Apr 8, 2022

好像的确是因为过滤了443端口,导致过滤后的规则中如果有按地区分类的节点组(比如香港节点、台湾节点、狮城节点之类的)是空白,又被其他分组引用时(比如group[2]->自动选择),clash发现引用组的服务器是空白,所以报错了,而且因为clash for windows看不到内存中mixin后的配置文件,所以之前的报错也是要研究很久才能找到问题的。。。请教一下能否改进脚本避免出现报错的问题呢? btw,查了一下函数indexOf,说是顺序检索,如果我仅填了443端口,类似4438和8443这样端口的节点是不是过滤之后还是会被保留在列表中呢?

更新了试试

@PaTTeeL
Copy link

PaTTeeL commented Apr 8, 2022

好像的确是因为过滤了443端口,导致过滤后的规则中如果有按地区分类的节点组(比如香港节点、台湾节点、狮城节点之类的)是空白,又被其他分组引用时(比如group[2]->自动选择),clash发现引用组的服务器是空白,所以报错了,而且因为clash for windows看不到内存中mixin后的配置文件,所以之前的报错也是要研究很久才能找到问题的。。。请教一下能否改进脚本避免出现报错的问题呢? btw,查了一下函数indexOf,说是顺序检索,如果我仅填了443端口,类似4438和8443这样端口的节点是不是过滤之后还是会被保留在列表中呢?

更新了试试

感谢大神!试过了果然有效,不再报错了。想有没有办法可以直接删除没有节点的地区分组吗?比如韩国节点筛选完没有443端口的,这一组直接去掉?暂时我修改了一下仅保留DIRECT在正常使用,只不过有点强迫症感觉挂在那里占空间。。。

@PaTTeeL
Copy link

PaTTeeL commented Apr 7, 2023

有了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