Skip to content

Commit 623a120

Browse files
authored
feat(openlist): add PassIPToUpsteam to driver (#1498)
1 parent ae2d2d1 commit 623a120

File tree

12 files changed

+88
-48
lines changed

12 files changed

+88
-48
lines changed

drivers/115/meta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type Addition struct {
1717
var config = driver.Config{
1818
Name: "115 Cloud",
1919
DefaultRoot: "0",
20-
LinkCacheType: 2,
20+
LinkCacheMode: driver.LinkCacheUA,
2121
}
2222

2323
func init() {

drivers/115_open/meta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type Addition struct {
1919
var config = driver.Config{
2020
Name: "115 Open",
2121
DefaultRoot: "0",
22-
LinkCacheType: 2,
22+
LinkCacheMode: driver.LinkCacheUA,
2323
}
2424

2525
func init() {

drivers/alias/driver.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,4 +524,25 @@ func (d *Alias) ArchiveDecompress(ctx context.Context, srcObj, dstDir model.Obj,
524524
}
525525
}
526526

527+
func (d *Alias) ResolveLinkCacheMode(path string) driver.LinkCacheMode {
528+
root, sub := d.getRootAndPath(path)
529+
dsts, ok := d.pathMap[root]
530+
if !ok {
531+
return 0
532+
}
533+
for _, dst := range dsts {
534+
storage, actualPath, err := op.GetStorageAndActualPath(stdpath.Join(dst, sub))
535+
if err == nil {
536+
continue
537+
}
538+
mode := storage.Config().LinkCacheMode
539+
if mode == -1 {
540+
return storage.(driver.LinkCacheModeResolver).ResolveLinkCacheMode(actualPath)
541+
} else {
542+
return mode
543+
}
544+
}
545+
return 0
546+
}
547+
527548
var _ driver.Driver = (*Alias)(nil)

drivers/alias/meta.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var config = driver.Config{
2626
NoUpload: false,
2727
DefaultRoot: "/",
2828
ProxyRangeOption: true,
29+
LinkCacheMode: driver.LinkCacheAuto,
2930
}
3031

3132
func init() {

drivers/baidu_photo/meta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Addition struct {
2020
var config = driver.Config{
2121
Name: "BaiduPhoto",
2222
LocalSort: true,
23-
LinkCacheType: 2,
23+
LinkCacheMode: driver.LinkCacheUA,
2424
}
2525

2626
func init() {

drivers/febbox/meta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ var config = driver.Config{
1919
Name: "FebBox",
2020
NoUpload: true,
2121
DefaultRoot: "0",
22-
LinkCacheType: 1,
22+
LinkCacheMode: driver.LinkCacheIP,
2323
}
2424

2525
func init() {

drivers/openlist/driver.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ type OpenList struct {
2626
}
2727

2828
func (d *OpenList) Config() driver.Config {
29-
if d.PassUAToUpsteam {
30-
c := config
31-
c.LinkCacheType = 2 // add User-Agent to cache key
32-
return c
33-
}
3429
return config
3530
}
3631

@@ -115,19 +110,29 @@ func (d *OpenList) List(ctx context.Context, dir model.Obj, args model.ListArgs)
115110

116111
func (d *OpenList) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
117112
var resp common.Resp[FsGetResp]
113+
headers := map[string]string{
114+
"User-Agent": base.UserAgent,
115+
}
118116
// if PassUAToUpsteam is true, then pass the user-agent to the upstream
119-
userAgent := base.UserAgent
120117
if d.PassUAToUpsteam {
121-
userAgent = args.Header.Get("user-agent")
122-
if userAgent == "" {
123-
userAgent = base.UserAgent
118+
userAgent := args.Header.Get("user-agent")
119+
if userAgent != "" {
120+
headers["User-Agent"] = base.UserAgent
121+
}
122+
}
123+
// if PassIPToUpsteam is true, then pass the ip address to the upstream
124+
if d.PassIPToUpsteam {
125+
ip := args.IP
126+
if ip != "" {
127+
headers["X-Forwarded-For"] = ip
128+
headers["X-Real-Ip"] = ip
124129
}
125130
}
126131
_, _, err := d.request("/fs/get", http.MethodPost, func(req *resty.Request) {
127132
req.SetResult(&resp).SetBody(FsGetReq{
128133
Path: file.GetPath(),
129134
Password: d.MetaPassword,
130-
}).SetHeader("user-agent", userAgent)
135+
}).SetHeaders(headers)
131136
})
132137
if err != nil {
133138
return nil, err
@@ -360,8 +365,15 @@ func (d *OpenList) ArchiveDecompress(ctx context.Context, srcObj, dstDir model.O
360365
return err
361366
}
362367

363-
//func (d *OpenList) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
364-
// return nil, errs.NotSupport
365-
//}
368+
func (d *OpenList) ResolveLinkCacheMode(_ string) driver.LinkCacheMode {
369+
var mode driver.LinkCacheMode
370+
if d.PassIPToUpsteam {
371+
mode |= driver.LinkCacheIP
372+
}
373+
if d.PassUAToUpsteam {
374+
mode |= driver.LinkCacheUA
375+
}
376+
return mode
377+
}
366378

367379
var _ driver.Driver = (*OpenList)(nil)

drivers/openlist/meta.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type Addition struct {
1212
Username string `json:"username"`
1313
Password string `json:"password"`
1414
Token string `json:"token"`
15+
PassIPToUpsteam bool `json:"pass_ip_to_upsteam" default:"true"`
1516
PassUAToUpsteam bool `json:"pass_ua_to_upsteam" default:"true"`
1617
ForwardArchiveReq bool `json:"forward_archive_requests" default:"true"`
1718
}
@@ -22,6 +23,7 @@ var config = driver.Config{
2223
DefaultRoot: "/",
2324
CheckStatus: true,
2425
ProxyRangeOption: true,
26+
LinkCacheMode: driver.LinkCacheAuto,
2527
}
2628

2729
func init() {

internal/cache/typed_cache.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,14 @@ func (c *TypedCache[T]) SetTypeWithExpirable(key, typeKey string, value T, exp E
4343
}
4444
}
4545

46-
// Prefer to use typeKeys for lookup; if none match, use fallbackTypeKey for lookup
47-
func (c *TypedCache[T]) GetType(key, fallbackTypeKey string, typeKeys ...string) (T, bool) {
46+
func (c *TypedCache[T]) GetType(key, typeKey string) (T, bool) {
4847
c.mu.RLock()
4948
cache, exists := c.entries[key]
5049
if !exists {
5150
c.mu.RUnlock()
5251
return *new(T), false
5352
}
54-
entry, exists := cache[fallbackTypeKey]
55-
if len(typeKeys) > 0 {
56-
for _, tk := range typeKeys {
57-
if entry, exists = cache[tk]; exists {
58-
fallbackTypeKey = tk
59-
break
60-
}
61-
}
62-
}
53+
entry, exists := cache[typeKey]
6354
if !exists {
6455
c.mu.RUnlock()
6556
return *new(T), false
@@ -72,8 +63,8 @@ func (c *TypedCache[T]) GetType(key, fallbackTypeKey string, typeKeys ...string)
7263
}
7364

7465
c.mu.Lock()
75-
if cache[fallbackTypeKey] == entry {
76-
delete(cache, fallbackTypeKey)
66+
if cache[typeKey] == entry {
67+
delete(cache, typeKey)
7768
if len(cache) == 0 {
7869
delete(c.entries, key)
7970
}

internal/driver/config.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,23 @@ type Config struct {
1717
ProxyRangeOption bool `json:"-"`
1818
// if the driver returns Link without URL, this should be set to true
1919
NoLinkURL bool `json:"-"`
20-
// LinkCacheType=1 add IP to cache key
21-
//
22-
// LinkCacheType=2 add UserAgent to cache key
23-
LinkCacheType uint8 `json:"-"`
20+
// Link cache behaviour:
21+
// - LinkCacheAuto: let driver decide per-path (implement driver.LinkCacheModeResolver)
22+
// - LinkCacheNone: no extra info added to cache key (default)
23+
// - flags (OR-able) can add more attributes to cache key (IP, UA, ...)
24+
LinkCacheMode `json:"-"`
2425
}
26+
type LinkCacheMode int8
27+
28+
const (
29+
LinkCacheAuto LinkCacheMode = -1 // Let the driver decide per-path (use driver.LinkCacheModeResolver)
30+
LinkCacheNone LinkCacheMode = 0 // No extra info added to cache key (default)
31+
)
32+
33+
const (
34+
LinkCacheIP LinkCacheMode = 1 << iota // include client IP in cache key
35+
LinkCacheUA // include User-Agent in cache key
36+
)
2537

2638
func (c Config) MustProxy() bool {
2739
return c.OnlyProxy || c.NoLinkURL

0 commit comments

Comments
 (0)