mirror of
https://github.com/clash-verge-rev/clash-verge-rev.git
synced 2026-11-23 21:12:28 +08:00
fix(home): resolve proxy auth for IP info card (#8708)
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>
This commit is contained in:
+69
-22
@@ -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<ProxyBasicAuth | undefined> => {
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user