2021-03-09 17:59:21 +08:00
|
|
|
// Package util...
|
|
|
|
//
|
|
|
|
// Description : util...
|
|
|
|
//
|
2021-07-25 19:05:59 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-09 17:59:21 +08:00
|
|
|
//
|
|
|
|
// Date : 2021-03-09 5:56 下午
|
|
|
|
package util
|
|
|
|
|
2021-11-22 17:35:57 +08:00
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
)
|
2021-03-09 17:59:21 +08:00
|
|
|
|
|
|
|
// GetHostIP 获取本机IP地址
|
|
|
|
//
|
2021-07-25 19:05:59 +08:00
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
2021-03-09 17:59:21 +08:00
|
|
|
//
|
|
|
|
// Date : 5:58 下午 2021/3/9
|
|
|
|
func 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
|
|
|
|
}
|
2021-11-22 17:35:57 +08:00
|
|
|
|
|
|
|
// GetRemoteIp 获取远端IP
|
|
|
|
//
|
|
|
|
// Author : go_developer@163.com<白茶清欢>
|
|
|
|
//
|
|
|
|
// Date : 5:35 下午 2021/11/22
|
|
|
|
func GetRemoteIp(req *http.Request) string {
|
|
|
|
|
|
|
|
// Try via request
|
|
|
|
|
|
|
|
ip, _, err := net.SplitHostPort(req.RemoteAddr)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "::1"
|
|
|
|
}
|
|
|
|
userIP := net.ParseIP(ip)
|
|
|
|
if userIP == nil {
|
|
|
|
return "::1"
|
|
|
|
}
|
|
|
|
return userIP.String()
|
|
|
|
}
|