@@ -2,7 +2,7 @@ import { SingboxConfigBuilder } from './SingboxConfigBuilder.js';
22import { generateHtml } from './htmlBuilder.js' ;
33import { ClashConfigBuilder } from './ClashConfigBuilder.js' ;
44import { SurgeConfigBuilder } from './SurgeConfigBuilder.js' ;
5- import { decodeBase64 , encodeBase64 , GenerateWebPath } from './utils.js' ;
5+ import { encodeBase64 , GenerateWebPath , tryDecodeSubscriptionLines } from './utils.js' ;
66import { PREDEFINED_RULE_SETS } from './config.js' ;
77import { t , setLanguage } from './i18n/index.js' ;
88import yaml from 'js-yaml' ;
@@ -156,38 +156,48 @@ async function handleRequest(request) {
156156 } else if ( url . pathname . startsWith ( '/xray' ) ) {
157157 // Handle Xray config requests
158158 const inputString = url . searchParams . get ( 'config' ) ;
159- const proxylist = inputString . split ( '\n' ) ;
159+ if ( ! inputString ) {
160+ return new Response ( 'Missing config parameter' , { status : 400 } ) ;
161+ }
160162
163+ const proxylist = inputString . split ( '\n' ) ;
161164 const finalProxyList = [ ] ;
162165 // Use custom UserAgent (for Xray) Hmmm...
163166 let userAgent = url . searchParams . get ( 'ua' ) ;
164167 if ( ! userAgent ) {
165168 userAgent = 'curl/7.74.0' ;
166169 }
167- let headers = new Headers ( {
168- " User-Agent" : userAgent
170+ const headers = new Headers ( {
171+ ' User-Agent' : userAgent
169172 } ) ;
170173
171174 for ( const proxy of proxylist ) {
172- if ( proxy . startsWith ( 'http://' ) || proxy . startsWith ( 'https://' ) ) {
175+ const trimmedProxy = proxy . trim ( ) ;
176+ if ( ! trimmedProxy ) {
177+ continue ;
178+ }
179+
180+ if ( trimmedProxy . startsWith ( 'http://' ) || trimmedProxy . startsWith ( 'https://' ) ) {
173181 try {
174- const response = await fetch ( proxy , {
175- method : 'GET' ,
176- headers : headers
177- } )
182+ const response = await fetch ( trimmedProxy , {
183+ method : 'GET' ,
184+ headers
185+ } ) ;
178186 const text = await response . text ( ) ;
179- let decodedText ;
180- decodedText = decodeBase64 ( text . trim ( ) ) ;
181- // Check if the decoded text needs URL decoding
182- if ( decodedText . includes ( '%' ) ) {
183- decodedText = decodeURIComponent ( decodedText ) ;
187+ let processed = tryDecodeSubscriptionLines ( text , { decodeUriComponent : true } ) ;
188+ if ( ! Array . isArray ( processed ) ) {
189+ processed = [ processed ] ;
184190 }
185- finalProxyList . push ( ...decodedText . split ( '\n ') ) ;
191+ finalProxyList . push ( ...processed . filter ( item => typeof item === 'string' && item . trim ( ) !== ' ') ) ;
186192 } catch ( e ) {
187193 console . warn ( 'Failed to fetch the proxy:' , e ) ;
188194 }
189195 } else {
190- finalProxyList . push ( proxy ) ;
196+ let processed = tryDecodeSubscriptionLines ( trimmedProxy ) ;
197+ if ( ! Array . isArray ( processed ) ) {
198+ processed = [ processed ] ;
199+ }
200+ finalProxyList . push ( ...processed . filter ( item => typeof item === 'string' && item . trim ( ) !== '' ) ) ;
191201 }
192202 }
193203
@@ -292,4 +302,4 @@ async function handleRequest(request) {
292302 console . error ( 'Error processing request:' , error ) ;
293303 return new Response ( t ( 'internalError' ) , { status : 500 } ) ;
294304 }
295- }
305+ }
0 commit comments