diff --git a/drivers/123/123.go b/drivers/123/123.go index a512ed48..e955ceaa 100644 --- a/drivers/123/123.go +++ b/drivers/123/123.go @@ -112,7 +112,7 @@ func (driver Pan123) GetFiles(parentId string, account *model.Account) ([]Pan123 "trashed": "false", } _, err := driver.Request("https://www.123pan.com/api/file/list", - base.Get, query, nil, &resp, false, account) + base.Get, nil, query, nil, &resp, false, account) if err != nil { return nil, err } @@ -122,7 +122,7 @@ func (driver Pan123) GetFiles(parentId string, account *model.Account) ([]Pan123 return res, nil } -func (driver Pan123) Request(url string, method int, query map[string]string, data *base.Json, resp interface{}, proxy bool, account *model.Account) ([]byte, error) { +func (driver Pan123) Request(url string, method int, headers, query map[string]string, data *base.Json, resp interface{}, proxy bool, account *model.Account) ([]byte, error) { rawUrl := url if account.APIProxyUrl != "" { url = fmt.Sprintf("%s/%s", account.APIProxyUrl, url) @@ -130,6 +130,9 @@ func (driver Pan123) Request(url string, method int, query map[string]string, da log.Debugf("request: %s", url) req := base.RestyClient.R() req.SetHeader("Authorization", "Bearer "+account.AccessToken) + if headers != nil { + req.SetHeaders(headers) + } if query != nil { req.SetQueryParams(query) } @@ -161,7 +164,7 @@ func (driver Pan123) Request(url string, method int, query map[string]string, da if err != nil { return nil, err } - return driver.Request(rawUrl, method, query, data, resp, proxy, account) + return driver.Request(rawUrl, method, headers, query, data, resp, proxy, account) } return nil, errors.New(jsoniter.Get(body, "message").ToString()) } diff --git a/drivers/123/driver.go b/drivers/123/driver.go index 83059479..1ffeaf7e 100644 --- a/drivers/123/driver.go +++ b/drivers/123/driver.go @@ -22,8 +22,8 @@ type Pan123 struct{} func (driver Pan123) Config() base.DriverConfig { return base.DriverConfig{ - Name: "123Pan", - OnlyProxy: false, + Name: "123Pan", + NeedSetLink: true, } } @@ -126,11 +126,19 @@ func (driver Pan123) Files(path string, account *model.Account) ([]model.File, e } func (driver Pan123) Link(args base.Args, account *model.Account) (*base.Link, error) { + log.Debugf("%+v", args) file, err := driver.GetFile(utils.ParsePath(args.Path), account) if err != nil { return nil, err } var resp Pan123DownResp + var headers map[string]string + if args.IP != "" && args.IP != "::1" { + headers = map[string]string{ + //"X-Real-IP": "1.1.1.1", + "X-Forwarded-For": args.IP, + } + } data := base.Json{ "driveId": 0, "etag": file.Etag, @@ -141,7 +149,7 @@ func (driver Pan123) Link(args base.Args, account *model.Account) (*base.Link, e "type": file.Type, } _, err = driver.Request("https://www.123pan.com/api/file/download_info", - base.Post, nil, &data, &resp, true, account) + base.Post, headers, nil, &data, &resp, true, account) //_, err = pan123Client.R().SetResult(&resp).SetHeader("authorization", "Bearer "+account.AccessToken). // SetBody().Post("https://www.123pan.com/api/file/download_info") if err != nil { @@ -174,11 +182,6 @@ func (driver Pan123) Path(path string, account *model.Account) (*model.File, []m return nil, nil, err } if !file.IsDir() { - link, err := driver.Link(base.Args{Path: path}, account) - if err != nil { - return nil, nil, err - } - file.Url = link.Url return file, nil, nil } files, err := driver.Files(path, account) @@ -215,7 +218,7 @@ func (driver Pan123) MakeDir(path string, account *model.Account) error { "type": 1, } _, err = driver.Request("https://www.123pan.com/api/file/upload_request", - base.Post, nil, &data, nil, false, account) + base.Post, nil, nil, &data, nil, false, account) //_, err = driver.Post("https://www.123pan.com/api/file/upload_request", data, account) if err == nil { _ = base.DeleteCache(dir, account) @@ -239,7 +242,7 @@ func (driver Pan123) Move(src string, dst string, account *model.Account) error "fileName": dstName, } _, err = driver.Request("https://www.123pan.com/api/file/rename", - base.Post, nil, &data, nil, false, account) + base.Post, nil, nil, &data, nil, false, account) //_, err = driver.Post("https://www.123pan.com/api/file/rename", data, account) } else { // move @@ -253,7 +256,7 @@ func (driver Pan123) Move(src string, dst string, account *model.Account) error "parentFileId": parentFileId, } _, err = driver.Request("https://www.123pan.com/api/file/mod_pid", - base.Post, nil, &data, nil, false, account) + base.Post, nil, nil, &data, nil, false, account) //_, err = driver.Post("https://www.123pan.com/api/file/mod_pid", data, account) } if err != nil { @@ -278,7 +281,7 @@ func (driver Pan123) Delete(path string, account *model.Account) error { "fileTrashInfoList": file, } _, err = driver.Request("https://www.123pan.com/api/file/trash", - base.Post, nil, &data, nil, false, account) + base.Post, nil, nil, &data, nil, false, account) //_, err = driver.Post("https://www.123pan.com/api/file/trash", data, account) if err == nil { _ = base.DeleteCache(utils.Dir(path), account) @@ -313,7 +316,7 @@ func (driver Pan123) Upload(file *model.FileStream, account *model.Account) erro "type": 0, } res, err := driver.Request("https://www.123pan.com/api/file/upload_request", - base.Post, nil, &data, nil, false, account) + base.Post, nil, nil, &data, nil, false, account) //res, err := driver.Post("https://www.123pan.com/api/file/upload_request", data, account) if err != nil { return err diff --git a/drivers/189/driver.go b/drivers/189/driver.go index 53da0406..c030d09e 100644 --- a/drivers/189/driver.go +++ b/drivers/189/driver.go @@ -26,8 +26,8 @@ type Cloud189 struct{} func (driver Cloud189) Config() base.DriverConfig { return base.DriverConfig{ - Name: "189Cloud", - OnlyProxy: false, + Name: "189Cloud", + NeedSetLink: true, } } @@ -194,11 +194,6 @@ func (driver Cloud189) Path(path string, account *model.Account) (*model.File, [ return nil, nil, err } if !file.IsDir() { - link, err := driver.Link(base.Args{Path: path}, account) - if err != nil { - return nil, nil, err - } - file.Url = link.Url return file, nil, nil } files, err := driver.Files(path, account) diff --git a/drivers/alidrive/driver.go b/drivers/alidrive/driver.go index 9d71b279..8f01c0fa 100644 --- a/drivers/alidrive/driver.go +++ b/drivers/alidrive/driver.go @@ -21,8 +21,8 @@ type AliDrive struct{} func (driver AliDrive) Config() base.DriverConfig { return base.DriverConfig{ - Name: "AliDrive", - OnlyProxy: false, + Name: "AliDrive", + NeedSetLink: true, } } @@ -201,11 +201,6 @@ func (driver AliDrive) Path(path string, account *model.Account) (*model.File, [ return nil, nil, err } if !file.IsDir() { - link, err := driver.Link(base.Args{Path: path}, account) - if err != nil { - return nil, nil, err - } - file.Url = link.Url return file, nil, nil } files, err := driver.Files(path, account) diff --git a/drivers/base/driver.go b/drivers/base/driver.go index 4f9072a8..7062bebe 100644 --- a/drivers/base/driver.go +++ b/drivers/base/driver.go @@ -9,10 +9,11 @@ import ( ) type DriverConfig struct { - Name string - OnlyProxy bool - NoLink bool // 必须本机返回的 - ApiProxy bool // 使用API中转的 + Name string + OnlyProxy bool + NoLink bool // 必须本机返回的 + ApiProxy bool // 使用API中转的 + NeedSetLink bool // 需要设置链接的 } type Args struct { diff --git a/drivers/google/driver.go b/drivers/google/driver.go index 48d58303..023eb87b 100644 --- a/drivers/google/driver.go +++ b/drivers/google/driver.go @@ -159,8 +159,7 @@ func (driver GoogleDrive) Path(path string, account *model.Account) (*model.File if err != nil { return nil, nil, err } - if file.Type != conf.FOLDER { - //file.Url, _ = driver.Link(path, account) + if !file.IsDir() { return file, nil, nil } files, err := driver.Files(path, account) diff --git a/drivers/lanzou/driver.go b/drivers/lanzou/driver.go index a63db1e3..a348cae7 100644 --- a/drivers/lanzou/driver.go +++ b/drivers/lanzou/driver.go @@ -14,8 +14,8 @@ type Lanzou struct{} func (driver Lanzou) Config() base.DriverConfig { return base.DriverConfig{ - Name: "Lanzou", - OnlyProxy: false, + Name: "Lanzou", + NeedSetLink: true, } } @@ -145,11 +145,6 @@ func (driver Lanzou) Path(path string, account *model.Account) (*model.File, []m return nil, nil, err } if !file.IsDir() { - link, err := driver.Link(base.Args{Path: path}, account) - if err != nil { - return nil, nil, err - } - file.Url = link.Url return file, nil, nil } files, err := driver.Files(path, account) diff --git a/drivers/onedrive/driver.go b/drivers/onedrive/driver.go index edc79754..0c8c90f3 100644 --- a/drivers/onedrive/driver.go +++ b/drivers/onedrive/driver.go @@ -16,8 +16,7 @@ type Onedrive struct{} func (driver Onedrive) Config() base.DriverConfig { return base.DriverConfig{ - Name: "Onedrive", - OnlyProxy: false, + Name: "Onedrive", } } @@ -193,7 +192,6 @@ func (driver Onedrive) Path(path string, account *model.Account) (*model.File, [ return nil, nil, err } if !file.IsDir() { - //file.Url, _ = driver.Link(path, account) return file, nil, nil } files, err := driver.Files(path, account) diff --git a/drivers/pikpak/driver.go b/drivers/pikpak/driver.go index 27dce66c..aa69ecc4 100644 --- a/drivers/pikpak/driver.go +++ b/drivers/pikpak/driver.go @@ -15,7 +15,8 @@ type PikPak struct{} func (driver PikPak) Config() base.DriverConfig { return base.DriverConfig{ - Name: "PikPak", + Name: "PikPak", + NeedSetLink: true, } } @@ -123,11 +124,6 @@ func (driver PikPak) Path(path string, account *model.Account) (*model.File, []m return nil, nil, err } if !file.IsDir() { - link, err := driver.Link(base.Args{Path: path}, account) - if err != nil { - return nil, nil, err - } - file.Url = link.Url return file, nil, nil } files, err := driver.Files(path, account) diff --git a/server/controllers/down.go b/server/controllers/down.go index 5c2be573..fefdc862 100644 --- a/server/controllers/down.go +++ b/server/controllers/down.go @@ -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) diff --git a/server/controllers/path.go b/server/controllers/path.go index d16b42dd..7e02ae8d 100644 --- a/server/controllers/path.go +++ b/server/controllers/path.go @@ -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 diff --git a/server/webdav/file.go b/server/webdav/file.go index 1602abee..c0a9cfe8 100644 --- a/server/webdav/file.go +++ b/server/webdav/file.go @@ -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 }