You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
909 B
50 lines
909 B
package router
|
|
|
|
import (
|
|
"epur-pay/cache"
|
|
"epur-pay/pkg/logger"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
func ClientIp(c *gin.Context) string {
|
|
|
|
ip := c.Request.Header.Get("Cf-Connecting-Ip")
|
|
|
|
if len(ip) <= 0 {
|
|
ip = c.Request.Header.Get("X-Real-Ip")
|
|
}
|
|
if len(ip) <= 0 {
|
|
ip = c.ClientIP()
|
|
}
|
|
return ip
|
|
}
|
|
|
|
func IPBlackList() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
if cache.Global.Caches.BlackList.Check(ClientIp(c)) == true {
|
|
logger.AccessLogger.Warnf("黑名单请求IP [%s]", ClientIp(c))
|
|
c.AbortWithStatus(http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
func Logs() gin.HandlerFunc {
|
|
return func(c *gin.Context) {
|
|
|
|
start := time.Now()
|
|
|
|
c.Next()
|
|
|
|
logger.RouterLogger.Infoln(
|
|
time.Now().Sub(start),
|
|
c.Request.Header.Get("Cf-Connecting-Ip"),
|
|
c.Request.Header.Get("Cf-Ipcountry"),
|
|
c.Request.Method,
|
|
c.Writer.Status(),
|
|
c.Request.RequestURI)
|
|
}
|
|
}
|