优化ip文件

This commit is contained in:
白茶清欢 2022-05-14 14:58:22 +08:00
parent 8fae26e662
commit fe96264f73
2 changed files with 15 additions and 6 deletions

View File

@ -14,10 +14,13 @@ var (
File *file
// Hash ...
Hash *hash
// IP ...
IP *ip
)
func init() {
Cli = &cli{}
File = &file{}
Hash = &hash{}
IP = &ip{}
}

18
ip.go
View File

@ -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"
}