package middleware import ( "server/call" "server/modules/web/app" "server/modules/web/values" "strconv" "strings" "github.com/gin-gonic/gin" "github.com/liangdas/mqant/log" ) // 白名单验证 func WhiteIPs() gin.HandlerFunc { return func(c *gin.Context) { a := app.NewApp(c) ip := a.GetRemoteIP() uri := c.Request.RequestURI tmp := strings.ReplaceAll(uri, "/provider", "") tmpSub := strings.Split(tmp, "/") if len(tmpSub) < 2 { c.Next() return } path := tmpSub[1] provider := call.GetConfigGameProviderByPath(path) if provider == nil { c.Next() return } if len(provider.SubIp) == 0 { c.Next() return } for _, v := range provider.SubIp { ips := strings.Split(v, "-") // 说明是固定ip if len(ips) == 1 { if ip == v { c.Next() return } continue } // 配置的是ip段的情况 ip0 := strings.Split(ips[0], ".") ip1 := strings.Split(ips[1], ".") thisIP := strings.Split(ip, ".") same := true for i := 0; i < len(thisIP)-1; i++ { if thisIP[i] != ip0[i] { same = false break } } if !same { continue } last0, _ := strconv.Atoi(ip0[len(ip0)-1]) last1, _ := strconv.Atoi(ip1[len(ip1)-1]) thisLast, _ := strconv.Atoi(thisIP[len(thisIP)-1]) if thisLast >= last0 && thisLast <= last1 { c.Next() return } } log.Debug("ip:%v,provider:%v,path:%v", ip, provider, uri) a.Code = values.CodeServer a.Response() c.Abort() } }