Skip to content

Commit c6bd437

Browse files
authored
feat(drivers/webdav): add support for 302 redirects (OpenListTeam#1952)
* Remove the `OnlyProxy` restriction and obtain the redirected link to support 302 * Add `driver.Config` `PreferProxy` to recommend user to enable the proxy by default --------- Signed-off-by: MadDogOwner <xiaoran@xrgzs.top>
1 parent e192149 commit c6bd437

File tree

10 files changed

+95
-14
lines changed

10 files changed

+95
-14
lines changed

drivers/123/meta.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var config = driver.Config{
2020
Name: "123Pan",
2121
DefaultRoot: "0",
2222
LocalSort: true,
23+
PreferProxy: true,
2324
}
2425

2526
func init() {
@@ -28,7 +29,7 @@ func init() {
2829
return &Pan123{
2930
Addition: Addition{
3031
UploadThread: 3,
31-
Platform: "web",
32+
Platform: "web",
3233
},
3334
}
3435
})

drivers/123_open/meta.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var config = driver.Config{
3535
Name: "123 Open",
3636
DefaultRoot: "0",
3737
LocalSort: true,
38+
PreferProxy: true,
3839
}
3940

4041
func init() {

drivers/123_share/meta.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ var config = driver.Config{
1919
LocalSort: true,
2020
NoUpload: true,
2121
DefaultRoot: "0",
22+
PreferProxy: true,
2223
}
2324

2425
func init() {

drivers/baidu_netdisk/meta.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package baidu_netdisk
22

33
import (
4+
"time"
5+
46
"github.com/OpenListTeam/OpenList/v4/internal/driver"
57
"github.com/OpenListTeam/OpenList/v4/internal/op"
6-
"time"
78
)
89

910
type Addition struct {
@@ -39,6 +40,7 @@ const (
3940
var config = driver.Config{
4041
Name: "BaiduNetdisk",
4142
DefaultRoot: "/",
43+
PreferProxy: true,
4244
}
4345

4446
func init() {

drivers/webdav/driver.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ package webdav
22

33
import (
44
"context"
5+
"fmt"
56
"net/http"
67
"os"
78
"path"
89
"time"
910

11+
"github.com/OpenListTeam/OpenList/v4/drivers/base"
1012
"github.com/OpenListTeam/OpenList/v4/internal/driver"
1113
"github.com/OpenListTeam/OpenList/v4/internal/model"
1214
"github.com/OpenListTeam/OpenList/v4/pkg/cron"
@@ -68,6 +70,22 @@ func (d *WebDav) Link(ctx context.Context, file model.Obj, args model.LinkArgs)
6870
if err != nil {
6971
return nil, err
7072
}
73+
if args.Redirect {
74+
// get the url after redirect
75+
req := base.NoRedirectClient.R()
76+
req.Header = header
77+
req.SetDoNotParseResponse(true)
78+
res, err := req.Get(url)
79+
if err != nil {
80+
return nil, err
81+
}
82+
_ = res.RawResponse.Body.Close()
83+
if (res.StatusCode() == 302 || res.StatusCode() == 307 || res.StatusCode() == 308) && res.Header().Get("location") != "" {
84+
url = res.Header().Get("location")
85+
} else {
86+
return nil, fmt.Errorf("redirect failed, status: %d", res.StatusCode())
87+
}
88+
}
7189
return &model.Link{
7290
URL: url,
7391
Header: header,

drivers/webdav/meta.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ type Addition struct {
1717
var config = driver.Config{
1818
Name: "WebDav",
1919
LocalSort: true,
20-
OnlyProxy: true,
2120
DefaultRoot: "/",
21+
PreferProxy: true,
2222
}
2323

2424
func init() {

internal/bootstrap/patch/all.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"github.com/OpenListTeam/OpenList/v4/internal/bootstrap/patch/v3_32_0"
66
"github.com/OpenListTeam/OpenList/v4/internal/bootstrap/patch/v3_41_0"
77
"github.com/OpenListTeam/OpenList/v4/internal/bootstrap/patch/v4_1_8"
8+
"github.com/OpenListTeam/OpenList/v4/internal/bootstrap/patch/v4_1_9"
89
)
910

1011
type VersionPatches struct {
@@ -39,4 +40,10 @@ var UpgradePatches = []VersionPatches{
3940
v4_1_8.FixAliasConfig,
4041
},
4142
},
43+
{
44+
Version: "v4.1.9",
45+
Patches: []func(){
46+
v4_1_9.EnableWebDavProxy,
47+
},
48+
},
4249
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package v4_1_9
2+
3+
import (
4+
"github.com/OpenListTeam/OpenList/v4/internal/db"
5+
"github.com/OpenListTeam/OpenList/v4/pkg/utils"
6+
)
7+
8+
// EnableWebDavProxy updates Webdav driver storages to enable proxy
9+
func EnableWebDavProxy() {
10+
storages, _, err := db.GetStorages(1, -1)
11+
if err != nil {
12+
utils.Log.Errorf("[EnableWebDavProxy] failed to get storages: %s", err.Error())
13+
return
14+
}
15+
for _, s := range storages {
16+
if s.Driver != "WebDav" {
17+
continue
18+
}
19+
if !s.WebProxy {
20+
s.WebProxy = true
21+
}
22+
if s.WebdavPolicy == "302_redirect" {
23+
s.WebdavPolicy = "native_proxy"
24+
}
25+
err = db.UpdateStorage(&s)
26+
if err != nil {
27+
utils.Log.Errorf("[EnableWebDavProxy] failed to update storage [%d]%s: %s", s.ID, s.MountPath, err.Error())
28+
}
29+
}
30+
}

internal/driver/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ type Config struct {
2424
LinkCacheMode `json:"-"`
2525
// if the driver only store indices of files (e.g. UrlTree)
2626
OnlyIndices bool `json:"only_indices"`
27+
// prefer proxy download even if direct link is available
28+
PreferProxy bool `json:"prefer_proxy"`
2729
}
2830
type LinkCacheMode int8
2931

@@ -40,3 +42,7 @@ const (
4042
func (c Config) MustProxy() bool {
4143
return c.OnlyProxy || c.NoLinkURL
4244
}
45+
46+
func (c Config) DefaultProxy() bool {
47+
return c.PreferProxy
48+
}

internal/op/driver.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,32 @@ func getMainItems(config driver.Config) []driver.Item {
9797
Required: true,
9898
})
9999
} else {
100-
items = append(items, []driver.Item{{
101-
Name: "web_proxy",
102-
Type: conf.TypeBool,
103-
}, {
104-
Name: "webdav_policy",
105-
Type: conf.TypeSelect,
106-
Options: "302_redirect,use_proxy_url,native_proxy",
107-
Default: "302_redirect",
108-
Required: true,
109-
},
110-
}...)
100+
if config.DefaultProxy() {
101+
items = append(items, []driver.Item{{
102+
Name: "web_proxy",
103+
Type: conf.TypeBool,
104+
Default: "true",
105+
}, {
106+
Name: "webdav_policy",
107+
Type: conf.TypeSelect,
108+
Options: "302_redirect,use_proxy_url,native_proxy",
109+
Default: "native_proxy",
110+
Required: true,
111+
},
112+
}...)
113+
} else {
114+
items = append(items, []driver.Item{{
115+
Name: "web_proxy",
116+
Type: conf.TypeBool,
117+
}, {
118+
Name: "webdav_policy",
119+
Type: conf.TypeSelect,
120+
Options: "302_redirect,use_proxy_url,native_proxy",
121+
Default: "302_redirect",
122+
Required: true,
123+
},
124+
}...)
125+
}
111126
if config.ProxyRangeOption {
112127
item := driver.Item{
113128
Name: "proxy_range",

0 commit comments

Comments
 (0)