@@ -12,6 +12,18 @@ import { isIPv4, isIPv6 } from '@/utils';
1212import { getISO } from '@/utils/geo' ;
1313import 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+
1527export 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 {
0 commit comments