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