kefu/middleware/ipwhite.go

36 lines
580 B
Go
Raw Permalink Normal View History

2024-12-10 02:50:12 +00:00
package middleware
import (
"github.com/gin-gonic/gin"
"kefu/models"
"log"
"strings"
)
//IP白名单
func SystemIpWhite(c *gin.Context) {
ip := c.ClientIP()
blackList := models.FindConfig("SystemWhiteList")
strings.ReplaceAll(blackList, "\r\n", "\n")
list := strings.Split(blackList, "\n")
exist := false
for _, word := range list {
word = strings.Trim(word, " ")
if word == "" {
continue
}
if ip == word {
exist = true
break
}
}
if !exist {
log.Println("ip whitelist forbidden", ip)
c.String(403, "403 forbidden")
c.Abort()
return
}
}