优化ip文件
This commit is contained in:
18
ip.go
18
ip.go
@ -12,12 +12,20 @@ import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
// ip ...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 14:47 2022/5/14
|
||||
type ip struct {
|
||||
}
|
||||
|
||||
// GetHostIP 获取本机IP地址
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:58 下午 2021/3/9
|
||||
func GetHostIP() string {
|
||||
func (i *ip) GetHostIP() string {
|
||||
hostIP := "127.0.0.1"
|
||||
addrs, _ := net.InterfaceAddrs()
|
||||
for _, address := range addrs {
|
||||
@ -36,16 +44,14 @@ func GetHostIP() string {
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:35 下午 2021/11/22
|
||||
func GetRemoteIp(req *http.Request) string {
|
||||
|
||||
func (i *ip) GetRemoteIp(req *http.Request) string {
|
||||
// Try via request
|
||||
|
||||
ip, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
remoteIP, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
|
||||
if err != nil {
|
||||
return "::1"
|
||||
}
|
||||
userIP := net.ParseIP(ip)
|
||||
userIP := net.ParseIP(remoteIP)
|
||||
if userIP == nil {
|
||||
return "::1"
|
||||
}
|
||||
|
Reference in New Issue
Block a user