feat(admin): 渠道管理 - 模型供应商支持渠道级 SOCKS5/HTTP 代理

- ModelChannel 新增 proxy_url 字段,v48 迁移自动加列
- outbound 新增 OutboundHTTPClientWithProxy,socks5/socks5h/http/https 均支持;
  目标主机名不做本地解析,直接交给代理服务器远端解析,本地开启 TUN/VPN 时仍可用
- 系统代理、Provider 出站、管理员模型测试路径均使用渠道级代理
- 渠道管理表单新增"代理地址"输入框,留空回退全局代理
This commit is contained in:
dxiaom committed 2026-10-10 00:59:37 +08:00
1 parent 09f9b8e902
commit 8b0a8054e1
13 files changed
+119 -8

No files matched your search

+7
View File
@@ -93,6 +93,7 @@ type ChannelRequest struct {
Models []string `json:"models"`
Headers []OutboundHeader `json:"headers"`
Enabled *bool `json:"enabled"`
ProxyURL string `json:"proxyUrl"`
}
type PublicModelChannel struct {
@@ -106,6 +107,7 @@ type PublicModelChannel struct {
APIKey string `json:"apiKey"`
APIFormat string `json:"apiFormat"`
ConcurrencyLimit int `json:"concurrencyLimit"`
ProxyURL string `json:"proxyUrl"`
Models []string `json:"models"`
ModelCosts []PublicChannelModelPrice `json:"modelCosts"`
Headers []OutboundHeader `json:"headers,omitempty"`
@@ -187,6 +189,7 @@ func (s *Service) channelFromRequest(req ChannelRequest, channel model.ModelChan
}
channel.ModelsJSON = string(modelsJSON)
channel.HeadersJSON = headersJSON
channel.ProxyURL = strings.TrimSpace(req.ProxyURL)
if req.Enabled != nil {
channel.Enabled = *req.Enabled
}
@@ -206,6 +209,9 @@ func mergeChannelRequest(req ChannelRequest, channel model.ModelChannel) Channel
if req.Headers == nil {
req.Headers, _ = ParseOutboundHeadersJSON(channel.HeadersJSON)
}
if strings.TrimSpace(req.ProxyURL) == "" {
req.ProxyURL = channel.ProxyURL
}
return req
}
@@ -255,6 +261,7 @@ func publicChannel(channel model.ModelChannel, admin bool, channelModels []model
APIKey: apiKey,
APIFormat: channel.APIFormat,
ConcurrencyLimit: channel.ConcurrencyLimit,
ProxyURL: channel.ProxyURL,
Models: models,
ModelCosts: modelCosts,
Headers: headers,
@@ -112,7 +112,7 @@ func (s *Service) TestAdminChannelModel(ctx context.Context, actor *model.User,
defer cancel()
testCtx = context.WithValue(testCtx, providerAnalyticsKey{}, providerAnalyticsContext{
Service: s, Billing: s.taskBilling(), UserID: actor.ID, ChannelID: channel.ID, Capability: capability,
Operation: "admin_model_test", Model: modelKey, VideoSeconds: videoSecondsValue,
Operation: "admin_model_test", Model: modelKey, VideoSeconds: videoSecondsValue, ProxyURL: channel.ProxyURL,
})
testCtx = withProtocolRegistry(testCtx, s.protocolRegistry())
startedAt := time.Now()
+4 -2
View File
@@ -12,6 +12,8 @@ func (s *Service) ValidateChannelOutboundURL(rawURL string) (*url.URL, error) {
return ValidateOutboundURL(rawURL)
}
func (s *Service) OutboundHTTPClientForChannel(timeout time.Duration, _ *url.URL) *http.Client {
return OutboundHTTPClient(timeout)
// OutboundHTTPClientForChannel 返回渠道专用的出站 HTTP 客户端。
// proxyURL 非空时使用渠道级代理(socks5/http/https),否则回退到全局客户端。
func (s *Service) OutboundHTTPClientForChannel(timeout time.Duration, _ *url.URL, proxyURL string) *http.Client {
return OutboundHTTPClientWithProxy(timeout, proxyURL)
}
+4
View File
@@ -30,6 +30,10 @@ func OutboundHTTPClient(timeout time.Duration) *http.Client {
return outbound.OutboundHTTPClient(timeout)
}
func OutboundHTTPClientWithProxy(timeout time.Duration, proxyURL string) *http.Client {
return outbound.OutboundHTTPClientWithProxy(timeout, proxyURL)
}
func ApplyDefaultOutboundHeaders(req *http.Request) {
outbound.ApplyDefaultOutboundHeaders(req)
}
+6
View File
@@ -78,6 +78,7 @@ type providerAnalyticsContext struct {
Capability string
Operation string
ChannelID string
ProxyURL string
Model string
VideoSeconds int
RequestKind string
@@ -107,6 +108,11 @@ func withProviderAnalytics(ctx context.Context, service *Service, task model.Tas
if normalized := normalizeCapability(input.Mode); normalized != "" {
metadata.Capability = normalized
}
if metadata.ChannelID != "" && service != nil {
if ch, err := service.repo.SystemChannel(metadata.ChannelID); err == nil {
metadata.ProxyURL = ch.ProxyURL
}
}
}
return context.WithValue(ctx, providerAnalyticsKey{}, metadata)
}
+3 -1
View File
@@ -257,10 +257,12 @@ func doBinaryWithConsumer(req *http.Request, onChunk func(string, []byte)) ([]by
cacheManagementRequest := requestKind == "cache-create" || requestKind == "cache-delete"
responseLimit := maxProviderResponseBytes
channelID := ""
channelProxyURL := ""
if metadata, ok := req.Context().Value(providerAnalyticsKey{}).(providerAnalyticsContext); ok && metadata.Service != nil {
runtimeService = metadata.Service
coordinator = metadata.Service.coordinator
channelID = metadata.ChannelID
channelProxyURL = metadata.ProxyURL
policy, err := metadata.Service.RuntimePolicy()
if err != nil {
err = fmt.Errorf("读取生成资源限制失败:%w", err)
@@ -302,7 +304,7 @@ func doBinaryWithConsumer(req *http.Request, onChunk func(string, []byte)) ([]by
return nil, "", err
}
ApplyDefaultOutboundHeaders(req)
client := OutboundHTTPClient(requestTimeout)
client := OutboundHTTPClientWithProxy(requestTimeout, channelProxyURL)
resp, err := client.Do(req)
if err != nil {
err = providerConnectionError(err)