迁移util相关
This commit is contained in:
59
util/ip.go
Normal file
59
util/ip.go
Normal file
@ -0,0 +1,59 @@
|
||||
// Package util...
|
||||
//
|
||||
// Description : util...
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 2021-03-09 5:56 下午
|
||||
package util
|
||||
|
||||
import (
|
||||
"net"
|
||||
"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 (i *ip) GetHostIP() string {
|
||||
hostIP := "127.0.0.1"
|
||||
addrs, _ := net.InterfaceAddrs()
|
||||
for _, address := range addrs {
|
||||
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() != nil {
|
||||
hostIP = ipnet.IP.String()
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return hostIP
|
||||
}
|
||||
|
||||
// GetRemoteIP 获取远端IP
|
||||
//
|
||||
// Author : go_developer@163.com<白茶清欢>
|
||||
//
|
||||
// Date : 5:35 下午 2021/11/22
|
||||
func (i *ip) GetRemoteIP(req *http.Request) string {
|
||||
// Try via request
|
||||
remoteIP, _, err := net.SplitHostPort(req.RemoteAddr)
|
||||
|
||||
if err != nil {
|
||||
return "::1"
|
||||
}
|
||||
userIP := net.ParseIP(remoteIP)
|
||||
if userIP == nil {
|
||||
return "::1"
|
||||
}
|
||||
return userIP.String()
|
||||
}
|
Reference in New Issue
Block a user