feat(user-secret): 代理白名单一键添加(极客/青果)并前置默认渠道

- 客户端设置面板新增「白名单」区块:自动获取出口 IP、可手改;加入前先同步未上报的代理配置,避免拿服务端旧 Key 代加
- 后端新增 POST /api/user-secrets/proxy-whitelist:先查后加;极客用套餐 ID+套餐 key,青果用已保存提取链接里的 key,重复添加不报错
- 极客套餐 key 加密存 biz_user_proxy_plan.jikip_plan_key(V181),回读只给掩码;换套餐 ID 时必须重填
- 白名单接口地址走配置(AIIMAGE_USER_SECRET_JIKIP/QINGGUO_WHITE_LIST_*),日志不落 key 与完整 URL
- 贞贞/极客等默认选项前置;catch 棘轮 7 处逐条记名并入基线(含 79b04afd 的 AdminApiGuardFilter 一枚)
This commit is contained in:
2026-10-30 11:05:55 +08:00
parent 79b04afdd9
commit 43ae12f722
30 changed files with 1206 additions and 40 deletions
+1
View File
@@ -212,6 +212,7 @@ export const API_ENDPOINTS = {
migrate: '/api/user-secrets/migrate',
proxyBalance: '/api/user-secrets/proxy-balance',
proxyPlan: '/api/user-secrets/proxy-plan',
proxyWhitelist: '/api/user-secrets/proxy-whitelist',
},
costGuard: {
status: '/api/user/cost-guard/status',
@@ -61,9 +61,19 @@ export interface UserApiSecretBalance {
export interface UserProxyPlan {
planId: string | null
jikipUserId: string | null
/** 是否已保存套餐 key(白名单添加用);服务端只回显掩码。 */
jikipPlanKeyConfigured?: boolean
jikipPlanKeyMasked?: string | null
configured: boolean
}
export interface UserProxyWhitelistResult {
ip: string
provider: string
added: boolean
message: string
}
export interface UserApiSecretMigrateItem {
moduleKey: string
value: string
@@ -127,11 +137,15 @@ export function fetchProxyPlan() {
)
}
export function saveProxyPlan(planId: string, jikipUserId: string) {
export function saveProxyPlan(planId: string, jikipUserId: string, jikipPlanKey?: string) {
const body: { planId: string; jikipUserId: string; jikipPlanKey?: string } = { planId, jikipUserId }
const key = (jikipPlanKey || '').trim()
// 留空表示保留服务端已存 key;套餐 ID 变了仍留空时由服务端清空旧 key
if (key) body.jikipPlanKey = key
return unwrapJavaResponse(
put<JavaApiResponse<UserProxyPlan>, { planId: string; jikipUserId: string }>(
put<JavaApiResponse<UserProxyPlan>, typeof body>(
buildJavaUrl(API_ENDPOINTS.userSecret.proxyPlan),
{ planId, jikipUserId },
body,
),
)
}
@@ -141,3 +155,13 @@ export function clearProxyPlan() {
del<JavaApiResponse<null>>(buildJavaUrl(API_ENDPOINTS.userSecret.proxyPlan)),
)
}
/** 把当前出口 IP 加入代理白名单:极客用套餐 ID+key,青果用已保存链接里的 key。 */
export function addProxyWhitelist(ip: string) {
return unwrapJavaResponse(
post<JavaApiResponse<UserProxyWhitelistResult>, { ip: string }>(
buildJavaUrl(API_ENDPOINTS.userSecret.proxyWhitelist),
{ ip },
),
)
}
@@ -172,7 +172,7 @@
</div>
<div v-if="proxySupported && proxyProvider === 'jikip'" class="retention-block">
<div class="field-label">jikip 套餐信息(用于余量查询与预警)</div>
<div class="field-label">jikip 套餐信息(余量查询 / 白名单添加)</div>
<div class="proxy-plan-row">
<input
v-model="proxyPlanId"
@@ -193,6 +193,17 @@
:disabled="proxyPlanLoading || proxyPlanSaving"
/>
</div>
<div class="proxy-plan-row proxy-plan-row--single">
<input
v-model="proxyPlanKey"
class="secret-input"
type="password"
:placeholder="proxyPlanKeyPlaceholder"
autocomplete="off"
spellcheck="false"
:disabled="proxyPlanLoading || proxyPlanSaving"
/>
</div>
<div class="secret-actions">
<button
type="button"
@@ -213,13 +224,51 @@
</button>
</div>
<div class="proxy-plan-hint">
填写后,下方余量按你自己的 jikip 套餐查询;留空则显示平台默认套餐。套餐 ID / 用户 ID 在 jikip 后台「我的套餐」和「账户信息」查看。
填写后,下方余量按你自己的 jikip 套餐查询;套餐 key 仅用于白名单添加,保存后只显示掩码,留空保存则保留已存值。套餐 ID / 用户 ID / key 在 jikip 后台「我的套餐」「账户信息」「套餐列表」查看。
</div>
</div>
<div v-else-if="proxySupported && proxyProvider === 'qingguo'" class="proxy-plan-hint">
青果的账户余量与通道用量请到青果后台查看;本客户端目前只做青果提取链接的连通性检测。
</div>
<div v-if="whitelistVisible" class="retention-block">
<div class="field-label">白名单(把本机出口 IP 加到{{ whitelistProviderLabel }})</div>
<div class="proxy-plan-row">
<input
v-model="whitelistIp"
class="secret-input"
type="text"
placeholder="当前出口 IP,如 36.251.68.43"
autocomplete="off"
spellcheck="false"
:disabled="whitelistAdding"
@input="resetWhitelistStatus"
/>
<button
type="button"
class="check-btn"
:disabled="whitelistFetching || whitelistAdding"
@click="fetchCurrentIp"
>
{{ whitelistFetching ? '获取中...' : '获取当前 IP' }}
</button>
</div>
<div class="secret-actions">
<button
type="button"
class="check-btn"
:disabled="!whitelistReady || whitelistAdding"
@click="addToWhitelist"
>
{{ whitelistAdding ? '加入中...' : '加入白名单' }}
</button>
<span v-if="whitelistStatusText" class="check-result" :class="whitelistResultClass">
{{ whitelistStatusText }}
</span>
</div>
<div class="proxy-plan-hint">{{ whitelistHint }}</div>
</div>
<div class="secret-actions">
<button
type="button"
@@ -248,7 +297,7 @@
</template>
<script setup lang="ts">
import { computed, onMounted, onUnmounted, reactive, ref } from 'vue'
import { computed, onMounted, onUnmounted, reactive, ref, watch } from 'vue'
import { ElMessage } from 'element-plus'
import {
checkApiSecret,
@@ -280,7 +329,16 @@ import {
type ProxyProviderKey,
} from '@/shared/utils/api-secret-channel'
import { getPywebviewApi, type ProxyMode, type DesktopConfigUpdate } from '@/shared/bridges/pywebview'
import { deleteMyApiSecret, putMyApiSecret, fetchProxyPlan, saveProxyPlan, clearProxyPlan } from '@/shared/api/types/modules/user-secret'
import {
addProxyWhitelist,
deleteMyApiSecret,
putMyApiSecret,
fetchProxyPlan,
saveProxyPlan,
clearProxyPlan,
type UserProxyWhitelistResult,
} from '@/shared/api/types/modules/user-secret'
import { fetchPublicIp } from '@/shared/utils/public-ip'
import { PROXY_PLAN_CHANGED_EVENT } from '@/shared/utils/proxy-balance-alert'
const props = withDefaults(
@@ -339,10 +397,19 @@ const proxyCheckError = ref('')
const proxyPlanId = ref('')
const proxyPlanUserId = ref('')
const proxyPlanKey = ref('')
const proxyPlanKeyConfigured = ref(false)
const proxyPlanKeyMasked = ref<string | null>(null)
const proxyPlanConfigured = ref(false)
const proxyPlanLoading = ref(false)
const proxyPlanSaving = ref(false)
const whitelistIp = ref('')
const whitelistFetching = ref(false)
const whitelistAdding = ref(false)
const whitelistResult = ref<UserProxyWhitelistResult | null>(null)
const whitelistError = ref('')
const proxyPlaceholder = computed(() =>
proxyProvider.value === 'qingguo'
? 'https://overseas.proxy.qg.net/get?key=...&num=1&format=json'
@@ -369,6 +436,52 @@ const proxyResultClass = computed(() => {
}
})
/** 白名单区:青果的 API 鉴权白名单始终相关;极客只有白名单授权(本地代理模式 1)才需要。 */
const whitelistVisible = computed(() =>
proxyReady.value
&& (proxyProvider.value === 'qingguo' || (proxyProvider.value === 'jikip' && proxyMode.value === 1)),
)
const whitelistProviderLabel = computed(() => (proxyProvider.value === 'qingguo' ? '青果' : '极客'))
const whitelistReady = computed(() => {
if (!whitelistVisible.value || !whitelistIp.value.trim() || proxyDirty.value) return false
if (!proxyUrl.value.trim()) return false
if (proxyProvider.value === 'jikip') {
return proxyPlanConfigured.value && proxyPlanKeyConfigured.value
}
return true
})
const whitelistHint = computed(() => {
if (proxyDirty.value) {
return '代理设置有未保存的修改:先保存代理设置,再加入白名单(服务端按已保存的配置代加)。'
}
if (!proxyUrl.value.trim()) {
return '先在上方填写并保存代理提取链接,再加入白名单(服务端按已保存的配置代加)。'
}
if (proxyProvider.value === 'qingguo') {
return '青果用已保存的代理提取链接里的 Key 代加白名单,立即生效;刚改过提取链接时先保存代理设置。'
}
if (!proxyPlanKeyConfigured.value) {
return '先在下方「极客套餐信息」填写套餐 ID 和套餐 key(极客「套餐列表」里查看),再点加入白名单。'
}
return '极客白名单 1-2 分钟内生效;换网络或公网 IP 变化后需要重新加入。'
})
const proxyPlanKeyPlaceholder = computed(() => (
proxyPlanKeyConfigured.value
? `套餐 key 已保存(${proxyPlanKeyMasked.value || '****'}),输入新值可覆盖`
: '套餐 key(白名单添加用,极客套餐列表里查看)'
))
const whitelistStatusText = computed(() => whitelistError.value || whitelistResult.value?.message || '')
const whitelistResultClass = computed(() => ({
'check-result--ok': Boolean(whitelistResult.value) && !whitelistError.value,
'check-result--fail': Boolean(whitelistError.value),
}))
/** 切换代理供应商:只改本地选择,保存时与地址一起写入(检测也会按新供应商解析响应)。 */
function selectProxyProvider(provider: ProxyProviderKey) {
if (proxyProvider.value === provider) return
@@ -376,6 +489,70 @@ function selectProxyProvider(provider: ProxyProviderKey) {
proxyDirty.value = true
proxyCheckResult.value = null
proxyCheckError.value = ''
whitelistResult.value = null
whitelistError.value = ''
}
/** 自动填当前出口 IP;失败只提示可手填,不写任何猜测值。 */
function fetchCurrentIp() {
if (whitelistFetching.value) return
whitelistFetching.value = true
whitelistError.value = ''
whitelistResult.value = null
fetchPublicIp()
.then((ip) => {
whitelistIp.value = ip
})
.catch((error) => {
whitelistError.value = error instanceof Error ? error.message : '获取当前 IP 失败,请手动填写'
})
.finally(() => {
whitelistFetching.value = false
})
}
function resetWhitelistStatus() {
whitelistResult.value = null
whitelistError.value = ''
}
/** 加入白名单:服务端按已存代理配置选供应商;极客用套餐 ID+key,青果用链接里的 key。 */
function addToWhitelist() {
if (whitelistAdding.value) return
const ip = whitelistIp.value.trim()
if (!ip) {
ElMessage.warning('请先获取或填写当前出口 IP')
return
}
if (proxyProvider.value === 'jikip' && !proxyPlanKeyConfigured.value) {
ElMessage.warning('请先在极客套餐信息里填写套餐 key')
return
}
whitelistAdding.value = true
whitelistError.value = ''
whitelistResult.value = null
// 老版本只把代理存在本机:代加前先把当前链接+供应商同步到服务端,否则服务端没有 Key 可代加
const ensureServerConfig = proxyUrl.value.trim() && !proxyLocked.value
? syncProxyToServer(proxyUrl.value.trim())
: Promise.resolve(false)
ensureServerConfig
.then((synced) => {
// 上报失败时不能拿服务端旧链接里的 Key 代加;toast 已由 syncProxyToServer 给出
if (proxyUrl.value.trim() && !proxyLocked.value && !synced) return null
return addProxyWhitelist(ip)
})
.then((result) => {
if (!result) return
whitelistResult.value = result
ElMessage.success(result.message || '已加入白名单')
})
.catch((error) => {
whitelistError.value = error instanceof Error ? error.message : '加入白名单失败'
ElMessage.error(whitelistError.value)
})
.finally(() => {
whitelistAdding.value = false
})
}
/** 检测代理连通性:地址有改动时检测输入值(不落库),否则检测/落库已存值并刷新状态展示。 */
@@ -690,8 +867,8 @@ async function loadProxyConfig() {
}
/** 代理配置上报服务端(后台密钥管理观测/检测):有地址则保存、清空则删除;失败不阻断本地保存。 */
async function syncProxyToServer(url: string) {
if (proxyUserId() === '0') return
async function syncProxyToServer(url: string): Promise<boolean> {
if (proxyUserId() === '0') return false
try {
if (url) {
// 供应商随地址一起上报:服务端检测要按供应商解析提取响应
@@ -699,9 +876,11 @@ async function syncProxyToServer(url: string) {
} else {
await deleteMyApiSecret('proxy')
}
return true
} catch (error) {
console.warn('[api-secret] 代理配置上报失败(不影响本地任务):', error)
ElMessage.warning('代理已保存到本地,但上报后台失败')
return false
}
}
@@ -719,6 +898,8 @@ async function loadProxyPlan() {
if (!result) return
proxyPlanId.value = result.planId || ''
proxyPlanUserId.value = result.jikipUserId || ''
proxyPlanKeyConfigured.value = Boolean(result.jikipPlanKeyConfigured)
proxyPlanKeyMasked.value = result.jikipPlanKeyMasked || null
proxyPlanConfigured.value = Boolean(result.configured)
} finally {
proxyPlanLoading.value = false
@@ -733,14 +914,24 @@ async function saveProxyPlanInfo() {
return
}
proxyPlanSaving.value = true
const hadKey = proxyPlanKeyConfigured.value
try {
const result = await saveProxyPlan(planId, jikipUserId).catch((error) => {
const result = await saveProxyPlan(planId, jikipUserId, proxyPlanKey.value).catch((error) => {
ElMessage.error(error instanceof Error ? error.message : '套餐信息保存失败')
return null
})
if (!result) return
proxyPlanConfigured.value = Boolean(result.configured)
ElMessage.success('套餐信息已保存,余量将按你的套餐查询')
proxyPlanKeyConfigured.value = Boolean(result.jikipPlanKeyConfigured)
proxyPlanKeyMasked.value = result.jikipPlanKeyMasked || null
proxyPlanKey.value = ''
whitelistResult.value = null
whitelistError.value = ''
if (hadKey && !result.jikipPlanKeyConfigured) {
ElMessage.warning('套餐 ID 已变更,旧套餐 key 已清空,请重新填写')
} else {
ElMessage.success('套餐信息已保存,余量将按你的套餐查询')
}
window.dispatchEvent(new Event(PROXY_PLAN_CHANGED_EVENT))
await loadBalance()
} finally {
@@ -759,7 +950,12 @@ async function clearProxyPlanInfo() {
if (!cleared) return
proxyPlanId.value = ''
proxyPlanUserId.value = ''
proxyPlanKey.value = ''
proxyPlanKeyConfigured.value = false
proxyPlanKeyMasked.value = null
proxyPlanConfigured.value = false
whitelistResult.value = null
whitelistError.value = ''
ElMessage.success('已清除套餐信息,余量恢复为平台默认套餐')
window.dispatchEvent(new Event(PROXY_PLAN_CHANGED_EVENT))
await loadBalance()
@@ -869,6 +1065,26 @@ async function reload() {
refreshSnapshots()
}
watch(whitelistVisible, (visible) => {
if (visible && !whitelistIp.value.trim()) {
fetchCurrentIp()
}
})
watch(proxyMode, () => {
whitelistResult.value = null
whitelistError.value = ''
})
watch(proxyProvider, (provider) => {
whitelistResult.value = null
whitelistError.value = ''
// 切到极客时补拉套餐信息(含套餐 key 是否已配置),否则白名单按钮的可用态是旧的
if (provider === 'jikip' && proxySupported.value) {
void loadProxyPlan()
}
})
let unsubscribe: (() => void) | null = null
onMounted(() => {
@@ -247,6 +247,14 @@
gap: 8px;
}
.proxy-plan-row + .proxy-plan-row {
margin-top: 8px;
}
.proxy-plan-row--single {
grid-template-columns: 1fr;
}
.proxy-plan-hint {
margin-top: 8px;
color: #7f8a96;
@@ -16,14 +16,15 @@ export interface SourceOption<T extends string> {
hint: string
}
// 选项按「默认项在前」排列:面板直接按数组顺序渲染,用户不用先找默认在哪。
export const LLM_CHANNEL_OPTIONS: Array<SourceOption<LlmChannelKey>> = [
{ key: 'longtaitou', label: '龙抬头', hint: 'longtaitou.token6688.com' },
{ key: 't8star', label: '贞贞', hint: 'ai.t8star.org(默认)' },
{ key: 'longtaitou', label: '龙抬头', hint: 'longtaitou.token6688.com' },
]
export const PROXY_PROVIDER_OPTIONS: Array<SourceOption<ProxyProviderKey>> = [
{ key: 'qingguo', label: '青果', hint: 'overseas.proxy.qg.net' },
{ key: 'jikip', label: '极客', hint: 'api.jikip.com(默认)' },
{ key: 'qingguo', label: '青果', hint: 'overseas.proxy.qg.net' },
]
export const DEFAULT_LLM_CHANNEL: LlmChannelKey = 't8star'
@@ -0,0 +1,45 @@
/**
* 客户端公网 IP 获取:用户在「代理 → 白名单」里点「获取当前 IP」时使用。
*
* 口径:myip.ipip.net 是极客帮助文档里的测试目标,且带 CORS(Access-Control-Allow-Origin: *),
* 浏览器/pywebview 可直接请求;解析失败时返回 null 由用户手填,绝不静默写一个猜的 IP。
*/
const PUBLIC_IP_ENDPOINT = 'https://myip.ipip.net'
const IPV4_TOKEN = /(?:^|[^\d.])(\d{1,3}(?:\.\d{1,3}){3})(?![\d.])/
/** 从任意文本里提取第一个合法 IPv4;没有返回 null。 */
export function parsePublicIp(text: unknown): string | null {
if (typeof text !== 'string') return null
const match = IPV4_TOKEN.exec(text)
if (!match) return null
const candidate = match[1]
const parts = candidate.split('.')
if (parts.length !== 4) return null
for (const part of parts) {
if (!/^(0|[1-9]\d{0,2})$/.test(part)) return null
if (Number(part) > 255) return null
}
return candidate
}
/** 请求公网 IP 回显服务;超时/网络/解析失败都会抛错,由调用方给出可见提示。 */
export async function fetchPublicIp(timeoutMs = 8000): Promise<string> {
if (typeof fetch !== 'function') {
throw new Error('当前环境不支持自动获取 IP,请手动填写')
}
const controller = new AbortController()
const timer = setTimeout(() => controller.abort(), timeoutMs)
try {
const response = await fetch(PUBLIC_IP_ENDPOINT, { signal: controller.signal, cache: 'no-store' })
if (!response.ok) {
throw new Error(`获取当前 IP 失败(HTTP ${response.status}),请手动填写`)
}
const ip = parsePublicIp(await response.text())
if (!ip) {
throw new Error('获取当前 IP 失败,请手动填写')
}
return ip
} finally {
clearTimeout(timer)
}
}