1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-11 08:12:26 +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. // GetInternalIp return internal ipv4.
// Play: https://go.dev/play/p/5mbu-gFp7ei // Play: https://go.dev/play/p/5mbu-gFp7ei
func GetInternalIp() string { func GetInternalIp() string {
addr, err := net.InterfaceAddrs() addrs, err := net.InterfaceAddrs()
if err != nil { if err != nil {
panic(err.Error()) panic(err.Error())
} }
for _, a := range addr {
if ipNet, ok := a.(*net.IPNet); ok && !ipNet.IP.IsLoopback() { for _, addr := range addrs {
if ipNet.IP.To4() != nil { var ip net.IP
return ipNet.IP.String() 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 "" 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. // GetRequestPublicIp return the requested public ip.
// Play: https://go.dev/play/p/kxU-YDc_eBo // Play: https://go.dev/play/p/kxU-YDc_eBo
func GetRequestPublicIp(req *http.Request) string { func GetRequestPublicIp(req *http.Request) string {