package middleware import ( "server/call" "server/common" "server/db" "server/modules/web/app" "server/modules/web/values" "server/util" "strconv" "strings" "github.com/gin-gonic/gin" ) var ( passURLs = map[string]struct{}{ "/firstpage": {}, "/game/list": {}, "/sys/config": {}, "/account/email/code": {}, "/account/email/regist": {}, "/account/email/login": {}, "/account/email/resetPass": {}, "/account/guestLogin": {}, "/account/gpLogin": {}, "/account/fbLogin": {}, "/account/tokenLogin": {}, "/account/phoneCode/get": {}, "/account/phoneCode/verify": {}, "/account/phoneCode/regist": {}, "/account/phoneCode/login": {}, "/share/upload": {}, "/share/config": {}, "/game/enter": {}, "/activity/appSpin/info": {}, "/activity/pdd/info": {}, "/account/phone/regist": {}, "/account/phone/login": {}, "/account/phone/resetPass": {}, "/balance/recharge/info": {}, "/share/info": {}, "/vip/info": {}, "/share/reference": {}, "/share/report": {}, "/share/transfer": {}, "/task/info": {}, "/activity/freeSpin/info": {}, "/promotions": {}, "/tg/luckyCode": {}, "/activity/sign/info": {}, "/ad/uploadFB": {}, "/activity/slots/info": {}, "/activity/sign/new/info": {}, "/activity/betDraw/info": {}, "/activity/betDraw/record": {}, "/activity/activityPopup/info": {}, } ) // 进行token校验 func TokenMiddleWare() gin.HandlerFunc { return func(c *gin.Context) { path := c.Request.RequestURI if PassURL(path) { c.Next() return } token := c.GetHeader("token") a := app.NewApp(c) uid, _ := db.Redis().GetInt(common.GetRedisKeyToken(token)) if uid == 0 { a.Code = values.CodeToken a.Response() c.Abort() return } c.Set("uid", uid) c.Set("token", token) c.Set("referrer", c.GetHeader("referrer")) util.Go(func() { db.Redis().AddUserExpire(uid, token) }) c.Next() } } func SetToken(c *gin.Context) { } // PassURL 过滤url func PassURL(path string) bool { index := strings.Index(path, "?") if index > 0 { path = path[:index] } _, ok := passURLs[path] if !ok { // index := strings.LastIndex(path, "/") // url := path[:index+1] + "*" // _, ok = passURLs[url] for k := range passURLs { if strings.Contains(path, k) { return true } } } return ok } // WhiteMiddleWare 白名单验证 func WhiteMiddleWare() gin.HandlerFunc { return func(c *gin.Context) { a := app.NewApp(c) ver := c.GetHeader("version") version, _ := strconv.Atoi(ver) ip := a.GetRemoteIP() if !call.WhitePass(ip, a.UUID, c.Request.RequestURI, version, a.Channel) { a.Code = values.CodeServer a.Response() c.Abort() return } c.Next() } }