refactor🎨: 更新客户端ip获取方法
This commit is contained in:
1 parent
880a3700d1
commit
f0e8f18c6d
2 files changed
+17
-5
No files matched your search
@@ -2,7 +2,7 @@ package global
|
|||||||
|
|
||||||
const (
|
const (
|
||||||
// Version go-admin version info
|
// Version go-admin version info
|
||||||
Version = "2.0.0-beta.3"
|
Version = "2.0.0-beta.4"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|||||||
+16
-4
@@ -1,17 +1,29 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetClientIP(ctx *gin.Context) string {
|
func GetClientIP(c *gin.Context) string {
|
||||||
ip := ctx.Request.Header.Get("X-Forwarded-For")
|
ClientIP := c.ClientIP()
|
||||||
|
fmt.Println("ClientIP:", ClientIP)
|
||||||
|
RemoteIP, _ := c.RemoteIP()
|
||||||
|
fmt.Println("RemoteIP:", RemoteIP)
|
||||||
|
ip := c.Request.Header.Get("X-Forwarded-For")
|
||||||
if strings.Contains(ip, "127.0.0.1") || ip == "" {
|
if strings.Contains(ip, "127.0.0.1") || ip == "" {
|
||||||
ip = ctx.Request.Header.Get("X-real-ip")
|
ip = c.Request.Header.Get("X-real-ip")
|
||||||
}
|
}
|
||||||
if ip == "" {
|
if ip == "" {
|
||||||
return "127.0.0.1"
|
ip = "127.0.0.1"
|
||||||
}
|
}
|
||||||
|
if RemoteIP.String() != "127.0.0.1" {
|
||||||
|
ip = RemoteIP.String()
|
||||||
|
}
|
||||||
|
if ClientIP != "127.0.0.1" {
|
||||||
|
ip = ClientIP
|
||||||
|
}
|
||||||
|
fmt.Println("ip:", ip)
|
||||||
return ip
|
return ip
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user