fix(user-secret): 白名单「获取当前 IP」改走服务端回显,第三方回显只作兜底

无影云桌面等受限网络里 myip.ipip.net 不可达,页面却能正常调自家接口,
导致青果白名单拿不到本机出口 IP。新增 /api/user-secrets/client-ip
回显服务端看到的来源 IP(XFF 逐跳取首个公网地址),前端优先用它,
失败再退回第三方回显。
This commit is contained in:
2026-10-30 17:07:32 +08:00
parent 1c0cc350be
commit c7b035dce9
10 changed files with 184 additions and 6 deletions
+1
View File
@@ -213,6 +213,7 @@ export const API_ENDPOINTS = {
proxyBalance: '/api/user-secrets/proxy-balance',
proxyPlan: '/api/user-secrets/proxy-plan',
proxyWhitelist: '/api/user-secrets/proxy-whitelist',
clientIp: '/api/user-secrets/client-ip',
},
costGuard: {
status: '/api/user/cost-guard/status',
@@ -165,3 +165,10 @@ export function addProxyWhitelist(ip: string) {
),
)
}
/** 服务端看到的调用方公网 IP:受限网络(无影桌面等)连不上第三方回显时的主通道。 */
export function fetchClientIp() {
return unwrapJavaResponse(
get<JavaApiResponse<{ ip: string }>>(buildJavaUrl(API_ENDPOINTS.userSecret.clientIp)),
)
}
@@ -2,9 +2,10 @@ import { computed, ref, type ComputedRef, type Ref } from 'vue'
import { ElMessage } from 'element-plus'
import {
addProxyWhitelist,
fetchClientIp,
type UserProxyWhitelistResult,
} from '@/shared/api/types/modules/user-secret'
import { fetchPublicIp } from '@/shared/utils/public-ip'
import { resolvePublicIp } from '@/shared/utils/public-ip'
import type { ProxyMode } from '@/shared/bridges/pywebview'
import type { ProxyProviderKey } from '@/shared/utils/api-secret-channel'
@@ -86,7 +87,8 @@ export function useProxyWhitelist(deps: ProxyWhitelistDeps) {
whitelistFetching.value = true
whitelistError.value = ''
whitelistResult.value = null
fetchPublicIp()
// 服务端回显优先:无影一类受限网络里第三方回显不可达,页面本身却能正常调自家接口
resolvePublicIp(async () => (await fetchClientIp()).ip)
.then((ip) => {
whitelistIp.value = ip
})
+15 -2
View File
@@ -1,8 +1,9 @@
/**
* 客户端公网 IP 获取:用户在「代理 → 白名单」里点「获取当前 IP」时使用。
*
* 口径:myip.ipip.net 是极客帮助文档里的测试目标,且带 CORS(Access-Control-Allow-Origin: *),
* 浏览器/pywebview 可直接请求;解析失败时返回 null 由用户手填,绝不静默写一个猜的 IP。
* 口径:先问自家服务端要它看到的来源 IP(受限网络里第三方回显常被拦,例如阿里云无影桌面),
* 拿不到再退回 myip.ipip.net(带 CORS,Access-Control-Allow-Origin: *);
* 解析失败时返回 null 由用户手填,绝不静默写一个猜的 IP。
*/
const PUBLIC_IP_ENDPOINT = 'https://myip.ipip.net'
const IPV4_TOKEN = /(?:^|[^\d.])(\d{1,3}(?:\.\d{1,3}){3})(?![\d.])/
@@ -22,6 +23,18 @@ export function parsePublicIp(text: unknown): string | null {
return candidate
}
/** 先取服务端回显,失败或拿不到合法 IPv4 时再走公网回显;两个出口都失败才抛错。 */
export async function resolvePublicIp(
fetchFromServer: () => Promise<string | null>,
fetchFromEcho: () => Promise<string> = () => fetchPublicIp(),
): Promise<string> {
// 服务端通道失败(未登录 / 网关拦截)不是终点:只把结果折成 null,交给公网回显兜底
const serverIp = await fetchFromServer()
.then((value) => parsePublicIp(value))
.catch(() => null)
return serverIp ?? fetchFromEcho()
}
/** 请求公网 IP 回显服务;超时/网络/解析失败都会抛错,由调用方给出可见提示。 */
export async function fetchPublicIp(timeoutMs = 8000): Promise<string> {
if (typeof fetch !== 'function') {