🐛 fix 123pan 403

This commit is contained in:
微凉
2021-12-19 20:32:47 +08:00
parent d00f75c814
commit 731dbf6c3a
12 changed files with 68 additions and 55 deletions

View File

@ -30,7 +30,7 @@ func Down(c *gin.Context) {
Proxy(c)
return
}
link, err := driver.Link(base.Args{Path: path}, account)
link, err := driver.Link(base.Args{Path: path, IP: c.ClientIP()}, account)
if err != nil {
common.ErrorResp(c, err, 500)
return
@ -71,6 +71,7 @@ func Proxy(c *gin.Context) {
c.Redirect(302, link)
return
}
// 对于中转不需要重设IP
link, err := driver.Link(base.Args{Path: path}, account)
if err != nil {
common.ErrorResp(c, err, 500)

View File

@ -46,6 +46,13 @@ func Path(c *gin.Context) {
} else {
file.Url = fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path)
}
} else if driver.Config().NeedSetLink {
link, err := driver.Link(base.Args{Path: path, IP: c.ClientIP()}, account)
if err != nil {
common.ErrorResp(c, err, 500)
return
}
file.Url = link.Url
}
c.JSON(200, common.Resp{
Code: 200,
@ -94,7 +101,7 @@ func Link(c *gin.Context) {
})
return
}
link, err := driver.Link(base.Args{Path: path}, account)
link, err := driver.Link(base.Args{Path: path, IP: c.ClientIP()}, account)
if err != nil {
common.ErrorResp(c, err, 500)
return

View File

@ -12,6 +12,7 @@ import (
"github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/utils"
log "github.com/sirupsen/logrus"
"net"
"net/http"
"path"
"path/filepath"
@ -101,6 +102,25 @@ func (fs *FileSystem) Files(rawPath string) ([]model.File, error) {
// }
//}
func ClientIP(r *http.Request) string {
xForwardedFor := r.Header.Get("X-Forwarded-For")
ip := strings.TrimSpace(strings.Split(xForwardedFor, ",")[0])
if ip != "" {
return ip
}
ip = strings.TrimSpace(r.Header.Get("X-Real-Ip"))
if ip != "" {
return ip
}
if ip, _, err := net.SplitHostPort(strings.TrimSpace(r.RemoteAddr)); err == nil {
return ip
}
return ""
}
func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) {
rawPath = utils.ParsePath(rawPath)
log.Debugf("get link path: %s", rawPath)
@ -123,7 +143,7 @@ func (fs *FileSystem) Link(r *http.Request, rawPath string) (string, error) {
link += "?sign" + sign
}
} else {
link_, err := driver.Link(base.Args{Path: path_}, account)
link_, err := driver.Link(base.Args{Path: path_, IP: ClientIP(r)}, account)
if err != nil {
return "", err
}