Skip to content

Commit 5f24409

Browse files
committed
feat(proxy): Added configurable signature for down proxy URLs
- Introduced `DownProxySign` field with default `true` in the `Proxy` struct - Added `BuildDownProxyURL` utility to generate proxy URLs with an optional signature - Updated proxy handling logic across multiple modules to support the new signature configuration
1 parent b4d9beb commit 5f24409

File tree

6 files changed

+26
-19
lines changed

6 files changed

+26
-19
lines changed

‎internal/model/storage.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ type Sort struct {
2828
}
2929

3030
type Proxy struct {
31-
WebProxy bool `json:"web_proxy"`
32-
WebdavPolicy string `json:"webdav_policy"`
33-
ProxyRange bool `json:"proxy_range"`
34-
DownProxyUrl string `json:"down_proxy_url"`
31+
WebProxy bool `json:"web_proxy"`
32+
WebdavPolicy string `json:"webdav_policy"`
33+
ProxyRange bool `json:"proxy_range"`
34+
DownProxyUrl string `json:"down_proxy_url"`
35+
DownProxySign bool `json:"down_proxy_sign" gorm:"default:true"`
3536
}
3637

3738
func (s *Storage) GetStorage() *Storage {

‎internal/op/driver.go‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ func getMainItems(config driver.Config) []driver.Item {
117117
Name: "down_proxy_url",
118118
Type: conf.TypeText,
119119
})
120+
items = append(items, driver.Item{
121+
Name: "down_proxy_sign",
122+
Type: conf.TypeBool,
123+
Default: "true",
124+
})
120125
if config.LocalSort {
121126
items = append(items, []driver.Item{{
122127
Name: "order_by",

‎server/common/proxy.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"github.com/alist-org/alist/v3/internal/model"
1515
"github.com/alist-org/alist/v3/internal/net"
16+
"github.com/alist-org/alist/v3/internal/sign"
1617
"github.com/alist-org/alist/v3/internal/stream"
1718
"github.com/alist-org/alist/v3/pkg/http_range"
1819
"github.com/alist-org/alist/v3/pkg/utils"
@@ -129,6 +130,14 @@ func ProxyRange(link *model.Link, size int64) {
129130
}
130131
}
131132

133+
func BuildDownProxyURL(downProxyURL, path string, useSign bool) string {
134+
base := strings.Split(downProxyURL, "\n")[0]
135+
if useSign {
136+
return fmt.Sprintf("%s%s?sign=%s", base, utils.EncodePath(path, true), sign.Sign(path))
137+
}
138+
return fmt.Sprintf("%s%s", base, utils.EncodePath(path, true))
139+
}
140+
132141
type InterceptResponseWriter struct {
133142
http.ResponseWriter
134143
io.Writer

‎server/handles/down.go‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,12 @@ import (
66
"io"
77
stdpath "path"
88
"strconv"
9-
"strings"
109

1110
"github.com/alist-org/alist/v3/internal/conf"
1211
"github.com/alist-org/alist/v3/internal/driver"
1312
"github.com/alist-org/alist/v3/internal/fs"
1413
"github.com/alist-org/alist/v3/internal/model"
1514
"github.com/alist-org/alist/v3/internal/setting"
16-
"github.com/alist-org/alist/v3/internal/sign"
1715
"github.com/alist-org/alist/v3/pkg/utils"
1816
"github.com/alist-org/alist/v3/server/common"
1917
"github.com/gin-gonic/gin"
@@ -62,10 +60,7 @@ func Proxy(c *gin.Context) {
6260
if downProxyUrl != "" {
6361
_, ok := c.GetQuery("d")
6462
if !ok {
65-
URL := fmt.Sprintf("%s%s?sign=%s",
66-
strings.Split(downProxyUrl, "\n")[0],
67-
utils.EncodePath(rawPath, true),
68-
sign.Sign(rawPath))
63+
URL := common.BuildDownProxyURL(downProxyUrl, rawPath, storage.GetStorage().DownProxySign)
6964
c.Redirect(302, URL)
7065
return
7166
}

‎server/handles/fsread.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,10 +340,11 @@ func FsGet(c *gin.Context) {
340340
query = "?sign=" + sign.Sign(reqPath)
341341
}
342342
if storage.GetStorage().DownProxyUrl != "" {
343-
rawURL = fmt.Sprintf("%s%s?sign=%s",
344-
strings.Split(storage.GetStorage().DownProxyUrl, "\n")[0],
345-
utils.EncodePath(reqPath, true),
346-
sign.Sign(reqPath))
343+
rawURL = common.BuildDownProxyURL(
344+
storage.GetStorage().DownProxyUrl,
345+
reqPath,
346+
storage.GetStorage().DownProxySign,
347+
)
347348
} else {
348349
rawURL = fmt.Sprintf("%s/p%s%s",
349350
common.GetApiUrl(c.Request),

‎server/webdav/webdav.go‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/alist-org/alist/v3/internal/errs"
2222
"github.com/alist-org/alist/v3/internal/fs"
2323
"github.com/alist-org/alist/v3/internal/model"
24-
"github.com/alist-org/alist/v3/internal/sign"
2524
"github.com/alist-org/alist/v3/pkg/utils"
2625
"github.com/alist-org/alist/v3/server/common"
2726
)
@@ -253,10 +252,7 @@ func (h *Handler) handleGetHeadPost(w http.ResponseWriter, r *http.Request) (sta
253252
return http.StatusInternalServerError, fmt.Errorf("webdav proxy error: %+v", err)
254253
}
255254
} else if storage.GetStorage().WebdavProxy() && downProxyUrl != "" {
256-
u := fmt.Sprintf("%s%s?sign=%s",
257-
strings.Split(downProxyUrl, "\n")[0],
258-
utils.EncodePath(reqPath, true),
259-
sign.Sign(reqPath))
255+
u := common.BuildDownProxyURL(downProxyUrl, reqPath, storage.GetStorage().DownProxySign)
260256
w.Header().Set("Cache-Control", "max-age=0, no-cache, no-store, must-revalidate")
261257
http.Redirect(w, r, u, http.StatusFound)
262258
} else {

0 commit comments

Comments
 (0)