🚧 support proxy

pull/548/head
微凉 2021-12-08 20:00:52 +08:00
parent 09e63027d9
commit 236f9969c0
4 changed files with 42 additions and 15 deletions

View File

@ -61,12 +61,19 @@ func GetDrivers() map[string][]Item {
res[k] = v.Items() res[k] = v.Items()
} else { } else {
res[k] = append([]Item{ res[k] = append([]Item{
//{
// Name: "allow_proxy",
// Label: "allow_proxy",
// Type: TypeBool,
// Required: true,
// Description: "allow proxy",
//},
{ {
Name: "proxy", Name: "proxy",
Label: "proxy", Label: "proxy",
Type: TypeBool, Type: TypeBool,
Required: true, Required: true,
Description: "allow proxy", Description: "web proxy",
}, },
{ {
Name: "webdav_proxy", Name: "webdav_proxy",
@ -77,6 +84,13 @@ func GetDrivers() map[string][]Item {
}, },
}, v.Items()...) }, v.Items()...)
} }
res[k] = append(res[k], Item{
Name: "proxy_url",
Label: "proxy_url",
Type: TypeString,
Required: false,
Description: "proxy url",
})
} }
return res return res
} }

View File

@ -7,22 +7,21 @@ import (
) )
type Account struct { type Account struct {
ID uint `json:"id" gorm:"primaryKey"` ID uint `json:"id" gorm:"primaryKey"` // 唯一ID
Name string `json:"name" gorm:"unique" binding:"required"` Name string `json:"name" gorm:"unique" binding:"required"` // 唯一名称
Index int `json:"index"` Index int `json:"index"` // 序号 用于排序
Type string `json:"type"` Type string `json:"type"` // 类型即driver
Username string `json:"username"` Username string `json:"username"`
Password string `json:"password"` Password string `json:"password"`
RefreshToken string `json:"refresh_token"` RefreshToken string `json:"refresh_token"`
AccessToken string `json:"access_token"` AccessToken string `json:"access_token"`
RootFolder string `json:"root_folder"` RootFolder string `json:"root_folder"`
Status string `json:"status"` Status string `json:"status"` // 状态
CronId int CronId int
DriveId string DriveId string
Limit int `json:"limit"` Limit int `json:"limit"`
OrderBy string `json:"order_by"` OrderBy string `json:"order_by"`
OrderDirection string `json:"order_direction"` OrderDirection string `json:"order_direction"`
Proxy bool `json:"proxy"`
UpdatedAt *time.Time `json:"updated_at"` UpdatedAt *time.Time `json:"updated_at"`
Search bool `json:"search"` Search bool `json:"search"`
ClientId string `json:"client_id"` ClientId string `json:"client_id"`
@ -33,8 +32,9 @@ type Account struct {
SiteId string `json:"site_id"` SiteId string `json:"site_id"`
OnedriveType string `json:"onedrive_type"` OnedriveType string `json:"onedrive_type"`
WebdavProxy bool `json:"webdav_proxy"` WebdavProxy bool `json:"webdav_proxy"`
AllowProxy bool `json:"allow_proxy"` Proxy bool `json:"proxy"` // 是否中转
ProxyUrl string `json:"proxy_url"` //AllowProxy bool `json:"allow_proxy"` // 是否允许中转下载
ProxyUrl string `json:"proxy_url"` // 用于中转下载服务的URL
} }
var accountsMap = map[string]Account{} var accountsMap = map[string]Account{}

View File

@ -47,22 +47,29 @@ func Proxy(c *gin.Context) {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
// 只有三种情况允许中转:
// 1. 账号开启中转
// 2. driver只能中转
// 3. 是文本类型文件
if !account.Proxy && !driver.Config().OnlyProxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
common.ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
return
}
// 中转时有中转机器使用中转机器
if account.ProxyUrl != "" { if account.ProxyUrl != "" {
name := utils.Base(rawPath) name := utils.Base(rawPath)
link := fmt.Sprintf("%s%s?sign=%s", account.ProxyUrl, rawPath, utils.SignWithToken(name, conf.Token)) link := fmt.Sprintf("%s%s?sign=%s", account.ProxyUrl, rawPath, utils.SignWithToken(name, conf.Token))
c.Redirect(302, link) c.Redirect(302, link)
return return
} }
if !account.Proxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
common.ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
return
}
link, err := driver.Link(path, account) link, err := driver.Link(path, account)
if err != nil { if err != nil {
common.ErrorResp(c, err, 500) common.ErrorResp(c, err, 500)
return return
} }
// 本机文件直接返回文件
if account.Type == "Native" { if account.Type == "Native" {
// 对于名称为index.html的文件需要特殊处理
if utils.Base(rawPath) == "index.html" { if utils.Base(rawPath) == "index.html" {
file, err := os.Open(link) file, err := os.Open(link)
if err != nil { if err != nil {

View File

@ -2,6 +2,7 @@ package controllers
import ( import (
"fmt" "fmt"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/model" "github.com/Xhofe/alist/model"
"github.com/Xhofe/alist/server/common" "github.com/Xhofe/alist/server/common"
"github.com/Xhofe/alist/utils" "github.com/Xhofe/alist/utils"
@ -37,8 +38,13 @@ func Path(c *gin.Context) {
return return
} }
if file != nil { if file != nil {
if driver.Config().OnlyProxy { // 对于中转文件或只能中转,将链接修改为中转链接
file.Url = fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path) if driver.Config().OnlyProxy || account.Proxy {
if account.ProxyUrl != "" {
file.Url = fmt.Sprintf("%s%s?sign=%s", account.ProxyUrl, req.Path, utils.SignWithToken(file.Name, conf.Token))
}else {
file.Url = fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path)
}
} }
c.JSON(200, common.Resp{ c.JSON(200, common.Resp{
Code: 200, Code: 200,