feat: 为 Provider 认证添加代理配置支持

- 前端新增可折叠的代理配置字段组(URL/用户名/密码)
- 更新 anyrouter/cubence/new-api/yescode 模板支持代理
- 后端 provider_ops 服务验证和数据获取时支持代理
- 移除 Dockerfile 中的 ca-certificates(回退之前改动)
This commit is contained in:
fawney19
2026-01-19 18:30:52 +08:00
parent b6a9969aee
commit ffdc9a9585
13 changed files with 336 additions and 101 deletions

View File

@@ -9,6 +9,7 @@
import type { AuthTemplate, AuthTemplateFieldGroup } from './types'
import type { SaveConfigRequest } from '@/api/providerOps'
import { PROXY_FIELD_GROUP, buildProxyUrl, parseProxyUrl } from './types'
export const anyrouterTemplate: AuthTemplate = {
id: 'anyrouter',
@@ -38,6 +39,7 @@ export const anyrouterTemplate: AuthTemplate = {
},
],
},
PROXY_FIELD_GROUP,
]
},
@@ -49,7 +51,9 @@ export const anyrouterTemplate: AuthTemplate = {
base_url: baseUrl,
connector: {
auth_type: 'cookie',
config: {},
config: {
proxy: buildProxyUrl(formData),
},
credentials: {
session_cookie: formData.session_cookie,
},
@@ -60,9 +64,11 @@ export const anyrouterTemplate: AuthTemplate = {
},
parseConfig(config: any): Record<string, any> {
const proxyData = parseProxyUrl(config?.connector?.config?.proxy)
return {
base_url: config?.base_url || '',
session_cookie: config?.connector?.credentials?.session_cookie || '',
...proxyData,
}
},

View File

@@ -9,6 +9,7 @@
import type { AuthTemplate, AuthTemplateFieldGroup, BalanceExtraItem } from './types'
import type { SaveConfigRequest } from '@/api/providerOps'
import { PROXY_FIELD_GROUP, buildProxyUrl, parseProxyUrl } from './types'
/**
* 格式化窗口限额显示(百分比格式)
@@ -78,6 +79,7 @@ export const cubenceTemplate: AuthTemplate = {
},
],
},
PROXY_FIELD_GROUP,
]
},
@@ -89,7 +91,9 @@ export const cubenceTemplate: AuthTemplate = {
base_url: baseUrl,
connector: {
auth_type: 'cookie',
config: {},
config: {
proxy: buildProxyUrl(formData),
},
credentials: {
token_cookie: formData.token_cookie,
},
@@ -100,9 +104,11 @@ export const cubenceTemplate: AuthTemplate = {
},
parseConfig(config: any): Record<string, any> {
const proxyData = parseProxyUrl(config?.connector?.config?.proxy)
return {
base_url: config?.base_url || '',
token_cookie: config?.connector?.credentials?.token_cookie || '',
...proxyData,
}
},

View File

@@ -9,6 +9,7 @@
import type { AuthTemplate, AuthTemplateFieldGroup } from './types'
import type { SaveConfigRequest } from '@/api/providerOps'
import { PROXY_FIELD_GROUP, buildProxyUrl, parseProxyUrl } from './types'
export const newApiTemplate: AuthTemplate = {
id: 'new_api',
@@ -45,6 +46,7 @@ export const newApiTemplate: AuthTemplate = {
},
],
},
PROXY_FIELD_GROUP,
]
},
@@ -58,6 +60,7 @@ export const newApiTemplate: AuthTemplate = {
auth_type: 'api_key',
config: {
auth_method: 'bearer',
proxy: buildProxyUrl(formData),
},
credentials: {
api_key: formData.api_key,
@@ -70,10 +73,12 @@ export const newApiTemplate: AuthTemplate = {
},
parseConfig(config: any): Record<string, any> {
const proxyData = parseProxyUrl(config?.connector?.config?.proxy)
return {
base_url: config?.base_url || '',
api_key: config?.connector?.credentials?.api_key || '',
user_id: config?.connector?.credentials?.user_id || '',
...proxyData,
}
},

View File

@@ -46,6 +46,14 @@ export interface AuthTemplateFieldGroup {
title?: string
/** 分组内的字段 */
fields: AuthTemplateField[]
/** 是否可折叠(默认展开) */
collapsible?: boolean
/** 折叠时的默认状态true=展开false=收起 */
defaultExpanded?: boolean
/** 是否有启用开关(与 collapsible 配合使用) */
hasToggle?: boolean
/** 启用开关对应的表单字段 key */
toggleKey?: string
}
/**
@@ -141,3 +149,134 @@ export interface AuthTemplateRegistry {
/** 注册模板 */
register(template: AuthTemplate): void
}
// ==================== 通用字段定义 ====================
/**
* 代理地址字段
*/
export const PROXY_URL_FIELD: AuthTemplateField = {
key: 'proxy_url',
label: '代理地址',
type: 'text',
placeholder: 'http://proxy:port 或 socks5://',
required: false,
}
/**
* 代理用户名字段
*/
export const PROXY_USERNAME_FIELD: AuthTemplateField = {
key: 'proxy_username',
label: '用户名',
type: 'text',
placeholder: '可选',
required: false,
}
/**
* 代理密码字段
*/
export const PROXY_PASSWORD_FIELD: AuthTemplateField = {
key: 'proxy_password',
label: '密码',
type: 'password',
placeholder: '可选',
required: false,
sensitive: true,
}
/**
* 通用代理配置字段组(可折叠,带启用开关)
*/
export const PROXY_FIELD_GROUP: AuthTemplateFieldGroup = {
title: '代理配置',
fields: [PROXY_URL_FIELD, PROXY_USERNAME_FIELD, PROXY_PASSWORD_FIELD],
collapsible: true,
defaultExpanded: false,
hasToggle: true,
toggleKey: 'proxy_enabled',
}
/**
* 构建代理 URL包含认证信息
*
* @param formData 表单数据
* @returns 完整的代理 URL或 undefined
*/
export function buildProxyUrl(formData: Record<string, any>): string | undefined {
if (!formData.proxy_enabled || !formData.proxy_url) {
return undefined
}
const proxyUrl = formData.proxy_url.trim()
const username = formData.proxy_username?.trim()
const password = formData.proxy_password?.trim()
// 如果没有认证信息,直接返回 URL
if (!username) {
return proxyUrl
}
// 解析 URL 并添加认证信息
try {
const url = new URL(proxyUrl)
url.username = username
if (password) {
url.password = password
}
return url.toString()
} catch {
// URL 解析失败,尝试简单拼接
const protocol = proxyUrl.includes('://') ? proxyUrl.split('://')[0] : 'http'
const host = proxyUrl.includes('://') ? proxyUrl.split('://')[1] : proxyUrl
const auth = password ? `${username}:${password}` : username
return `${protocol}://${auth}@${host}`
}
}
/**
* 从代理 URL 解析表单数据
*
* @param proxyUrl 代理 URL
* @returns 表单数据
*/
export function parseProxyUrl(proxyUrl: string | undefined): Record<string, any> {
if (!proxyUrl) {
return {
proxy_enabled: false,
proxy_url: '',
proxy_username: '',
proxy_password: '',
}
}
try {
const url = new URL(proxyUrl)
const username = url.username || ''
const password = url.password || ''
// 移除认证信息后的 URL
url.username = ''
url.password = ''
const cleanUrl = url.toString()
return {
proxy_enabled: true,
proxy_url: cleanUrl,
proxy_username: username,
proxy_password: password,
}
} catch {
// 解析失败,直接使用原始 URL
return {
proxy_enabled: true,
proxy_url: proxyUrl,
proxy_username: '',
proxy_password: '',
}
}
}
// 兼容旧的导出(已废弃,请使用 PROXY_FIELD_GROUP
export const PROXY_FIELD: AuthTemplateField = PROXY_URL_FIELD

View File

@@ -10,6 +10,7 @@
import type { AuthTemplate, AuthTemplateFieldGroup, BalanceExtraItem } from './types'
import type { SaveConfigRequest } from '@/api/providerOps'
import { PROXY_FIELD_GROUP, buildProxyUrl, parseProxyUrl } from './types'
/**
* 格式化限额显示(百分比格式)
@@ -51,6 +52,7 @@ export const yescodeTemplate: AuthTemplate = {
},
],
},
PROXY_FIELD_GROUP,
]
},
@@ -62,7 +64,9 @@ export const yescodeTemplate: AuthTemplate = {
base_url: baseUrl,
connector: {
auth_type: 'cookie',
config: {},
config: {
proxy: buildProxyUrl(formData),
},
credentials: {
auth_cookie: formData.auth_cookie,
},
@@ -73,9 +77,11 @@ export const yescodeTemplate: AuthTemplate = {
},
parseConfig(config: any): Record<string, any> {
const proxyData = parseProxyUrl(config?.connector?.config?.proxy)
return {
base_url: config?.base_url || '',
auth_cookie: config?.connector?.credentials?.auth_cookie || '',
...proxyData,
}
},

View File

@@ -48,80 +48,139 @@
v-for="(group, groupIndex) in fieldGroups"
:key="groupIndex"
>
<!-- 分组标题 -->
<!-- 可折叠的分组如代理配置 -->
<div
v-if="group.title"
class="pt-2 text-sm font-medium text-muted-foreground"
>
{{ group.title }}
</div>
<!-- 字段列表 -->
<div
v-for="field in group.fields"
:key="field.key"
v-if="group.collapsible && group.hasToggle && group.toggleKey"
class="space-y-2"
>
<Label>
{{ field.label }}
<span
v-if="field.required"
class="text-muted-foreground/70"
>*</span>
</Label>
<!-- 标题栏标题在左开关在右在卡片外 -->
<div class="flex items-center justify-between">
<span class="text-sm font-medium text-foreground">{{ group.title }}</span>
<div class="flex items-center gap-2">
<span class="text-xs text-muted-foreground">启用代理</span>
<Switch
:model-value="formData[group.toggleKey] || false"
@update:model-value="formData[group.toggleKey] = $event"
/>
</div>
</div>
<!-- 文本输入 -->
<Input
v-if="field.type === 'text'"
v-model="formData[field.key]"
:placeholder="field.placeholder"
disable-autofill
/>
<!-- 密码/敏感输入 -->
<Input
v-else-if="field.type === 'password'"
v-model="formData[field.key]"
:placeholder="sensitivePlaceholders[field.key] || field.placeholder"
masked
/>
<!-- 下拉选择 -->
<Select
v-else-if="field.type === 'select'"
v-model="formData[field.key]"
@update:model-value="handleFieldChange(field.key, $event)"
<!-- 展开内容卡片 -->
<div
v-if="formData[group.toggleKey]"
class="rounded-lg border border-border bg-muted/30 px-4 py-3"
>
<SelectTrigger>
<SelectValue :placeholder="field.placeholder || '请选择'" />
</SelectTrigger>
<SelectContent>
<SelectItem
v-for="option in field.options"
:key="option.value"
:value="option.value"
<!-- 横向排列的字段代理地址占更多空间 -->
<div class="flex gap-3">
<div
v-for="(field, fieldIndex) in group.fields"
:key="field.key"
class="space-y-1"
:class="fieldIndex === 0 ? 'flex-[2]' : 'flex-1'"
>
{{ option.label }}
</SelectItem>
</SelectContent>
</Select>
<Label class="text-xs text-muted-foreground">
{{ field.label }}
</Label>
<!-- 多行文本 -->
<Textarea
v-else-if="field.type === 'textarea'"
v-model="formData[field.key]"
:placeholder="field.placeholder"
rows="3"
/>
<!-- 文本输入 -->
<Input
v-if="field.type === 'text'"
v-model="formData[field.key]"
:placeholder="field.placeholder"
disable-autofill
class="h-8 text-sm"
/>
<!-- 帮助文本 -->
<p
v-if="field.helpText"
class="text-xs text-muted-foreground"
>
{{ field.helpText }}
</p>
<!-- 密码/敏感输入 -->
<Input
v-else-if="field.type === 'password'"
v-model="formData[field.key]"
:placeholder="sensitivePlaceholders[field.key] || field.placeholder"
masked
class="h-8 text-sm"
/>
</div>
</div>
</div>
</div>
<!-- 普通分组非折叠 -->
<template v-else>
<!-- 分组标题 -->
<div
v-if="group.title"
class="pt-2 text-sm font-medium text-muted-foreground"
>
{{ group.title }}
</div>
<!-- 字段列表 -->
<div
v-for="field in group.fields"
:key="field.key"
class="space-y-2"
>
<Label>
{{ field.label }}
<span
v-if="field.required"
class="text-muted-foreground/70"
>*</span>
</Label>
<!-- 文本输入 -->
<Input
v-if="field.type === 'text'"
v-model="formData[field.key]"
:placeholder="field.placeholder"
disable-autofill
/>
<!-- 密码/敏感输入 -->
<Input
v-else-if="field.type === 'password'"
v-model="formData[field.key]"
:placeholder="sensitivePlaceholders[field.key] || field.placeholder"
masked
/>
<!-- 下拉选择 -->
<Select
v-else-if="field.type === 'select'"
v-model="formData[field.key]"
@update:model-value="handleFieldChange(field.key, $event)"
>
<SelectTrigger>
<SelectValue :placeholder="field.placeholder || '请选择'" />
</SelectTrigger>
<SelectContent>
<SelectItem
v-for="option in field.options"
:key="option.value"
:value="option.value"
>
{{ option.label }}
</SelectItem>
</SelectContent>
</Select>
<!-- 多行文本 -->
<Textarea
v-else-if="field.type === 'textarea'"
v-model="formData[field.key]"
:placeholder="field.placeholder"
rows="3"
/>
<!-- 帮助文本 -->
<p
v-if="field.helpText"
class="text-xs text-muted-foreground"
>
{{ field.helpText }}
</p>
</div>
</template>
</template>
</template>
</div>
@@ -165,6 +224,7 @@ import {
SelectItem,
SelectTrigger,
SelectValue,
Switch,
} from '@/components/ui'
import { saveProviderOpsConfig, verifyProviderAuth, getProviderOpsConfig } from '@/api/providerOps'
import { useToast } from '@/composables/useToast'
@@ -175,7 +235,7 @@ import {
} from '../auth-templates'
// 敏感字段列表(用于验证和加载配置时的特殊处理)
const SENSITIVE_FIELDS = ['api_key', 'password', 'session_token', 'session_cookie', 'token_cookie', 'auth_cookie', 'cookie_string', 'cookies'] as const
const SENSITIVE_FIELDS = ['api_key', 'password', 'session_token', 'session_cookie', 'token_cookie', 'auth_cookie', 'cookie_string', 'cookies', 'proxy_password'] as const
const props = defineProps<{
open: boolean