mirror of
https://github.com/OpenListTeam/OpenList.git
synced 2025-11-25 03:15:19 +08:00
feat(openlist): add PassIPToUpsteam to driver (#1498)
This commit is contained in:
@@ -17,7 +17,7 @@ type Addition struct {
|
|||||||
var config = driver.Config{
|
var config = driver.Config{
|
||||||
Name: "115 Cloud",
|
Name: "115 Cloud",
|
||||||
DefaultRoot: "0",
|
DefaultRoot: "0",
|
||||||
LinkCacheType: 2,
|
LinkCacheMode: driver.LinkCacheUA,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ type Addition struct {
|
|||||||
var config = driver.Config{
|
var config = driver.Config{
|
||||||
Name: "115 Open",
|
Name: "115 Open",
|
||||||
DefaultRoot: "0",
|
DefaultRoot: "0",
|
||||||
LinkCacheType: 2,
|
LinkCacheMode: driver.LinkCacheUA,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
@@ -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)
|
var _ driver.Driver = (*Alias)(nil)
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ var config = driver.Config{
|
|||||||
NoUpload: false,
|
NoUpload: false,
|
||||||
DefaultRoot: "/",
|
DefaultRoot: "/",
|
||||||
ProxyRangeOption: true,
|
ProxyRangeOption: true,
|
||||||
|
LinkCacheMode: driver.LinkCacheAuto,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ type Addition struct {
|
|||||||
var config = driver.Config{
|
var config = driver.Config{
|
||||||
Name: "BaiduPhoto",
|
Name: "BaiduPhoto",
|
||||||
LocalSort: true,
|
LocalSort: true,
|
||||||
LinkCacheType: 2,
|
LinkCacheMode: driver.LinkCacheUA,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ var config = driver.Config{
|
|||||||
Name: "FebBox",
|
Name: "FebBox",
|
||||||
NoUpload: true,
|
NoUpload: true,
|
||||||
DefaultRoot: "0",
|
DefaultRoot: "0",
|
||||||
LinkCacheType: 1,
|
LinkCacheMode: driver.LinkCacheIP,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
@@ -26,11 +26,6 @@ type OpenList struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (d *OpenList) Config() driver.Config {
|
func (d *OpenList) Config() driver.Config {
|
||||||
if d.PassUAToUpsteam {
|
|
||||||
c := config
|
|
||||||
c.LinkCacheType = 2 // add User-Agent to cache key
|
|
||||||
return c
|
|
||||||
}
|
|
||||||
return config
|
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) {
|
func (d *OpenList) Link(ctx context.Context, file model.Obj, args model.LinkArgs) (*model.Link, error) {
|
||||||
var resp common.Resp[FsGetResp]
|
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
|
// if PassUAToUpsteam is true, then pass the user-agent to the upstream
|
||||||
userAgent := base.UserAgent
|
|
||||||
if d.PassUAToUpsteam {
|
if d.PassUAToUpsteam {
|
||||||
userAgent = args.Header.Get("user-agent")
|
userAgent := args.Header.Get("user-agent")
|
||||||
if userAgent == "" {
|
if userAgent != "" {
|
||||||
userAgent = base.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) {
|
_, _, err := d.request("/fs/get", http.MethodPost, func(req *resty.Request) {
|
||||||
req.SetResult(&resp).SetBody(FsGetReq{
|
req.SetResult(&resp).SetBody(FsGetReq{
|
||||||
Path: file.GetPath(),
|
Path: file.GetPath(),
|
||||||
Password: d.MetaPassword,
|
Password: d.MetaPassword,
|
||||||
}).SetHeader("user-agent", userAgent)
|
}).SetHeaders(headers)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -360,8 +365,15 @@ func (d *OpenList) ArchiveDecompress(ctx context.Context, srcObj, dstDir model.O
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
//func (d *OpenList) Other(ctx context.Context, args model.OtherArgs) (interface{}, error) {
|
func (d *OpenList) ResolveLinkCacheMode(_ string) driver.LinkCacheMode {
|
||||||
// return nil, errs.NotSupport
|
var mode driver.LinkCacheMode
|
||||||
//}
|
if d.PassIPToUpsteam {
|
||||||
|
mode |= driver.LinkCacheIP
|
||||||
|
}
|
||||||
|
if d.PassUAToUpsteam {
|
||||||
|
mode |= driver.LinkCacheUA
|
||||||
|
}
|
||||||
|
return mode
|
||||||
|
}
|
||||||
|
|
||||||
var _ driver.Driver = (*OpenList)(nil)
|
var _ driver.Driver = (*OpenList)(nil)
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ type Addition struct {
|
|||||||
Username string `json:"username"`
|
Username string `json:"username"`
|
||||||
Password string `json:"password"`
|
Password string `json:"password"`
|
||||||
Token string `json:"token"`
|
Token string `json:"token"`
|
||||||
|
PassIPToUpsteam bool `json:"pass_ip_to_upsteam" default:"true"`
|
||||||
PassUAToUpsteam bool `json:"pass_ua_to_upsteam" default:"true"`
|
PassUAToUpsteam bool `json:"pass_ua_to_upsteam" default:"true"`
|
||||||
ForwardArchiveReq bool `json:"forward_archive_requests" default:"true"`
|
ForwardArchiveReq bool `json:"forward_archive_requests" default:"true"`
|
||||||
}
|
}
|
||||||
@@ -22,6 +23,7 @@ var config = driver.Config{
|
|||||||
DefaultRoot: "/",
|
DefaultRoot: "/",
|
||||||
CheckStatus: true,
|
CheckStatus: true,
|
||||||
ProxyRangeOption: true,
|
ProxyRangeOption: true,
|
||||||
|
LinkCacheMode: driver.LinkCacheAuto,
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
|||||||
17
internal/cache/typed_cache.go
vendored
17
internal/cache/typed_cache.go
vendored
@@ -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, typeKey string) (T, bool) {
|
||||||
func (c *TypedCache[T]) GetType(key, fallbackTypeKey string, typeKeys ...string) (T, bool) {
|
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
cache, exists := c.entries[key]
|
cache, exists := c.entries[key]
|
||||||
if !exists {
|
if !exists {
|
||||||
c.mu.RUnlock()
|
c.mu.RUnlock()
|
||||||
return *new(T), false
|
return *new(T), false
|
||||||
}
|
}
|
||||||
entry, exists := cache[fallbackTypeKey]
|
entry, exists := cache[typeKey]
|
||||||
if len(typeKeys) > 0 {
|
|
||||||
for _, tk := range typeKeys {
|
|
||||||
if entry, exists = cache[tk]; exists {
|
|
||||||
fallbackTypeKey = tk
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !exists {
|
if !exists {
|
||||||
c.mu.RUnlock()
|
c.mu.RUnlock()
|
||||||
return *new(T), false
|
return *new(T), false
|
||||||
@@ -72,8 +63,8 @@ func (c *TypedCache[T]) GetType(key, fallbackTypeKey string, typeKeys ...string)
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
if cache[fallbackTypeKey] == entry {
|
if cache[typeKey] == entry {
|
||||||
delete(cache, fallbackTypeKey)
|
delete(cache, typeKey)
|
||||||
if len(cache) == 0 {
|
if len(cache) == 0 {
|
||||||
delete(c.entries, key)
|
delete(c.entries, key)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,11 +17,23 @@ type Config struct {
|
|||||||
ProxyRangeOption bool `json:"-"`
|
ProxyRangeOption bool `json:"-"`
|
||||||
// if the driver returns Link without URL, this should be set to true
|
// if the driver returns Link without URL, this should be set to true
|
||||||
NoLinkURL bool `json:"-"`
|
NoLinkURL bool `json:"-"`
|
||||||
// LinkCacheType=1 add IP to cache key
|
// Link cache behaviour:
|
||||||
//
|
// - LinkCacheAuto: let driver decide per-path (implement driver.LinkCacheModeResolver)
|
||||||
// LinkCacheType=2 add UserAgent to cache key
|
// - LinkCacheNone: no extra info added to cache key (default)
|
||||||
LinkCacheType uint8 `json:"-"`
|
// - 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 {
|
func (c Config) MustProxy() bool {
|
||||||
return c.OnlyProxy || c.NoLinkURL
|
return c.OnlyProxy || c.NoLinkURL
|
||||||
|
|||||||
@@ -213,3 +213,8 @@ type WithDetails interface {
|
|||||||
type Reference interface {
|
type Reference interface {
|
||||||
InitReference(storage Driver) error
|
InitReference(storage Driver) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type LinkCacheModeResolver interface {
|
||||||
|
// ResolveLinkCacheMode returns the LinkCacheMode for the given path.
|
||||||
|
ResolveLinkCacheMode(path string) LinkCacheMode
|
||||||
|
}
|
||||||
|
|||||||
@@ -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)
|
return nil, nil, errors.WithMessagef(errs.StorageNotInit, "storage status: %s", storage.GetStorage().Status)
|
||||||
}
|
}
|
||||||
|
|
||||||
typeKey := args.Type
|
mode := storage.Config().LinkCacheMode
|
||||||
var typeKeys []string
|
if mode == -1 {
|
||||||
switch storage.Config().LinkCacheType {
|
mode = storage.(driver.LinkCacheModeResolver).ResolveLinkCacheMode(path)
|
||||||
case 1:
|
}
|
||||||
if args.IP != "" {
|
typeKey := args.Type
|
||||||
typeKey += "/" + args.IP
|
if mode&driver.LinkCacheIP == 1 {
|
||||||
typeKeys = []string{typeKey}
|
typeKey += "/" + args.IP
|
||||||
}
|
}
|
||||||
case 2:
|
if mode&driver.LinkCacheUA == 1 {
|
||||||
if ua := args.Header.Get("User-Agent"); ua != "" {
|
typeKey += "/" + args.Header.Get("User-Agent")
|
||||||
typeKey += "/" + ua
|
|
||||||
typeKeys = []string{typeKey}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
key := Key(storage, path)
|
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 ||
|
if ol.link.Expiration != nil ||
|
||||||
ol.link.SyncClosers.AcquireReference() || !ol.link.RequireReference {
|
ol.link.SyncClosers.AcquireReference() || !ol.link.RequireReference {
|
||||||
return ol.link, ol.obj, nil
|
return ol.link, ol.obj, nil
|
||||||
|
|||||||
Reference in New Issue
Block a user