From 34103b3ebadd03738929a04efedaac818c6523a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=9D=A4=E6=98=AF=E7=BA=B1=E9=9B=BE=E9=85=B1=E5=93=9F?= =?UTF-8?q?=EF=BD=9E?= <49941141+Dragon1573@users.noreply.github.com> Date: Mon, 23 Nov 2026 07:33:51 +0000 Subject: [PATCH] fix(home): resolve proxy auth for IP info card (#8708) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The IP info card now routes lookups through the live mixed listener. Mihomo's /configs endpoint reports authentication as usernames only, so the previous code split an entry on ":" and threw when the separator was absent. With authentication enabled through the Global Extend Config or a merge, the card errored before any request. Recover the password from the generated runtime config by matching the username reported by /configs and pass credentials through the plugin's proxy basicAuth option. Fall back to a plain proxy request instead of failing outright when the credentials cannot be resolved. Assisted-by (Code Edits): DeepSeek v4.1 Flash 1017 (High) Assisted-by (Commit Messages): DeepSeek v4 Pro 0813 (High) Signed-off-by: ❤是纱雾酱哟~ <49941141+Dragon1573@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- Changelog.md | 2 +- src/services/api.ts | 91 ++++++++++++++++++++++++++++++++++----------- 2 files changed, 70 insertions(+), 23 deletions(-) diff --git a/Changelog.md b/Changelog.md index e3f99781a..7db3a6e3b 100644 --- a/Changelog.md +++ b/Changelog.md @@ -41,7 +41,7 @@ - 修复「TUN 设置」未手动保存时,界面显示的默认 MTU 1500 未生效 - 修复「链式代理」节点列表按名称排序,未保持订阅中的节点顺序 - 修复规则模式下 Muse AI 解锁检测显示错误的出口地区 -- 修复使用系统代理时 IP 信息显示直连地址 +- 修复使用系统代理或本地代理认证时首页 IP 信息显示直连地址或报错 **🖥️ Windows** diff --git a/src/services/api.ts b/src/services/api.ts index f0877673d..343ab2e61 100644 --- a/src/services/api.ts +++ b/src/services/api.ts @@ -8,6 +8,8 @@ import { getBaseConfig } from 'tauri-plugin-mihomo-api' import { debugLog } from '@/utils/debug' +import { getRuntimeConfig } from './cmds' + const getUserAgentPromise = once(async () => { try { const [name, version] = await Promise.all([getName(), getVersion()]) @@ -161,6 +163,72 @@ const getProxyHost = (allowLan: boolean, bindAddress: string) => { return isIpv6 ? `[${address}]` : address } +interface ProxyBasicAuth { + username: string + password: string +} + +const parseProxyBasicAuth = (entry: string): ProxyBasicAuth | undefined => { + const separator = entry.indexOf(':') + if (separator < 0) return undefined + return { + username: entry.slice(0, separator), + password: entry.slice(separator + 1), + } +} + +// `/configs` only exposes usernames, so the password has to come from the +// generated runtime config. +const resolveProxyBasicAuth = async ( + enabledAuthentication: string[], +): Promise => { + const inline = enabledAuthentication + .map(parseProxyBasicAuth) + .find((auth) => auth !== undefined) + if (inline) return inline + + try { + const runtimeConfig = (await getRuntimeConfig()) as + | (IConfigData & { authentication?: unknown }) + | null + const runtimeAuthentication = Array.isArray(runtimeConfig?.authentication) + ? runtimeConfig.authentication + : [] + const storedAuthentication = runtimeAuthentication + .filter((entry): entry is string => typeof entry === 'string') + .map(parseProxyBasicAuth) + .filter((auth): auth is ProxyBasicAuth => auth !== undefined) + + return ( + storedAuthentication.find((auth) => + enabledAuthentication.includes(auth.username), + ) ?? storedAuthentication[0] + ) + } catch (error) { + debugLog( + 'Failed to read the runtime config for local proxy authentication', + error, + ) + return undefined + } +} + +const resolveIpProxy = async () => { + const { mixedPort, allowLan, bindAddress, authentication } = + await getBaseConfig() + const proxyUrl = `http://${getProxyHost(allowLan, bindAddress)}:${mixedPort}` + const enabledAuthentication = authentication ?? [] + + if (enabledAuthentication.length === 0) { + return { all: proxyUrl } + } + + const basicAuth = await resolveProxyBasicAuth(enabledAuthentication) + return { + all: basicAuth ? { url: proxyUrl, basicAuth } : proxyUrl, + } +} + // 获取当前IP和地理位置信息 export const getIpInfo = async (): Promise< IpInfo & { lastFetchTs: number } @@ -174,28 +242,7 @@ export const getIpInfo = async (): Promise< ) let lastError: unknown | null = null const userAgent = await getUserAgentPromise() - const { mixedPort, allowLan, bindAddress, authentication } = - await getBaseConfig() - const proxyUrl = `http://${getProxyHost(allowLan, bindAddress)}:${mixedPort}` - const proxyAuthentication = authentication?.[0] - const authSeparator = proxyAuthentication?.indexOf(':') ?? -1 - - if (proxyAuthentication !== undefined && authSeparator < 0) { - throw new Error('Invalid mixed proxy authentication configuration') - } - - const proxy = { - all: - proxyAuthentication === undefined - ? proxyUrl - : { - url: proxyUrl, - basicAuth: { - username: proxyAuthentication.slice(0, authSeparator), - password: proxyAuthentication.slice(authSeparator + 1), - }, - }, - } + const proxy = await resolveIpProxy() console.debug('User-Agent for IP detection:', userAgent) for (const service of shuffledServices) {