🉑 origin优化
This commit is contained in:
@ -26,6 +26,6 @@ func ReadConf(config string) bool {
|
||||
return false
|
||||
}
|
||||
log.Debugf("config:%+v",conf.Conf)
|
||||
conf.Origins = strings.Split(conf.Conf.Info.SiteUrl,",")
|
||||
conf.Origins = strings.Split(conf.Conf.Server.SiteUrl,",")
|
||||
return true
|
||||
}
|
@ -3,6 +3,7 @@ package bootstrap
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
log "github.com/sirupsen/logrus"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
@ -32,8 +33,10 @@ func CheckUpdate() {
|
||||
log.Warnf("解析更新失败:%s",err.Error())
|
||||
return
|
||||
}
|
||||
if conf.VERSION == release.TagName {
|
||||
log.Infof("当前已是最新版本:%s",release.TagName)
|
||||
lasted:=release.TagName[1:]
|
||||
now:=conf.VERSION[1:]
|
||||
if utils.VersionCompare(lasted,now) != 1 {
|
||||
log.Infof("当前已是最新版本:%s",conf.VERSION)
|
||||
}else {
|
||||
log.Infof("发现新版本:%s",release.TagName)
|
||||
log.Infof("请至'%s'获取更新.",release.HtmlUrl)
|
||||
|
@ -3,8 +3,6 @@ package conf
|
||||
type Config struct {
|
||||
Info struct{
|
||||
Title string `yaml:"title" json:"title"`
|
||||
SiteUrl string `yaml:"site_url" json:"site_url"`//网站url
|
||||
BackendUrl string `yaml:"backend_url" json:"backend_url"`//后端地址
|
||||
Logo string `yaml:"logo" json:"logo"`
|
||||
FooterText string `yaml:"footer_text" json:"footer_text"`
|
||||
FooterUrl string `yaml:"footer_url" json:"footer_url"`
|
||||
@ -24,6 +22,7 @@ type Config struct {
|
||||
Port string `yaml:"port"`//端口
|
||||
Search bool `yaml:"search" json:"search"`//允许搜索
|
||||
Static string `yaml:"static"`
|
||||
SiteUrl string `yaml:"site_url" json:"site_url"`//网站url
|
||||
} `yaml:"server"`
|
||||
Cache struct{
|
||||
Enable bool `yaml:"enable"`
|
||||
|
@ -2,7 +2,7 @@ package controllers
|
||||
|
||||
import "github.com/gin-gonic/gin"
|
||||
|
||||
func metaResponse(code int, msg string) gin.H {
|
||||
func MetaResponse(code int, msg string) gin.H {
|
||||
return gin.H{
|
||||
"meta":gin.H{
|
||||
"code":code,
|
||||
@ -11,7 +11,7 @@ func metaResponse(code int, msg string) gin.H {
|
||||
}
|
||||
}
|
||||
|
||||
func dataResponse(data interface{}) gin.H {
|
||||
func DataResponse(data interface{}) gin.H {
|
||||
return gin.H{
|
||||
"meta":gin.H{
|
||||
"code":200,
|
||||
|
@ -12,7 +12,7 @@ import (
|
||||
func Get(c *gin.Context) {
|
||||
var get alidrive.GetReq
|
||||
if err := c.ShouldBindJSON(&get); err != nil {
|
||||
c.JSON(200,metaResponse(400,"Bad Request"))
|
||||
c.JSON(200, MetaResponse(400,"Bad Request"))
|
||||
return
|
||||
}
|
||||
log.Debugf("get:%+v",get)
|
||||
@ -22,25 +22,25 @@ func Get(c *gin.Context) {
|
||||
// file,exist:=conf.Cache.Get(cacheKey)
|
||||
// if exist {
|
||||
// log.Debugf("使用了缓存:%s",cacheKey)
|
||||
// c.JSON(200,dataResponse(file))
|
||||
// c.JSON(200,DataResponse(file))
|
||||
// return
|
||||
// }
|
||||
//}
|
||||
file,err:=alidrive.GetFile(get.FileId)
|
||||
if err !=nil {
|
||||
c.JSON(200,metaResponse(500,err.Error()))
|
||||
c.JSON(200, MetaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
paths,err:=alidrive.GetPaths(get.FileId)
|
||||
if err!=nil {
|
||||
c.JSON(200,metaResponse(500,err.Error()))
|
||||
c.JSON(200, MetaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
file.Paths=*paths
|
||||
//if conf.Conf.Cache.Enable {
|
||||
// conf.Cache.Set(cacheKey,file,cache.DefaultExpiration)
|
||||
//}
|
||||
c.JSON(200,dataResponse(file))
|
||||
c.JSON(200, DataResponse(file))
|
||||
}
|
||||
|
||||
func Down(c *gin.Context) {
|
||||
@ -58,7 +58,7 @@ func Down(c *gin.Context) {
|
||||
//}
|
||||
file,err:=alidrive.GetFile(fileId)
|
||||
if err != nil {
|
||||
c.JSON(200, metaResponse(500,err.Error()))
|
||||
c.JSON(200, MetaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
//if conf.Conf.Cache.Enable {
|
||||
|
@ -17,7 +17,7 @@ type ListReq struct {
|
||||
func List(c *gin.Context) {
|
||||
var list ListReq
|
||||
if err := c.ShouldBindJSON(&list);err!=nil {
|
||||
c.JSON(200, metaResponse(400,"Bad Request"))
|
||||
c.JSON(200, MetaResponse(400,"Bad Request"))
|
||||
return
|
||||
}
|
||||
log.Debugf("list:%+v",list)
|
||||
@ -27,7 +27,7 @@ func List(c *gin.Context) {
|
||||
files,exist:=conf.Cache.Get(cacheKey)
|
||||
if exist {
|
||||
log.Debugf("使用了缓存:%s",cacheKey)
|
||||
c.JSON(200, dataResponse(files))
|
||||
c.JSON(200, DataResponse(files))
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -47,21 +47,21 @@ func List(c *gin.Context) {
|
||||
files,err=alidrive.GetList(list.ParentFileId,list.Limit,list.Marker,list.OrderBy,list.OrderDirection)
|
||||
}
|
||||
if err!=nil {
|
||||
c.JSON(200, metaResponse(500,err.Error()))
|
||||
c.JSON(200, MetaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
password:=alidrive.HasPassword(files)
|
||||
if password!="" && password!=list.Password {
|
||||
if list.Password=="" {
|
||||
c.JSON(200, metaResponse(401,"need password."))
|
||||
c.JSON(200, MetaResponse(401,"need password."))
|
||||
return
|
||||
}
|
||||
c.JSON(200, metaResponse(401,"wrong password."))
|
||||
c.JSON(200, MetaResponse(401,"wrong password."))
|
||||
return
|
||||
}
|
||||
paths,err:=alidrive.GetPaths(list.ParentFileId)
|
||||
if err!=nil {
|
||||
c.JSON(200, metaResponse(500,err.Error()))
|
||||
c.JSON(200, MetaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
files.Paths=*paths
|
||||
@ -69,5 +69,5 @@ func List(c *gin.Context) {
|
||||
if conf.Conf.Cache.Enable {
|
||||
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
|
||||
}
|
||||
c.JSON(200, dataResponse(files))
|
||||
c.JSON(200, DataResponse(files))
|
||||
}
|
@ -11,12 +11,12 @@ import (
|
||||
|
||||
func Search(c *gin.Context) {
|
||||
if !conf.Conf.Server.Search {
|
||||
c.JSON(200, metaResponse(403,"Not allow search."))
|
||||
c.JSON(200, MetaResponse(403,"Not allow search."))
|
||||
return
|
||||
}
|
||||
var search alidrive.SearchReq
|
||||
if err := c.ShouldBindJSON(&search); err != nil {
|
||||
c.JSON(200, metaResponse(400,"Bad Request"))
|
||||
c.JSON(200, MetaResponse(400,"Bad Request"))
|
||||
return
|
||||
}
|
||||
log.Debugf("search:%+v",search)
|
||||
@ -26,7 +26,7 @@ func Search(c *gin.Context) {
|
||||
files,exist:=conf.Cache.Get(cacheKey)
|
||||
if exist {
|
||||
log.Debugf("使用了缓存:%s",cacheKey)
|
||||
c.JSON(200, dataResponse(files))
|
||||
c.JSON(200, DataResponse(files))
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -39,11 +39,11 @@ func Search(c *gin.Context) {
|
||||
//}
|
||||
files,err:=alidrive.Search(search.Query,search.Limit,search.OrderBy)
|
||||
if err != nil {
|
||||
c.JSON(200, metaResponse(500,err.Error()))
|
||||
c.JSON(200, MetaResponse(500,err.Error()))
|
||||
return
|
||||
}
|
||||
if conf.Conf.Cache.Enable {
|
||||
conf.Cache.Set(cacheKey,files,cache.DefaultExpiration)
|
||||
}
|
||||
c.JSON(200, dataResponse(files))
|
||||
c.JSON(200, DataResponse(files))
|
||||
}
|
@ -6,7 +6,7 @@ import (
|
||||
)
|
||||
|
||||
func Info(c *gin.Context) {
|
||||
c.JSON(200, dataResponse(conf.Conf.Info))
|
||||
c.JSON(200, DataResponse(conf.Conf.Info))
|
||||
}
|
||||
|
||||
func RefreshCache(c *gin.Context) {
|
||||
@ -14,12 +14,12 @@ func RefreshCache(c *gin.Context) {
|
||||
if conf.Conf.Cache.Enable {
|
||||
if password == conf.Conf.Cache.RefreshPassword {
|
||||
conf.Cache.Flush()
|
||||
c.JSON(200,metaResponse(200,"flush success."))
|
||||
c.JSON(200, MetaResponse(200,"flush success."))
|
||||
return
|
||||
}
|
||||
c.JSON(200,metaResponse(401,"wrong password."))
|
||||
c.JSON(200, MetaResponse(401,"wrong password."))
|
||||
return
|
||||
}
|
||||
c.JSON(200,metaResponse(400,"disabled cache."))
|
||||
c.JSON(200, MetaResponse(400,"disabled cache."))
|
||||
return
|
||||
}
|
@ -2,31 +2,28 @@ package server
|
||||
|
||||
import (
|
||||
"github.com/Xhofe/alist/conf"
|
||||
"github.com/Xhofe/alist/server/controllers"
|
||||
"github.com/Xhofe/alist/utils"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func CrosHandler() gin.HandlerFunc {
|
||||
return func(context *gin.Context) {
|
||||
method := context.Request.Method
|
||||
// 设置跨域
|
||||
if conf.Conf.Info.SiteUrl=="*"||utils.ContainsString(conf.Origins,context.GetHeader("Origin"))!=-1 {
|
||||
context.Header("Access-Control-Allow-Origin",context.GetHeader("Origin"))
|
||||
}else {
|
||||
context.Header("Access-Control-Allow-Origin", conf.Conf.Info.SiteUrl)//跨域访问
|
||||
}
|
||||
context.Header("Access-Control-Allow-Origin",context.GetHeader("Origin"))
|
||||
context.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE,UPDATE")
|
||||
context.Header("Access-Control-Allow-Headers", "Content-Length,session,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language, Keep-Alive, User-Agent, Cache-Control, Content-Type, Pragma")
|
||||
context.Header("Access-Control-Expose-Headers", "Content-Length, Access-Control-Allow-Origin, Access-Control-Allow-Headers,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified,Pragma,FooBar")
|
||||
context.Header("Access-Control-Allow-Headers", "Content-Length,session,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language, Keep-Alive, User-Agent, Cache-Control, Content-Type")
|
||||
context.Header("Access-Control-Expose-Headers", "Content-Length,Cache-Control,Content-Language,Content-Type,Expires,Last-Modified")
|
||||
context.Header("Access-Control-Max-Age", "172800")
|
||||
context.Header("Access-Control-Allow-Credentials", "true")
|
||||
//context.Set("content-type", "application/json") //设置返回格式是json
|
||||
|
||||
if method == "OPTIONS" {
|
||||
context.JSON(http.StatusOK, gin.H{})
|
||||
// 信任域名
|
||||
if conf.Conf.Server.SiteUrl!="*"&&utils.ContainsString(conf.Origins,context.GetHeader("Origin"))==-1 {
|
||||
context.JSON(200,controllers.MetaResponse(413,"The origin is not in the site_url list, please configure it correctly."))
|
||||
context.Abort()
|
||||
}
|
||||
if method == "OPTIONS" {
|
||||
context.AbortWithStatus(204)
|
||||
}
|
||||
|
||||
//处理请求
|
||||
context.Next()
|
||||
}
|
||||
|
@ -3,6 +3,8 @@ package utils
|
||||
import (
|
||||
log "github.com/sirupsen/logrus"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetCode(rawUrl string) string {
|
||||
@ -24,4 +26,30 @@ func ContainsString(array []string, val string) (index int) {
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func VersionCompare(version1, version2 string) int {
|
||||
a := strings.Split(version1, ".")
|
||||
b := strings.Split(version2, ".")
|
||||
flag := 1
|
||||
if len(a) > len(b) {
|
||||
a, b = b, a
|
||||
flag = -1
|
||||
}
|
||||
for i := range a {
|
||||
x, _ := strconv.Atoi(a[i])
|
||||
y, _ := strconv.Atoi(b[i])
|
||||
if x < y {
|
||||
return -1 * flag
|
||||
} else if x > y {
|
||||
return 1 * flag
|
||||
}
|
||||
}
|
||||
for _, v:= range b[len(a):] {
|
||||
y, _ := strconv.Atoi(v)
|
||||
if y > 0 {
|
||||
return -1 * flag
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
Reference in New Issue
Block a user