From a73501463f2266c29fac1327c11a71bd1c1c82f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=AE=E5=87=89?= <927625802@qq.com> Date: Sat, 13 Nov 2021 16:02:19 +0800 Subject: [PATCH] :sparkles: fix proxy --- server/down.go | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/server/down.go b/server/down.go index 2461eefe..ac0014fd 100644 --- a/server/down.go +++ b/server/down.go @@ -6,8 +6,10 @@ import ( "github.com/Xhofe/alist/utils" "github.com/gin-gonic/gin" log "github.com/sirupsen/logrus" + "net/http/httputil" "net/url" "path/filepath" + "strings" ) func Down(c *gin.Context) { @@ -64,6 +66,21 @@ func Proxy(c *gin.Context) { return } else { driver.Proxy(c) - // TODO + r := c.Request + w := c.Writer + target, err := url.Parse(link) + if err != nil { + ErrorResp(c, err, 500) + return + } + protocol := "http://" + if strings.HasPrefix(link, "https://") { + protocol = "https://" + } + targetHost, err := url.Parse(fmt.Sprintf("%s%s", protocol, target.Host)) + proxy := httputil.NewSingleHostReverseProxy(targetHost) + r.URL = target + r.Host = target.Host + proxy.ServeHTTP(w, r) } }