🐛 多域名跨域

This commit is contained in:
微凉
2021-01-08 16:32:02 +08:00
parent 0cd4624a36
commit dcebf5257f
6 changed files with 26 additions and 6 deletions

View File

@ -1,4 +1,4 @@
name: Build name: build
on: on:
push: push:

View File

@ -1,4 +1,4 @@
name: Release name: release
on: on:
push: push:
tags: tags:

View File

@ -6,6 +6,7 @@ import (
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
"io/ioutil" "io/ioutil"
"strings"
) )
func ReadConf(config string) bool { func ReadConf(config string) bool {
@ -25,5 +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,",")
return true return true
} }

View File

@ -13,12 +13,14 @@ var(
Authorization string Authorization string
Cache *cache.Cache Cache *cache.Cache
Origins []string
) )
var Conf = new(Config) var Conf = new(Config)
const ( const (
VERSION="v0.1.4" VERSION="v0.1.5"
ImageThumbnailProcess="image/resize,w_50" ImageThumbnailProcess="image/resize,w_50"
VideoThumbnailProcess="video/snapshot,t_0,f_jpg,w_50" VideoThumbnailProcess="video/snapshot,t_0,f_jpg,w_50"

View File

@ -2,6 +2,7 @@ package server
import ( import (
"github.com/Xhofe/alist/conf" "github.com/Xhofe/alist/conf"
"github.com/Xhofe/alist/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"net/http" "net/http"
) )
@ -9,10 +10,14 @@ import (
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
context.Writer.Header().Set("Access-Control-Allow-Origin", "*") // 设置跨域
context.Header("Access-Control-Allow-Origin", conf.Conf.Info.SiteUrl) // 设置允许访问所有域 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-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", "Authorization, Content-Length, X-CSRF-Token, Token,session,X_Requested_With,Accept, Origin, Host, Connection, Accept-Encoding, Accept-Language,DNT, X-CustomHeader, Keep-Alive, User-Agent, X-Requested-With, If-Modified-Since, Cache-Control, Content-Type, Pragma,token,openid,opentoken") 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-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-Max-Age", "172800") context.Header("Access-Control-Max-Age", "172800")
context.Header("Access-Control-Allow-Credentials", "true") context.Header("Access-Control-Allow-Credentials", "true")

View File

@ -13,4 +13,15 @@ func GetCode(rawUrl string) string {
} }
code:=u.Query().Get("code") code:=u.Query().Get("code")
return code return code
}
func ContainsString(array []string, val string) (index int) {
index = -1
for i := 0; i < len(array); i++ {
if array[i] == val {
index = i
return
}
}
return
} }