增加自适应模式

This commit is contained in:
hoteas 2021-12-28 09:26:26 +08:00
parent a5c002bbce
commit 9c00ac6ba1
2 changed files with 38 additions and 19 deletions

View File

@ -302,6 +302,7 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) {
// 没有保存就生成随机的session
cookie, err := req.Cookie(that.Config.GetString("sessionName"))
sessionId := Md5(strconv.Itoa(Rand(10)))
needSetCookie := ""
token := req.Header.Get("Authorization")
if len(token) != 32 {
@ -314,16 +315,9 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) {
//没有token则查阅session
} else if err == nil && cookie.Value != "" {
sessionId = cookie.Value
//session也没有则判断是否创建cookie
} else {
//跨域不再通过cookie校验
if that.Config.GetString("crossDomain") == "" {
http.SetCookie(w, &http.Cookie{Name: that.Config.GetString("sessionName"), Value: sessionId, Path: "/"})
} else {
//跨域允许需要设置cookie的允许跨域https才有效果
w.Header().Set("Set-Cookie", that.Config.GetString("sessionName")+"="+sessionId+"; Path=/; SameSite=None; Secure")
}
needSetCookie = sessionId
}
unescapeUrl, err := url.QueryUnescape(req.RequestURI)
@ -342,7 +336,7 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) {
context.HandlerStr, context.RouterString = that.urlSer(context.HandlerStr)
//跨域设置
that.crossDomain(&context)
that.crossDomain(&context, needSetCookie)
defer func() {
//是否展示日志
@ -356,6 +350,7 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) {
ipStr = req.Header.Get("X-Real-IP")
}
}
that.WebConnectLog.Infoln(ipStr, context.Req.Method,
"time cost:", ObjToFloat64(time.Now().UnixNano()-nowUnixTime.UnixNano())/1000000.00, "ms",
"data length:", ObjToFloat64(context.DataSize)/1000.00, "KB", context.HandlerStr)
@ -434,9 +429,14 @@ func (that *Application) handler(w http.ResponseWriter, req *http.Request) {
}
func (that *Application) crossDomain(context *Context) {
func (that *Application) crossDomain(context *Context, sessionId string) {
//没有跨域设置
if context.Config.GetString("crossDomain") == "" {
if sessionId != "" {
http.SetCookie(context.Resp, &http.Cookie{Name: that.Config.GetString("sessionName"), Value: sessionId, Path: "/"})
}
return
}
@ -450,6 +450,10 @@ func (that *Application) crossDomain(context *Context) {
if context.Config.GetString("crossDomain") != "auto" {
//不跨域,则不设置
if strings.Contains(context.Config.GetString("crossDomain"), remoteHost) {
if sessionId != "" {
http.SetCookie(context.Resp, &http.Cookie{Name: that.Config.GetString("sessionName"), Value: sessionId, Path: "/"})
}
return
}
header.Set("Access-Control-Allow-Origin", that.Config.GetString("crossDomain"))
@ -462,22 +466,29 @@ func (that *Application) crossDomain(context *Context) {
header.Set("Access-Control-Expose-Headers", "*")
header.Set("Access-Control-Allow-Headers", "X-Requested-With,Content-Type,Access-Token")
if sessionId != "" {
//跨域允许需要设置cookie的允许跨域https才有效果
context.Resp.Header().Set("Set-Cookie", that.Config.GetString("sessionName")+"="+sessionId+"; Path=/; SameSite=None; Secure")
}
return
}
origin := context.Req.Header.Get("Origin")
refer := context.Req.Header.Get("Referer")
if strings.Contains(origin, remoteHost) || strings.Contains(refer, remoteHost) {
if (origin != "" && strings.Contains(origin, remoteHost)) || strings.Contains(refer, remoteHost) {
if sessionId != "" {
http.SetCookie(context.Resp, &http.Cookie{Name: that.Config.GetString("sessionName"), Value: sessionId, Path: "/"})
}
return
}
if origin != "" {
header.Set("Access-Control-Allow-Origin", origin)
//return
}
if refer != "" {
} else if refer != "" {
tempInt := 0
lastInt := strings.IndexFunc(refer, func(r rune) bool {
if r == '/' && tempInt > 8 {
@ -493,11 +504,19 @@ func (that *Application) crossDomain(context *Context) {
refer = Substr(refer, 0, lastInt)
header.Set("Access-Control-Allow-Origin", refer)
//header.Set("Access-Control-Allow-Origin", "*")
}
header.Set("Access-Control-Allow-Methods", "GET,POST,OPTIONS,PUT,DELETE")
header.Set("Access-Control-Allow-Credentials", "true")
header.Set("Access-Control-Expose-Headers", "*")
header.Set("Access-Control-Allow-Headers", "X-Requested-With,Content-Type,Access-Token")
if sessionId != "" {
//跨域允许需要设置cookie的允许跨域https才有效果
context.Resp.Header().Set("Set-Cookie", that.Config.GetString("sessionName")+"="+sessionId+"; Path=/; SameSite=None; Secure")
}
}
//Init 初始化application

View File

@ -1,3 +1,3 @@
<!DOCTYPE html><html lang=""><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><link rel="icon" href="favicon.ico"><title></title><style>body{
margin: 0px;
}</style><link href="css/chunk-1dd2a8d0.e4ca99de.css" rel="prefetch"><link href="css/chunk-37187220.eb2402e0.css" rel="prefetch"><link href="css/chunk-8481683a.0e0eeebd.css" rel="prefetch"><link href="css/chunk-a74869b6.c460e209.css" rel="prefetch"><link href="css/chunk-c5fa0a04.c530e31c.css" rel="prefetch"><link href="css/chunk-d1a9ebe6.5cc24c46.css" rel="prefetch"><link href="js/chunk-1dd2a8d0.c3cd4fb6.js" rel="prefetch"><link href="js/chunk-2c065dd6.99c035f4.js" rel="prefetch"><link href="js/chunk-37187220.ed5ac584.js" rel="prefetch"><link href="js/chunk-58db4e7f.c298e695.js" rel="prefetch"><link href="js/chunk-6581ae4b.953630b3.js" rel="prefetch"><link href="js/chunk-78ba61e2.520b239c.js" rel="prefetch"><link href="js/chunk-8481683a.ca77d416.js" rel="prefetch"><link href="js/chunk-a74869b6.59e9b13d.js" rel="prefetch"><link href="js/chunk-c5fa0a04.3f11e245.js" rel="prefetch"><link href="js/chunk-d1a9ebe6.fba0f501.js" rel="prefetch"><link href="css/app.5e2eb449.css" rel="preload" as="style"><link href="js/app.a2f36579.js" rel="preload" as="script"><link href="css/app.5e2eb449.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but hotime doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="js/app.a2f36579.js"></script></body></html>
}</style><link href="css/chunk-2b8aef56.7087d841.css" rel="prefetch"><link href="css/chunk-38db7d04.2b6ce0ac.css" rel="prefetch"><link href="css/chunk-5c99f384.31e35517.css" rel="prefetch"><link href="css/chunk-60f282ff.83752cba.css" rel="prefetch"><link href="css/chunk-a74869b6.c460e209.css" rel="prefetch"><link href="css/chunk-d1a9ebe6.5cc24c46.css" rel="prefetch"><link href="js/chunk-28c289a1.0ed6fefe.js" rel="prefetch"><link href="js/chunk-2b8aef56.8330998b.js" rel="prefetch"><link href="js/chunk-2c065dd6.d9c3e429.js" rel="prefetch"><link href="js/chunk-38db7d04.18ee879a.js" rel="prefetch"><link href="js/chunk-58db4e7f.c298e695.js" rel="prefetch"><link href="js/chunk-5c99f384.be52d852.js" rel="prefetch"><link href="js/chunk-60f282ff.cbb91cc0.js" rel="prefetch"><link href="js/chunk-78ba61e2.520b239c.js" rel="prefetch"><link href="js/chunk-a74869b6.01e5db7b.js" rel="prefetch"><link href="js/chunk-d1a9ebe6.fba0f501.js" rel="prefetch"><link href="css/app.5e2eb449.css" rel="preload" as="style"><link href="js/app.c87636c4.js" rel="preload" as="script"><link href="css/app.5e2eb449.css" rel="stylesheet"></head><body><noscript><strong>We're sorry but hotime doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div><script src="js/app.c87636c4.js"></script></body></html>