From 623a12050e7df5cf73f0c44b81e4722df3ea880a Mon Sep 17 00:00:00 2001 From: MadDogOwner Date: Sun, 19 Oct 2025 22:45:40 +0800 Subject: [PATCH] feat(openlist): add PassIPToUpsteam to driver (#1498) --- drivers/115/meta.go | 2 +- drivers/115_open/meta.go | 2 +- drivers/alias/driver.go | 21 +++++++++++++++++++ drivers/alias/meta.go | 1 + drivers/baidu_photo/meta.go | 2 +- drivers/febbox/meta.go | 2 +- drivers/openlist/driver.go | 38 +++++++++++++++++++++++------------ drivers/openlist/meta.go | 2 ++ internal/cache/typed_cache.go | 17 ++++------------ internal/driver/config.go | 20 ++++++++++++++---- internal/driver/driver.go | 5 +++++ internal/op/fs.go | 26 ++++++++++-------------- 12 files changed, 89 insertions(+), 49 deletions(-) diff --git a/drivers/115/meta.go b/drivers/115/meta.go index 5dc9ef41..02c180c3 100644 --- a/drivers/115/meta.go +++ b/drivers/115/meta.go @@ -17,7 +17,7 @@ type Addition struct { var config = driver.Config{ Name: "115 Cloud", DefaultRoot: "0", - LinkCacheType: 2, + LinkCacheMode: driver.LinkCacheUA, } func init() { diff --git a/drivers/115_open/meta.go b/drivers/115_open/meta.go index d9d7d598..f66ae931 100644 --- a/drivers/115_open/meta.go +++ b/drivers/115_open/meta.go @@ -19,7 +19,7 @@ type Addition struct { var config = driver.Config{ Name: "115 Open", DefaultRoot: "0", - LinkCacheType: 2, + LinkCacheMode: driver.LinkCacheUA, } func init() { diff --git a/drivers/alias/driver.go b/drivers/alias/driver.go index ec4b0844..cfa1152d 100644 --- a/drivers/alias/driver.go +++ b/drivers/alias/driver.go @@ -524,4 +524,25 @@ func (d *Alias) ArchiveDecompress(ctx context.Context, srcObj, dstDir model.Obj, } } +func (d *Alias) ResolveLinkCacheMode(path string) driver.LinkCacheMode { + root, sub := d.getRootAndPath(path) + dsts, ok := d.pathMap[root] + if !ok { + return 0 + } + for _, dst := range dsts { + storage, actualPath, err := op.GetStorageAndActualPath(stdpath.Join(dst, sub)) + if err == nil { + continue + } + mode := storage.Config().LinkCacheMode + if mode == -1 { + return storage.(driver.LinkCacheModeResolver).ResolveLinkCacheMode(actualPath) + } else { + return mode + } + } + return 0 +} + var _ driver.Driver = (*Alias)(nil) diff --git a/drivers/alias/meta.go b/drivers/alias/meta.go index bdab5374..763e6647 100644 --- a/drivers/alias/meta.go +++ b/drivers/alias/meta.go @@ -26,6 +26,7 @@ var config = driver.Config{ NoUpload: false, DefaultRoot: "/", ProxyRangeOption: true, + LinkCacheMode: driver.LinkCacheAuto, } func init() { diff --git a/drivers/baidu_photo/meta.go b/drivers/baidu_photo/meta.go index d144b0c7..6c6ca5c3 100644 --- a/drivers/baidu_photo/meta.go +++ b/drivers/baidu_photo/meta.go @@ -20,7 +20,7 @@ type Addition struct { var config = driver.Config{ Name: "BaiduPhoto", LocalSort: true, - LinkCacheType: 2, + LinkCacheMode: driver.LinkCacheUA, } func init() { diff --git a/drivers/febbox/meta.go b/drivers/febbox/meta.go index fdc5931a..cb6ff8fb 100644 --- a/drivers/febbox/meta.go +++ b/drivers/febbox/meta.go @@ -19,7 +19,7 @@ var config = driver.Config{ Name: "FebBox", NoUpload: true, DefaultRoot: "0", - LinkCacheType: 1, + LinkCacheMode: driver.LinkCacheIP, } func init() { diff --git a/drivers/openlist/driver.go b/drivers/openlist/driver.go index 2c064369..edf27c38 100644 --- a/drivers/openlist/driver.go +++ b/drivers/openlist/driver.go @@ -26,11 +26,6 @@ type OpenList struct { } func (d *OpenList) Config() driver.Config { - if d.PassUAToUpsteam { - c := config - c.LinkCacheType = 2 // add User-Agent to cache key - return c - } return config } @@ -115,19 +110,29 @@ func (d *OpenList) List(ctx context.Context, dir model.Obj, args model.ListArgs) func (d *OpenList) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) { var resp common.Resp[FsGetResp] + headers := map[string]string{ + "User-Agent": base.UserAgent, + } // if PassUAToUpsteam is true, then pass the user-agent to the upstream - userAgent := base.UserAgent if d.PassUAToUpsteam { - userAgent = args.Header.Get("user-agent") - if userAgent == "" { - userAgent = base.UserAgent + userAgent := args.Header.Get("user-agent") + if userAgent != "" { + headers["User-Agent"] = base.UserAgent + } + } + // if PassIPToUpsteam is true, then pass the ip address to the upstream + if d.PassIPToUpsteam { + ip := args.IP + if ip != "" { + headers["X-Forwarded-For"] = ip + headers["X-Real-Ip"] = ip } } _, _, err := d.request("/fs/get", http.MethodPost, func(req *resty.Request) { req.SetResult(&resp).SetBody(FsGetReq{ Path: file.GetPath(), Password: d.MetaPassword, - }).SetHeader("user-agent", userAgent) + }).SetHeaders(headers) }) if err != nil { return nil, err @@ -360,8 +365,15 @@ func (d *OpenList) ArchiveDecompress(ctx context.Context, srcObj, dstDir model.O return err } -//func (d *OpenList) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) { -// return nil, errs.NotSupport -//} +func (d *OpenList) ResolveLinkCacheMode(_ string) driver.LinkCacheMode { + var mode driver.LinkCacheMode + if d.PassIPToUpsteam { + mode |= driver.LinkCacheIP + } + if d.PassUAToUpsteam { + mode |= driver.LinkCacheUA + } + return mode +} var _ driver.Driver = (*OpenList)(nil) diff --git a/drivers/openlist/meta.go b/drivers/openlist/meta.go index fc76c142..10c950a3 100644 --- a/drivers/openlist/meta.go +++ b/drivers/openlist/meta.go @@ -12,6 +12,7 @@ type Addition struct { Username string `json:"username"` Password string `json:"password"` Token string `json:"token"` + PassIPToUpsteam bool `json:"pass_ip_to_upsteam" default:"true"` PassUAToUpsteam bool `json:"pass_ua_to_upsteam" default:"true"` ForwardArchiveReq bool `json:"forward_archive_requests" default:"true"` } @@ -22,6 +23,7 @@ var config = driver.Config{ DefaultRoot: "/", CheckStatus: true, ProxyRangeOption: true, + LinkCacheMode: driver.LinkCacheAuto, } func init() { diff --git a/internal/cache/typed_cache.go b/internal/cache/typed_cache.go index 3277a782..7ba126be 100644 --- a/internal/cache/typed_cache.go +++ b/internal/cache/typed_cache.go @@ -43,23 +43,14 @@ func (c *TypedCache[T]) SetTypeWithExpirable(key, typeKey string, value T, exp E } } -// Prefer to use typeKeys for lookup; if none match, use fallbackTypeKey for lookup -func (c *TypedCache[T]) GetType(key, fallbackTypeKey string, typeKeys ...string) (T, bool) { +func (c *TypedCache[T]) GetType(key, typeKey string) (T, bool) { c.mu.RLock() cache, exists := c.entries[key] if !exists { c.mu.RUnlock() return *new(T), false } - entry, exists := cache[fallbackTypeKey] - if len(typeKeys) > 0 { - for _, tk := range typeKeys { - if entry, exists = cache[tk]; exists { - fallbackTypeKey = tk - break - } - } - } + entry, exists := cache[typeKey] if !exists { c.mu.RUnlock() return *new(T), false @@ -72,8 +63,8 @@ func (c *TypedCache[T]) GetType(key, fallbackTypeKey string, typeKeys ...string) } c.mu.Lock() - if cache[fallbackTypeKey] == entry { - delete(cache, fallbackTypeKey) + if cache[typeKey] == entry { + delete(cache, typeKey) if len(cache) == 0 { delete(c.entries, key) } diff --git a/internal/driver/config.go b/internal/driver/config.go index 7db34512..12fb1577 100644 --- a/internal/driver/config.go +++ b/internal/driver/config.go @@ -17,11 +17,23 @@ type Config struct { ProxyRangeOption bool `json:"-"` // if the driver returns Link without URL, this should be set to true NoLinkURL bool `json:"-"` - // LinkCacheType=1 add IP to cache key - // - // LinkCacheType=2 add UserAgent to cache key - LinkCacheType uint8 `json:"-"` + // Link cache behaviour: + // - LinkCacheAuto: let driver decide per-path (implement driver.LinkCacheModeResolver) + // - LinkCacheNone: no extra info added to cache key (default) + // - flags (OR-able) can add more attributes to cache key (IP, UA, ...) + LinkCacheMode `json:"-"` } +type LinkCacheMode int8 + +const ( + LinkCacheAuto LinkCacheMode = -1 // Let the driver decide per-path (use driver.LinkCacheModeResolver) + LinkCacheNone LinkCacheMode = 0 // No extra info added to cache key (default) +) + +const ( + LinkCacheIP LinkCacheMode = 1 << iota // include client IP in cache key + LinkCacheUA // include User-Agent in cache key +) func (c Config) MustProxy() bool { return c.OnlyProxy || c.NoLinkURL diff --git a/internal/driver/driver.go b/internal/driver/driver.go index 7521e8d7..1ce1e451 100644 --- a/internal/driver/driver.go +++ b/internal/driver/driver.go @@ -213,3 +213,8 @@ type WithDetails interface { type Reference interface { InitReference(storage Driver) error } + +type LinkCacheModeResolver interface { + // ResolveLinkCacheMode returns the LinkCacheMode for the given path. + ResolveLinkCacheMode(path string) LinkCacheMode +} diff --git a/internal/op/fs.go b/internal/op/fs.go index 1624ee3c..c761828e 100644 --- a/internal/op/fs.go +++ b/internal/op/fs.go @@ -168,23 +168,19 @@ func Link(ctx context.Context, storage driver.Driver, path string, args model.Li return nil, nil, errors.WithMessagef(errs.StorageNotInit, "storage status: %s", storage.GetStorage().Status) } - typeKey := args.Type - var typeKeys []string - switch storage.Config().LinkCacheType { - case 1: - if args.IP != "" { - typeKey += "/" + args.IP - typeKeys = []string{typeKey} - } - case 2: - if ua := args.Header.Get("User-Agent"); ua != "" { - typeKey += "/" + ua - typeKeys = []string{typeKey} - } + mode := storage.Config().LinkCacheMode + if mode == -1 { + mode = storage.(driver.LinkCacheModeResolver).ResolveLinkCacheMode(path) + } + typeKey := args.Type + if mode&driver.LinkCacheIP == 1 { + typeKey += "/" + args.IP + } + if mode&driver.LinkCacheUA == 1 { + typeKey += "/" + args.Header.Get("User-Agent") } - key := Key(storage, path) - if ol, exists := Cache.linkCache.GetType(key, args.Type, typeKeys...); exists { + if ol, exists := Cache.linkCache.GetType(key, typeKey); exists { if ol.link.Expiration != nil || ol.link.SyncClosers.AcquireReference() || !ol.link.RequireReference { return ol.link, ol.obj, nil