(Workaround) Ignoring invalid clash providers instead of raising errors (忽略无效 Clash Providers 避免抛异常)
aur3l14no opened this issue · 3 comments
aur3l14no commented
现版本某个 provider 失效会导致获取 provider 的过程整个终止,但很多时候我们希望在这种情况下能返回那些正常的 provider 并忽略无效的。
所以写了个丑丑的 workaround,如果其他人有这个需求可以参考下。
'use strict';
const { utils } = require('surgio');
const { getClashSubscription } = require('surgio/build/provider/ClashProvider');
const userAgent = 'Clash'
const airports = [
['YourAirPortName', 'https://example.com/sub/ilovexjp?clash=1'],
]
async function getClashNodes(name, url) {
try {
const { nodeList } = await getClashSubscription({ url: url, udpRelay: false, tls13: true, requestUserAgent: userAgent })
return nodeList.map(({ nodeName, ...rest }) => ({ nodeName: `[${name}] ${nodeName}`, ...rest }))
} catch (e) {
return []
}
}
async function getProvider() {
let res = await Promise.all(
airports.map(async ([name, url]) => getClashNodes(name, url))
)
return {
type: 'custom',
nodeList: res.flat(),
};
}
module.exports = getProvider;
geekdada commented
感谢回报。
我有个想法不知大家觉得怎么样。
module.exports = {
...,
onFetchError: () => {
return [] // 节点定义
},
}
lowking commented
感谢回报。
我有个想法不知大家觉得怎么样。
module.exports = { ..., onFetchError: () => { return [] // 节点定义 }, }
return [] 之后可以加个通知,告知哪个订阅异常,方便后续调整
RulesW commented
鉴于部分provider在拉取失效时实际仍然可用,我觉得这里或许可以考虑延长缓存有效期,或者可以这样
module.exports = {
...,
onFetchError: async (req, cache) => {
if (req.timeout && cache) {
return cache;
}
else {
return []; // Node definition
}
},
}