add proxy

This commit is contained in:
微凉
2021-09-04 19:52:56 +08:00
parent eefe53ff01
commit b51b4e083d
7 changed files with 107 additions and 7 deletions

View File

@ -31,6 +31,7 @@ func ReadConf(config string) bool {
log.Debugf("config:%+v", conf.Conf)
conf.Conf.Info.Roots = utils.GetNames()
conf.Origins = strings.Split(conf.Conf.Server.SiteUrl, ",")
conf.AllowProxies = strings.Split(conf.Conf.Server.AllowProxy,",")
return true
}
func Write(path string) bool {
@ -63,6 +64,7 @@ server:
static: dist
site_url: '*'
password: password #用于重建目录
allow_proxy: vtt
ali_drive:
api_url: https://api.aliyundrive.com/v2
max_files_count: 100

View File

@ -17,6 +17,7 @@ server:
static: dist
site_url: '*'
password: password #用于重建目录
allow_proxy: vtt #允许代理的后缀,以,分割
ali_drive:
api_url: https://api.aliyundrive.com/v2
max_files_count: 50 #重建目录时每次请求的文件

View File

@ -31,13 +31,14 @@ type Config struct {
} `yaml:"preview" json:"preview"`
} `yaml:"info"`
Server struct {
Address string `yaml:"address"`
Port string `yaml:"port"` //端口
Search bool `yaml:"search"` //允许搜索
Download bool `yaml:"download"` //允许下载
Static string `yaml:"static"`
SiteUrl string `yaml:"site_url"` //网站url
Password string `yaml:"password"`
Address string `yaml:"address"`
Port string `yaml:"port"` //端口
Search bool `yaml:"search"` //允许搜索
Download bool `yaml:"download"` //允许下载
Static string `yaml:"static"`
SiteUrl string `yaml:"site_url"` //网站url
Password string `yaml:"password"`
AllowProxy string `yaml:"allow_proxy"` // 允许代理的后缀
} `yaml:"server"`
AliDrive struct {
ApiUrl string `yaml:"api_url"` //阿里云盘api

View File

@ -17,6 +17,7 @@ var (
DB *gorm.DB
Origins []string // allow origins
AllowProxies []string
)
var Conf = new(Config)

View File

@ -0,0 +1,84 @@
package controllers
import (
"github.com/Xhofe/alist/alidrive"
"github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/server/models"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin"
log "github.com/sirupsen/logrus"
"net/http/httputil"
url2 "net/url"
"path/filepath"
"strings"
)
type ProxyReq struct {
Password string `form:"pw"`
}
// Down handle download request
func Proxy(c *gin.Context) {
if !conf.Conf.Server.Download {
c.JSON(200, MetaResponse(403, "不允许下载或预览."))
return
}
filePath := c.Param("path")[1:]
if !utils.HasSuffixes(filePath, conf.AllowProxies) {
c.JSON(200, MetaResponse(403, "该类型文件不允许代理."))
return
}
var down ProxyReq
if err := c.ShouldBindQuery(&down); err != nil {
c.JSON(200, MetaResponse(400, "错误的请求."))
return
}
log.Debugf("down:%s", filePath)
dir, name := filepath.Split(filePath)
fileModel, err := models.GetFileByDirAndName(dir, name)
if err != nil {
if fileModel == nil {
c.JSON(200, MetaResponse(404, "文件不存在."))
return
}
c.JSON(200, MetaResponse(500, err.Error()))
return
}
if fileModel.Password != "" && down.Password != utils.Get16MD5Encode(fileModel.Password) {
if down.Password == "" {
c.JSON(200, MetaResponse(401, "需要密码."))
} else {
c.JSON(200, MetaResponse(401, "密码错误."))
}
return
}
if fileModel.Type == "folder" {
c.JSON(200, MetaResponse(406, "无法代理目录."))
return
}
drive := utils.GetDriveByName(strings.Split(filePath, "/")[0])
if drive == nil {
c.JSON(200, MetaResponse(500, "找不到drive."))
return
}
file, err := alidrive.GetDownLoadUrl(fileModel.FileId, drive)
if err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return
}
url, err := url2.Parse(file.Url)
if err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return
}
target, err := url2.Parse("https://" + url.Host)
if err != nil {
c.JSON(200, MetaResponse(500, err.Error()))
return
}
proxy := httputil.NewSingleHostReverseProxy(target)
c.Request.URL = url
c.Request.Host = url.Host
c.Request.Header.Del("Origin")
proxy.ServeHTTP(c.Writer, c.Request)
}

View File

@ -34,5 +34,6 @@ func InitApiRouter(engine *gin.Engine) {
apiV2.POST("/video_preview/:drive", controllers.VideoPreview)
apiV2.POST("/video_preview_play_info/:drive", controllers.VideoPreviewPlayInfo)
engine.GET("/d/*path", controllers.Down)
engine.GET("/p/*path",controllers.Proxy)
}
}

View File

@ -56,3 +56,13 @@ func VersionCompare(version1, version2 string) int {
}
return 0
}
// HasSuffixes check string has suffixes in string array.
func HasSuffixes(str string, suffixes []string) bool {
for _, suffix := range suffixes {
if strings.HasSuffix(str,suffix) {
return true
}
}
return false
}