mirror of
https://gh.catmak.name/https://github.com/mihomo-party-org/mihomo-party
synced 2025-12-26 20:50:30 +08:00
feat: add HTML detection and error handling for profile parsing
This commit is contained in:
parent
55416f32cd
commit
51d169d2e8
@ -306,9 +306,28 @@ export async function setProfileStr(id: string, content: string): Promise<void>
|
||||
|
||||
export async function getProfile(id: string | undefined): Promise<IMihomoConfig> {
|
||||
const profile = await getProfileStr(id)
|
||||
|
||||
// 检测是否为 HTML 内容(订阅返回错误页面)
|
||||
const trimmed = profile.trim()
|
||||
if (
|
||||
trimmed.startsWith('<!DOCTYPE') ||
|
||||
trimmed.startsWith('<html') ||
|
||||
trimmed.startsWith('<HTML') ||
|
||||
/<style[^>]*>/i.test(trimmed.slice(0, 500))
|
||||
) {
|
||||
throw new Error(
|
||||
`Profile "${id}" contains HTML instead of YAML. The subscription may have returned an error page. Please re-import or update the subscription.`
|
||||
)
|
||||
}
|
||||
|
||||
try {
|
||||
let result = parse(profile)
|
||||
if (typeof result !== 'object') result = {}
|
||||
return result as IMihomoConfig
|
||||
} catch (e) {
|
||||
const msg = e instanceof Error ? e.message : String(e)
|
||||
throw new Error(`Failed to parse profile "${id}": ${msg}`)
|
||||
}
|
||||
}
|
||||
|
||||
// attachment;filename=xxx.yaml; filename*=UTF-8''%xx%xx%xx
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user