Skip to content

Commit 9b06271

Browse files
committed
feat: 订阅失败处理新增兜底通知和兜底静默
1 parent 8ffb62f commit 9b06271

5 files changed

Lines changed: 660 additions & 383 deletions

File tree

backend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sub-store",
3-
"version": "2.22.2",
3+
"version": "2.22.3",
44
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
55
"main": "src/main.js",
66
"scripts": {

backend/src/restful/download.js

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@ import { isIPv4, isIPv6 } from '@/utils';
1212
import { getISO } from '@/utils/geo';
1313
import env from '@/utils/env';
1414

15+
function buildEmptyNezhaPayload() {
16+
return JSON.stringify(
17+
{
18+
code: 0,
19+
message: 'success',
20+
result: [],
21+
},
22+
null,
23+
2,
24+
);
25+
}
26+
1527
export default function register($app) {
1628
$app.get('/share/col/:name/:target', async (req, res) => {
1729
const { target } = req.params;
@@ -89,7 +101,10 @@ async function downloadSubscription(req, res) {
89101
const useMihomoExternal = req.query.target === 'SurgeMac';
90102

91103
const platform =
92-
req.query.target || getPlatformFromHeaders(req.headers) || 'JSON';
104+
req.query.platform ||
105+
req.query.target ||
106+
getPlatformFromHeaders(req.headers) ||
107+
'JSON';
93108
const reqUA = req.headers['user-agent'] || req.headers['User-Agent'];
94109
$.info(
95110
`正在下载订阅:${name}\n请求 User-Agent: ${reqUA}\n请求 target: ${req.query.target}\n实际输出: ${platform}`,
@@ -340,14 +355,18 @@ async function downloadSubscription(req, res) {
340355
if (resultFormat === 'nezha') {
341356
output = nezhaTransform(output);
342357
} else if (resultFormat === 'nezha-monitor') {
343-
nezhaIndex = /^\d+$/.test(nezhaIndex)
344-
? parseInt(nezhaIndex, 10)
345-
: output.findIndex((i) => i.name === nezhaIndex);
346-
output = await nezhaMonitor(
347-
output[nezhaIndex],
348-
nezhaIndex,
349-
req.query,
350-
);
358+
if (!Array.isArray(output) || output.length === 0) {
359+
output = buildEmptyNezhaPayload();
360+
} else {
361+
nezhaIndex = /^\d+$/.test(nezhaIndex)
362+
? parseInt(nezhaIndex, 10)
363+
: output.findIndex((i) => i.name === nezhaIndex);
364+
output = await nezhaMonitor(
365+
output[nezhaIndex],
366+
nezhaIndex,
367+
req.query,
368+
);
369+
}
351370
}
352371
res.set('Content-Type', 'application/json;charset=utf-8');
353372
} else {
@@ -403,7 +422,10 @@ async function downloadCollection(req, res) {
403422
const useMihomoExternal = req.query.target === 'SurgeMac';
404423

405424
const platform =
406-
req.query.target || getPlatformFromHeaders(req.headers) || 'JSON';
425+
req.query.platform ||
426+
req.query.target ||
427+
getPlatformFromHeaders(req.headers) ||
428+
'JSON';
407429

408430
const allCols = $.read(COLLECTIONS_KEY);
409431
const collection = findByName(allCols, name);
@@ -627,14 +649,18 @@ async function downloadCollection(req, res) {
627649
if (resultFormat === 'nezha') {
628650
output = nezhaTransform(output);
629651
} else if (resultFormat === 'nezha-monitor') {
630-
nezhaIndex = /^\d+$/.test(nezhaIndex)
631-
? parseInt(nezhaIndex, 10)
632-
: output.findIndex((i) => i.name === nezhaIndex);
633-
output = await nezhaMonitor(
634-
output[nezhaIndex],
635-
nezhaIndex,
636-
req.query,
637-
);
652+
if (!Array.isArray(output) || output.length === 0) {
653+
output = buildEmptyNezhaPayload();
654+
} else {
655+
nezhaIndex = /^\d+$/.test(nezhaIndex)
656+
? parseInt(nezhaIndex, 10)
657+
: output.findIndex((i) => i.name === nezhaIndex);
658+
output = await nezhaMonitor(
659+
output[nezhaIndex],
660+
nezhaIndex,
661+
req.query,
662+
);
663+
}
638664
}
639665
res.set('Content-Type', 'application/json;charset=utf-8');
640666
} else {
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { ProxyUtils } from '@/core/proxy-utils';
2+
3+
const IGNORE_FAILED_REMOTE_SUB_NOTIFY_MODES = new Set([
4+
'enabled',
5+
'fallbackNotify',
6+
]);
7+
const IGNORE_FAILED_REMOTE_SUB_FALLBACK_MODES = new Set([
8+
'fallbackNotify',
9+
'fallbackQuiet',
10+
]);
11+
12+
function normalizeIgnoreFailedRemoteSub(mode) {
13+
if (mode === true) return 'quiet';
14+
if (mode === false || mode == null || mode === '') return 'disabled';
15+
return mode;
16+
}
17+
18+
function resolveIgnoreFailedRemoteSubMode(...values) {
19+
for (const value of values) {
20+
if (value != null && value !== '') {
21+
return normalizeIgnoreFailedRemoteSub(value);
22+
}
23+
}
24+
25+
return 'disabled';
26+
}
27+
28+
function shouldNotifyIgnoreFailedRemoteSub(mode) {
29+
return IGNORE_FAILED_REMOTE_SUB_NOTIFY_MODES.has(
30+
normalizeIgnoreFailedRemoteSub(mode),
31+
);
32+
}
33+
34+
function shouldFallbackIgnoreFailedRemoteSub(mode) {
35+
return IGNORE_FAILED_REMOTE_SUB_FALLBACK_MODES.has(
36+
normalizeIgnoreFailedRemoteSub(mode),
37+
);
38+
}
39+
40+
function handleIgnoreFailedRemoteSubError({ mode, message, notify }) {
41+
const resolvedMode = normalizeIgnoreFailedRemoteSub(mode);
42+
43+
if (
44+
resolvedMode === 'disabled' ||
45+
shouldFallbackIgnoreFailedRemoteSub(resolvedMode)
46+
) {
47+
throw new Error(message);
48+
}
49+
50+
if (shouldNotifyIgnoreFailedRemoteSub(resolvedMode)) {
51+
notify?.(message);
52+
}
53+
}
54+
55+
function notifyIgnoreFailedRemoteSubFallback({ mode, notify, error }) {
56+
if (!shouldNotifyIgnoreFailedRemoteSub(mode)) return;
57+
notify?.(error);
58+
}
59+
60+
function buildEmptySubscriptionOutput({
61+
platform = 'JSON',
62+
produceType,
63+
produceOpts = {},
64+
}) {
65+
if (produceType === 'raw') {
66+
return JSON.stringify([]);
67+
}
68+
69+
return ProxyUtils.produce([], platform, produceType, produceOpts);
70+
}
71+
72+
export {
73+
buildEmptySubscriptionOutput,
74+
handleIgnoreFailedRemoteSubError,
75+
normalizeIgnoreFailedRemoteSub,
76+
notifyIgnoreFailedRemoteSubFallback,
77+
resolveIgnoreFailedRemoteSubMode,
78+
shouldFallbackIgnoreFailedRemoteSub,
79+
shouldNotifyIgnoreFailedRemoteSub,
80+
};

0 commit comments

Comments
 (0)