kefu/middleware/ipblack.go

84 lines
1.5 KiB
Go

package middleware
import (
"github.com/gin-gonic/gin"
"kefu/models"
"kefu/types"
"log"
"strings"
)
func Ipblack(c *gin.Context) {
ip := c.ClientIP()
ipblack := models.FindIp(ip)
if ipblack.IP != "" {
c.JSON(200, gin.H{
"code": types.ApiCode.IP_BAN,
"msg": types.ApiCode.GetMessage(types.ApiCode.IP_BAN),
})
c.Abort()
return
}
}
//限制客服IP
func KefuIpblack(c *gin.Context) {
ip := c.ClientIP()
ipblack := models.FindIp(ip)
entId, ok := c.Get("ent_id")
if !ok {
entId = ""
}
if ipblack.IP != "" {
if ipblack.EntId == "" || entId.(string) == ipblack.EntId {
c.JSON(200, gin.H{
"code": types.ApiCode.IP_BAN,
"msg": types.ApiCode.GetMessage(types.ApiCode.IP_BAN),
})
c.Abort()
return
}
}
}
//限制IP或refer
func SystemReferIpblack(c *gin.Context) {
ip := c.ClientIP()
refer := c.Request.Referer()
blackList := models.FindConfig("SystemBlackList")
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 {
log.Println("ip forbidden", ip, word)
exist = true
break
}
}
if refer != "" {
for _, word := range list {
word = strings.Trim(word, " ")
if word == "" {
continue
}
if strings.Contains(refer, word) {
log.Println("refer forbidden", refer, word)
exist = true
break
}
}
}
if exist {
c.String(403, "403 forbidden")
c.Abort()
return
}
}