mirror of https://github.com/Xhofe/alist
🚧 support proxy url
parent
b8698700ef
commit
190c8001a5
|
@ -101,7 +101,7 @@ func (driver Alist) Link(path string, account *model.Account) (string, error) {
|
||||||
if utils.GetFileType(filepath.Ext(path)) == conf.TEXT {
|
if utils.GetFileType(filepath.Ext(path)) == conf.TEXT {
|
||||||
flag = "p"
|
flag = "p"
|
||||||
}
|
}
|
||||||
return fmt.Sprintf("%s/%s%s?sign=%s", account.SiteUrl, flag, path, utils.Get16MD5Encode(fmt.Sprintf("%s-%s", conf.Token, name))), nil
|
return fmt.Sprintf("%s/%s%s?sign=%s", account.SiteUrl, flag, path, utils.SignWithToken(name,conf.Token)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (driver Alist) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
func (driver Alist) Path(path string, account *model.Account) (*model.File, []model.File, error) {
|
||||||
|
|
|
@ -9,8 +9,9 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type DriverConfig struct {
|
type DriverConfig struct {
|
||||||
Name string
|
Name string
|
||||||
OnlyProxy bool
|
OnlyProxy bool
|
||||||
|
NeedHeader bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type Driver interface {
|
type Driver interface {
|
||||||
|
|
|
@ -15,8 +15,9 @@ type GoogleDrive struct{}
|
||||||
|
|
||||||
func (driver GoogleDrive) Config() base.DriverConfig {
|
func (driver GoogleDrive) Config() base.DriverConfig {
|
||||||
return base.DriverConfig{
|
return base.DriverConfig{
|
||||||
Name: "GoogleDrive",
|
Name: "GoogleDrive",
|
||||||
OnlyProxy: true,
|
OnlyProxy: true,
|
||||||
|
NeedHeader: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -33,6 +33,8 @@ 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"`
|
||||||
|
ProxyUrl string `json:"proxy_url"`
|
||||||
}
|
}
|
||||||
|
|
||||||
var accountsMap = map[string]Account{}
|
var accountsMap = map[string]Account{}
|
||||||
|
|
|
@ -35,7 +35,7 @@ func CheckDownLink(path string, passwordMd5 string, name string) bool {
|
||||||
log.Debugf("check down path: %s", path)
|
log.Debugf("check down path: %s", path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
log.Debugf("check down link: %s,%s", meta.Password, passwordMd5)
|
log.Debugf("check down link: %s,%s", meta.Password, passwordMd5)
|
||||||
if meta.Password != "" && utils.Get16MD5Encode("alist"+meta.Password+name) != passwordMd5 {
|
if meta.Password != "" && utils.SignWithPassword(name, meta.Password) != passwordMd5 {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
|
|
@ -23,7 +23,7 @@ func Down(c *gin.Context) {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if driver.Config().OnlyProxy {
|
if driver.Config().OnlyProxy || account.Proxy {
|
||||||
Proxy(c)
|
Proxy(c)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,12 @@ func Proxy(c *gin.Context) {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if account.ProxyUrl != "" {
|
||||||
|
name := utils.Base(rawPath)
|
||||||
|
link := fmt.Sprintf("%s%s?sign=%s", account.ProxyUrl, rawPath, utils.SignWithToken(name, conf.Token))
|
||||||
|
c.Redirect(302, link)
|
||||||
|
return
|
||||||
|
}
|
||||||
if !account.Proxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
|
if !account.Proxy && utils.GetFileType(filepath.Ext(rawPath)) != conf.TEXT {
|
||||||
common.ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
|
common.ErrorResp(c, fmt.Errorf("[%s] not allowed proxy", account.Name), 403)
|
||||||
return
|
return
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func Path(c *gin.Context) {
|
func Path(c *gin.Context) {
|
||||||
reqV,_ := c.Get("req")
|
reqV, _ := c.Get("req")
|
||||||
req := reqV.(common.PathReq)
|
req := reqV.(common.PathReq)
|
||||||
if model.AccountsCount() > 1 && req.Path == "/" {
|
if model.AccountsCount() > 1 && req.Path == "/" {
|
||||||
files, err := model.GetAccountFiles()
|
files, err := model.GetAccountFiles()
|
||||||
|
@ -66,7 +66,7 @@ func Path(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Link(c *gin.Context) {
|
func Link(c *gin.Context) {
|
||||||
reqV,_ := c.Get("req")
|
reqV, _ := c.Get("req")
|
||||||
req := reqV.(common.PathReq)
|
req := reqV.(common.PathReq)
|
||||||
rawPath := req.Path
|
rawPath := req.Path
|
||||||
rawPath = utils.ParsePath(rawPath)
|
rawPath = utils.ParsePath(rawPath)
|
||||||
|
@ -81,6 +81,16 @@ func Link(c *gin.Context) {
|
||||||
common.ErrorResp(c, err, 500)
|
common.ErrorResp(c, err, 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if driver.Config().NeedHeader {
|
||||||
|
common.SuccessResp(c, gin.H{
|
||||||
|
"url": link,
|
||||||
|
"header": gin.H{
|
||||||
|
"name": "Authorization",
|
||||||
|
"value": "Bearer " + account.AccessToken,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
if driver.Config().OnlyProxy {
|
if driver.Config().OnlyProxy {
|
||||||
common.SuccessResp(c, gin.H{
|
common.SuccessResp(c, gin.H{
|
||||||
"url": fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path),
|
"url": fmt.Sprintf("//%s/d%s", c.Request.Host, req.Path),
|
||||||
|
@ -95,7 +105,7 @@ func Link(c *gin.Context) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Preview(c *gin.Context) {
|
func Preview(c *gin.Context) {
|
||||||
reqV,_ := c.Get("req")
|
reqV, _ := c.Get("req")
|
||||||
req := reqV.(common.PathReq)
|
req := reqV.(common.PathReq)
|
||||||
rawPath := req.Path
|
rawPath := req.Path
|
||||||
rawPath = utils.ParsePath(rawPath)
|
rawPath = utils.ParsePath(rawPath)
|
||||||
|
|
|
@ -13,7 +13,7 @@ func DownCheck(c *gin.Context) {
|
||||||
rawPath := c.Param("path")
|
rawPath := c.Param("path")
|
||||||
rawPath = utils.ParsePath(rawPath)
|
rawPath = utils.ParsePath(rawPath)
|
||||||
name := utils.Base(rawPath)
|
name := utils.Base(rawPath)
|
||||||
if sign == utils.Get16MD5Encode(fmt.Sprintf("%s-%s", conf.Token, name)) {
|
if sign == utils.SignWithToken(name, conf.Token) {
|
||||||
c.Next()
|
c.Next()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,8 @@ func InitApiRouter(r *gin.Engine) {
|
||||||
path := public.Group("", middlewares.PathCheck, middlewares.CheckAccount)
|
path := public.Group("", middlewares.PathCheck, middlewares.CheckAccount)
|
||||||
path.POST("/path", controllers.Path)
|
path.POST("/path", controllers.Path)
|
||||||
path.POST("/preview", controllers.Preview)
|
path.POST("/preview", controllers.Preview)
|
||||||
path.POST("/link", controllers.Link)
|
|
||||||
|
//path.POST("/link",middlewares.Auth, controllers.Link)
|
||||||
|
|
||||||
public.GET("/settings", controllers.GetSettingsPublic)
|
public.GET("/settings", controllers.GetSettingsPublic)
|
||||||
}
|
}
|
||||||
|
@ -43,6 +44,8 @@ func InitApiRouter(r *gin.Engine) {
|
||||||
admin.POST("/meta/create", controllers.CreateMeta)
|
admin.POST("/meta/create", controllers.CreateMeta)
|
||||||
admin.POST("/meta/save", controllers.SaveMeta)
|
admin.POST("/meta/save", controllers.SaveMeta)
|
||||||
admin.DELETE("/meta", controllers.DeleteMeta)
|
admin.DELETE("/meta", controllers.DeleteMeta)
|
||||||
|
|
||||||
|
admin.POST("/link", controllers.Link)
|
||||||
}
|
}
|
||||||
Static(r)
|
Static(r)
|
||||||
WebDav(r)
|
WebDav(r)
|
||||||
|
|
|
@ -87,7 +87,7 @@ func GetPW(path string, name string) string {
|
||||||
meta, err := model.GetMetaByPath(path)
|
meta, err := model.GetMetaByPath(path)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if meta.Password != "" {
|
if meta.Password != "" {
|
||||||
utils.Get16MD5Encode("alist" + meta.Password + name)
|
return utils.SignWithPassword(name, meta.Password)
|
||||||
}
|
}
|
||||||
return ""
|
return ""
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -3,6 +3,7 @@ package utils
|
||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetMD5Encode
|
// GetMD5Encode
|
||||||
|
@ -16,3 +17,11 @@ func GetMD5Encode(data string) string {
|
||||||
func Get16MD5Encode(data string) string {
|
func Get16MD5Encode(data string) string {
|
||||||
return GetMD5Encode(data)[8:24]
|
return GetMD5Encode(data)[8:24]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func SignWithPassword(name, password string) string {
|
||||||
|
return Get16MD5Encode(fmt.Sprintf("alist-%s-%s", password, name))
|
||||||
|
}
|
||||||
|
|
||||||
|
func SignWithToken(name, token string) string {
|
||||||
|
return Get16MD5Encode(fmt.Sprintf("alist-%s-%s", token, name))
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue