fix(admin): 渠道代理 - 校验代理地址并拒绝无效出口

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

校验渠道级代理地址并在历史无效配置下拒绝出站请求,保留空代理回退全局代理的行为,同时补充管理端和出站层回归测试。

Co-authored-by: ddcat666 <26564566+ddcat666@users.noreply.github.com>
This commit is contained in:
dxiaomandddcat666 authored and ddcat666 committed 2026-10-10 05:44:48 +08:00
1 parent 524d2910ca
commit d13e1dcaa3
9 files changed
+88 -9

No files matched your search

+5 -1
View File
@@ -189,7 +189,11 @@ func (s *Service) channelFromRequest(req ChannelRequest, channel model.ModelChan
}
channel.ModelsJSON = string(modelsJSON)
channel.HeadersJSON = headersJSON
channel.ProxyURL = strings.TrimSpace(req.ProxyURL)
proxyURL := strings.TrimSpace(req.ProxyURL)
if _, err := ValidateProxyURL(proxyURL); err != nil {
return channel, err
}
channel.ProxyURL = proxyURL
if req.Enabled != nil {
channel.Enabled = *req.Enabled
}
@@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"yingce/backend/internal/model"
@@ -189,6 +190,24 @@ func TestChannelFromRequestStoresAndClearsHeaders(t *testing.T) {
}
}
func TestChannelFromRequestValidatesProxyURL(t *testing.T) {
limit := 4
base := model.ModelChannel{}
channel, err := channelFromRequest(ChannelRequest{Name: "Proxy", BaseURL: "https://93.184.216.34/v1", ProxyURL: "socks5://user:secret@127.0.0.1:1080", ConcurrencyLimit: &limit}, base)
if err != nil {
t.Fatal(err)
}
if channel.ProxyURL != "socks5://user:secret@127.0.0.1:1080" {
t.Fatalf("proxy URL = %q", channel.ProxyURL)
}
for _, proxyURL := range []string{"ftp://127.0.0.1:1080", "socks5://", "socks5://127.0.0.1:1080#fragment", "http://" + strings.Repeat("a", 506)} {
_, err := channelFromRequest(ChannelRequest{Name: "Proxy", BaseURL: "https://93.184.216.34/v1", ProxyURL: proxyURL, ConcurrencyLimit: &limit}, base)
if err == nil {
t.Fatalf("invalid proxy URL %q was accepted", proxyURL)
}
}
}
func TestPublicChannelOnlyReturnsSystemHeadersAndProxyToAdmin(t *testing.T) {
channel := model.ModelChannel{ID: "system-1", Scope: model.ChannelScopeSystem, BaseURL: "https://example.com/v1", HeadersJSON: `[{"name":"X-Gateway-Tenant","value":"tenant-a"}]`, ProxyURL: "socks5://user:secret@127.0.0.1:1080"}
adminView := publicChannel(channel, true, nil)
+1 -1
View File
@@ -13,7 +13,7 @@ func (s *Service) ValidateChannelOutboundURL(rawURL string) (*url.URL, error) {
}
// OutboundHTTPClientForChannel 返回渠道专用的出站 HTTP 客户端。
// proxyURL 非空时使用渠道级代理(socks5/http/https),否则回退到全局客户端。
// proxyURL 非空时使用渠道级代理(socks5/socks5h/http/https),否则回退到全局客户端。
func (s *Service) OutboundHTTPClientForChannel(timeout time.Duration, _ *url.URL, proxyURL string) *http.Client {
return OutboundHTTPClientWithProxy(timeout, proxyURL)
}
+5
View File
@@ -26,6 +26,11 @@ func ValidateCustomRelayURL(rawURL string) (*url.URL, error) {
return parsed, mapOutboundError(err)
}
func ValidateProxyURL(rawURL string) (*url.URL, error) {
parsed, err := outbound.ValidateProxyURL(rawURL)
return parsed, mapOutboundError(err)
}
func OutboundHTTPClient(timeout time.Duration) *http.Client {
return outbound.OutboundHTTPClient(timeout)
}