From 3b2a02cfed89549fc89b378ccfb3037c906bb9ab Mon Sep 17 00:00:00 2001 From: XOF Date: Mon, 15 Dec 2025 05:21:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20proxy/handler.go?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- proxy/handler.go | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/proxy/handler.go b/proxy/handler.go index 7032e8f..d185a68 100644 --- a/proxy/handler.go +++ b/proxy/handler.go @@ -42,18 +42,15 @@ func NewHandler( func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { path := strings.TrimPrefix(r.URL.Path, "/p/") - parts := strings.SplitN(path, "/", 2) - if len(parts) == 0 || parts[0] == "" { - http.Error(w, "Invalid token", http.StatusBadRequest) + slashIdx := strings.Index(path, "/") + if slashIdx == -1 { + http.Error(w, "Invalid proxy path", http.StatusBadRequest) return } - token := parts[0] - subPath := "" - if len(parts) > 1 { - subPath = "/" + parts[1] - } + token := path[:slashIdx] + targetPath := path[slashIdx+1:] session := h.sessionManager.Get(token) if session == nil { @@ -61,9 +58,24 @@ func (h *ProxyHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) { return } - targetURL := session.TargetURL + subPath + var targetURL string + + if strings.HasPrefix(targetPath, "http://") || strings.HasPrefix(targetPath, "https://") { + targetURL = targetPath + } else { + baseURL := strings.TrimSuffix(session.TargetURL, "/") + if !strings.HasPrefix(targetPath, "/") { + targetPath = "/" + targetPath + } + targetURL = baseURL + targetPath + } + if r.URL.RawQuery != "" { - targetURL += "?" + r.URL.RawQuery + if strings.Contains(targetURL, "?") { + targetURL += "&" + r.URL.RawQuery + } else { + targetURL += "?" + r.URL.RawQuery + } } if err := h.validator.ValidateURL(targetURL); err != nil {