修复了一些问题
This commit is contained in:
@@ -145,7 +145,7 @@ func (m *RequestLogMiddleware) Handler() gin.HandlerFunc {
|
||||
}
|
||||
|
||||
// 获取请求IP
|
||||
ip := getClientIP(c)
|
||||
ip := utils.GetClientIP(c)
|
||||
|
||||
// 本地IP不记录
|
||||
if utils.GetIPLocation(ip) == "本地" {
|
||||
@@ -253,28 +253,6 @@ func (m *RequestLogMiddleware) logRequest(c *gin.Context, ip, userID string, req
|
||||
m.DB.Create(&log)
|
||||
}
|
||||
|
||||
// getClientIP 获取客户端IP
|
||||
func getClientIP(c *gin.Context) string {
|
||||
// 优先从X-Forwarded-For获取
|
||||
ip := c.GetHeader("X-Forwarded-For")
|
||||
if ip != "" {
|
||||
// X-Forwarded-For可能包含多个IP,取第一个
|
||||
ips := strings.Split(ip, ",")
|
||||
if len(ips) > 0 {
|
||||
return strings.TrimSpace(ips[0])
|
||||
}
|
||||
}
|
||||
|
||||
// 从X-Real-IP获取
|
||||
ip = c.GetHeader("X-Real-IP")
|
||||
if ip != "" {
|
||||
return ip
|
||||
}
|
||||
|
||||
// 从RemoteAddr获取
|
||||
return c.ClientIP()
|
||||
}
|
||||
|
||||
// responseWriter 响应写入器(用于捕获响应内容)
|
||||
type responseWriter struct {
|
||||
gin.ResponseWriter
|
||||
|
||||
Reference in New Issue
Block a user