fix(proxy): ignore `Referer` if got redirect (close #3996)

pull/4027/head
Andy Hsu 2023-03-31 20:29:55 +08:00
parent 0c5820a98f
commit 3b07c72f88
2 changed files with 12 additions and 29 deletions

View File

@ -343,9 +343,9 @@ func (c *Client) Read(path string) ([]byte, error) {
}
func (c *Client) Link(path string) (string, http.Header, error) {
method := "HEAD"
url := PathEscape(Join(c.root, path))
r, err := http.NewRequest(method, url, nil)
method := "GET"
u := PathEscape(Join(c.root, path))
r, err := http.NewRequest(method, u, nil)
if err != nil {
return "", nil, newPathErrorErr("Link", path, err)
@ -366,31 +366,6 @@ func (c *Client) Link(path string) (string, http.Header, error) {
if c.interceptor != nil {
c.interceptor(method, r)
}
rs, err := c.c.Do(r)
if err != nil {
return "", nil, newPathErrorErr("Link", path, err)
}
if rs.StatusCode == 401 {
wwwAuthenticateHeader := strings.ToLower(rs.Header.Get("Www-Authenticate"))
if strings.Contains(wwwAuthenticateHeader, "digest") {
c.authMutex.Lock()
c.auth = &DigestAuth{auth.User(), auth.Pass(), digestParts(rs)}
c.auth.Authorize(r, method, path)
c.authMutex.Unlock()
} else if strings.Contains(wwwAuthenticateHeader, "basic") {
c.authMutex.Lock()
c.auth = &BasicAuth{auth.User(), auth.Pass()}
c.auth.Authorize(r, method, path)
c.authMutex.Unlock()
} else {
return "", nil, newPathError("Authorize", c.root, rs.StatusCode)
}
} else if rs.StatusCode > 400 {
return "", nil, newPathError("Authorize", path, rs.StatusCode)
}
return r.URL.String(), r.Header, nil
}

View File

@ -16,7 +16,15 @@ import (
log "github.com/sirupsen/logrus"
)
var HttpClient = &http.Client{}
var HttpClient = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
if len(via) >= 10 {
return errors.New("stopped after 10 redirects")
}
req.Header.Del("Referer")
return nil
},
}
func Proxy(w http.ResponseWriter, r *http.Request, link *model.Link, file model.Obj) error {
// read data with native