Skip to content

Commit

Permalink
fix(123): pass ip when getting download link
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 10, 2022
1 parent 220cd4d commit 5ed43fd
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
46 changes: 43 additions & 3 deletions drivers/123/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"fmt"
"io"
"net/http"
"net/url"
"os"

"github.com/alist-org/alist/v3/drivers/base"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/go-resty/resty/v2"
log "github.com/sirupsen/logrus"
)

type Pan123 struct {
Expand Down Expand Up @@ -68,9 +70,47 @@ func (d *Pan123) List(ctx context.Context, dir model.Obj, args model.ListArgs) (

func (d *Pan123) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
if f, ok := file.(File); ok {
return &model.Link{
URL: f.DownloadUrl,
}, nil
var resp DownResp
var headers map[string]string
if !utils.IsLocalIPAddr(args.IP) {
headers = map[string]string{
//"X-Real-IP": "1.1.1.1",
"X-Forwarded-For": args.IP,
}
}
data := base.Json{
"driveId": 0,
"etag": f.Etag,
"fileId": f.FileId,
"fileName": f.FileName,
"s3keyFlag": f.S3KeyFlag,
"size": f.Size,
"type": f.Type,
}
_, err := d.request("https://www.123pan.com/api/file/download_info", http.MethodPost, func(req *resty.Request) {
req.SetBody(data).SetHeaders(headers)
}, &resp)
if err != nil {
return nil, err
}
u, err := url.Parse(resp.Data.DownloadUrl)
if err != nil {
return nil, err
}
u_ := fmt.Sprintf("https://%s%s", u.Host, u.Path)
res, err := base.NoRedirectClient.R().SetQueryParamsFromValues(u.Query()).Head(u_)
if err != nil {
return nil, err
}
log.Debug(res.String())
link := model.Link{
URL: resp.Data.DownloadUrl,
}
log.Debugln("res code: ", res.StatusCode())
if res.StatusCode() == 302 {
link.URL = res.Header().Get("location")
}
return &link, nil
} else {
return nil, fmt.Errorf("can't convert obj")
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/utils/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,26 @@ func ClientIP(r *http.Request) string {

return ""
}

func IsLocalIPAddr(ip string) bool {
return IsLocalIP(net.ParseIP(ip))
}

func IsLocalIP(ip net.IP) bool {
if ip == nil {
return false
}
if ip.IsLoopback() {
return true
}

ip4 := ip.To4()
if ip4 == nil {
return false
}

return ip4[0] == 10 || // 10.0.0.0/8
(ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31) || // 172.16.0.0/12
(ip4[0] == 169 && ip4[1] == 254) || // 169.254.0.0/16
(ip4[0] == 192 && ip4[1] == 168) // 192.168.0.0/16
}

0 comments on commit 5ed43fd

Please sign in to comment.