fix proxy

pull/548/head
微凉 2021-11-13 16:02:19 +08:00
parent cf07b3921c
commit a73501463f
1 changed files with 18 additions and 1 deletions

View File

@ -6,8 +6,10 @@ import (
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net/http/httputil"
"net/url" "net/url"
"path/filepath" "path/filepath"
"strings"
) )
func Down(c *gin.Context) { func Down(c *gin.Context) {
@ -64,6 +66,21 @@ func Proxy(c *gin.Context) {
return return
} else { } else {
driver.Proxy(c) 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)
} }
} }