From ae3c167cd6469a3de815ede8474b9c1ad591b720 Mon Sep 17 00:00:00 2001 From: xfy Date: Wed, 3 Jun 2026 17:50:06 +0800 Subject: [PATCH] refactor: remove extractHostFromURL, use netutil.ParseTargetURL --- internal/proxy/header_modifier.go | 3 +- internal/proxy/proxy.go | 28 +----------- internal/proxy/proxy_coverage_extra_test.go | 49 --------------------- 3 files changed, 4 insertions(+), 76 deletions(-) diff --git a/internal/proxy/header_modifier.go b/internal/proxy/header_modifier.go index 08b5601..e96f8a0 100644 --- a/internal/proxy/header_modifier.go +++ b/internal/proxy/header_modifier.go @@ -11,6 +11,7 @@ import ( "github.com/valyala/fasthttp" "rua.plus/lolly/internal/loadbalance" "rua.plus/lolly/internal/logging" + "rua.plus/lolly/internal/netutil" "rua.plus/lolly/internal/variable" ) @@ -30,7 +31,7 @@ func (p *Proxy) modifyRequestHeaders(ctx *fasthttp.RequestCtx, target *loadbalan // 设置 Host header 为目标主机 // 从 target.URL 提取 host:port(HostClient 连接需要此格式) - targetHost := extractHostFromURL(target.URL) + targetHost, _ := netutil.ParseTargetURL(target.URL, false) if targetHost != "" { headers.Set("Host", targetHost) } diff --git a/internal/proxy/proxy.go b/internal/proxy/proxy.go index 4857eaa..95a47e3 100644 --- a/internal/proxy/proxy.go +++ b/internal/proxy/proxy.go @@ -359,7 +359,8 @@ func createHostClient(targetURL string, timeout config.ProxyTimeout, transportCf // 上游 SSL 配置(使用原生 TLSConfig) if sslCfg != nil && sslCfg.Enabled && isTLS { - tlsCfg, err := CreateTLSConfig(sslCfg, extractHostFromURL(targetURL)) + host, _ := netutil.ParseTargetURL(targetURL, false) + tlsCfg, err := CreateTLSConfig(sslCfg, host) if err != nil { logging.Error().Err(err).Str("target", targetURL).Msg("Failed to create upstream TLS config") } else { @@ -990,29 +991,4 @@ func isWebSocketRequest(ctx *fasthttp.RequestCtx) bool { return strings.EqualFold(string(upgrade), "websocket") } -// extractHostFromURL 从 URL 字符串中提取 host:port 部分。 -// -// 移除 http:// 或 https:// 协议前缀,以及路径部分, -// 仅保留主机名和端口(如 "example.com:8080")。 -// -// 参数: -// - urlStr: 完整 URL 字符串 -// -// 返回值: -// - string: host:port 格式的主机地址 -func extractHostFromURL(urlStr string) string { - // 移除协议前缀 - host := urlStr - if strings.HasPrefix(host, "http://") { - host = host[7:] - } else if strings.HasPrefix(host, "https://") { - host = host[8:] - } - // 移除路径部分 - if idx := strings.Index(host, "/"); idx != -1 { - host = host[:idx] - } - - return host -} diff --git a/internal/proxy/proxy_coverage_extra_test.go b/internal/proxy/proxy_coverage_extra_test.go index 4ca9c03..ee795c0 100644 --- a/internal/proxy/proxy_coverage_extra_test.go +++ b/internal/proxy/proxy_coverage_extra_test.go @@ -1423,55 +1423,6 @@ func TestSelectTarget_LuaEnabled(t *testing.T) { }) } -// TestExtractHostFromURL 测试 extractHostFromURL 函数。 -func TestExtractHostFromURL(t *testing.T) { - tests := []struct { - name string - url string - want string - }{ - { - name: "HTTP URL with port", - url: "http://example.com:8080", - want: "example.com:8080", - }, - { - name: "HTTPS URL with port", - url: "https://example.com:8443", - want: "example.com:8443", - }, - { - name: "HTTP URL without port", - url: "http://example.com", - want: "example.com", - }, - { - name: "HTTPS URL without port", - url: "https://example.com", - want: "example.com", - }, - { - name: "URL with path", - url: "http://example.com:8080/api/users", - want: "example.com:8080", - }, - { - name: "No protocol prefix", - url: "example.com:8080", - want: "example.com:8080", - }, - } - - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - got := extractHostFromURL(tt.url) - if got != tt.want { - t.Errorf("extractHostFromURL() = %q, want %q", got, tt.want) - } - }) - } -} - // TestBackgroundRefresh_304 测试后台刷新收到 304 响应。 func TestBackgroundRefresh_304(t *testing.T) { ln := fasthttputil.NewInmemoryListener()