1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-04 12:52:28 +08:00

refactoring: defect #247

This commit is contained in:
dudaodong
2024-09-20 19:29:31 +08:00
parent fcdf1d5839
commit f99a8ef3cf

View File

@@ -21,21 +21,48 @@ import (
// GetInternalIp return internal ipv4.
// Play: https://go.dev/play/p/5mbu-gFp7ei
func GetInternalIp() string {
addr, err := net.InterfaceAddrs()
addrs, err := net.InterfaceAddrs()
if err != nil {
panic(err.Error())
}
for _, a := range addr {
if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
return ipNet.IP.String()
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip != nil && (ip.IsLinkLocalUnicast() || ip.IsGlobalUnicast()) {
continue
}
if ipv4 := ip.To4(); ipv4 != nil {
return ipv4.String()
}
}
return ""
}
// func GetInternalIp() string {
// addr, err := net.InterfaceAddrs()
// if err != nil {
// panic(err.Error())
// }
// for _, a := range addr {
// if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
// if ipNet.IP.To4() != nil {
// return ipNet.IP.String()
// }
// }
// }
// return ""
// }
// GetRequestPublicIp return the requested public ip.
// Play: https://go.dev/play/p/kxU-YDc_eBo
func GetRequestPublicIp(req *http.Request) string {