@@ -11,12 +11,8 @@ import {
1111} from "@subboost/core/subscription/import-error" ;
1212import { tryNormalizeSubscriptionUrlInput } from "@subboost/core/subscription/url-input" ;
1313import type { ParseResult , ParsedNode } from "@subboost/core/types/node" ;
14-
15- export const SUBSCRIPTION_IMPORT_USER_AGENTS = [
16- "v2rayN/7.20.4" ,
17- "mihomo/1.19.24" ,
18- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36" ,
19- ] as const ;
14+ import { shouldTryClashMetaForV2raynPayload } from "./fetch-profile-heuristics" ;
15+ import { SUBSCRIPTION_IMPORT_USER_AGENTS } from "./user-agents" ;
2016
2117export type SourceImportPurpose = "content" | "userinfo" ;
2218
@@ -105,6 +101,22 @@ function isUsableParsedAttempt(attempt: ParsedAttempt): attempt is Extract<Parse
105101 return attempt . ok && attempt . parsed . nodes . length > 0 && ! looksLikeClientUpdatePlaceholderNodes ( attempt . parsed . nodes ) ;
106102}
107103
104+ function shouldContinueAfterCleanAttempt ( params : {
105+ attempt : ParsedAttempt ;
106+ currentUserAgent : string ;
107+ nextUserAgent ?: string ;
108+ } ) : boolean {
109+ const { attempt, currentUserAgent, nextUserAgent } = params ;
110+ if ( ! isUsableParsedAttempt ( attempt ) || attempt . parsed . errors . length > 0 ) return true ;
111+ if (
112+ currentUserAgent === SUBSCRIPTION_IMPORT_USER_AGENTS [ 0 ] &&
113+ nextUserAgent === SUBSCRIPTION_IMPORT_USER_AGENTS [ 1 ]
114+ ) {
115+ return shouldTryClashMetaForV2raynPayload ( attempt . content , attempt . parsed ) ;
116+ }
117+ return false ;
118+ }
119+
108120function toFailure ( attempt : ParsedAttempt | null , fallback = "获取 url 失败" ) : SourceImportFailure {
109121 if ( ! attempt ) {
110122 return {
@@ -264,14 +276,23 @@ export async function importSubscriptionFromUrl(
264276 const userAgents = options . userAgents ?. length ? options . userAgents : SUBSCRIPTION_IMPORT_USER_AGENTS ;
265277 let best : ParsedAttempt | null = null ;
266278
267- for ( const userAgent of userAgents ) {
279+ for ( let index = 0 ; index < userAgents . length ; index += 1 ) {
280+ const userAgent = userAgents [ index ] ;
268281 const attempt = await fetchAndParseWithUserAgent ( url , userAgent , {
269282 timeoutMs,
270283 maxBytes,
271284 fetchText : options . fetchText ,
272285 } ) ;
273286 best = pickBetterAttempt ( best , attempt ) ;
274- if ( isUsableParsedAttempt ( attempt ) && attempt . parsed . errors . length === 0 ) break ;
287+ if (
288+ ! shouldContinueAfterCleanAttempt ( {
289+ attempt,
290+ currentUserAgent : userAgent ,
291+ nextUserAgent : userAgents [ index + 1 ] ,
292+ } )
293+ ) {
294+ break ;
295+ }
275296 }
276297
277298 if ( ! best || ! best . ok || ! isUsableParsedAttempt ( best ) ) {
0 commit comments