kos/util/ip/internal.go

37 lines
616 B
Go
Raw Permalink Normal View History

2023-04-23 17:57:36 +08:00
package ip
import (
"net"
"strings"
)
// Internal get internal ip.
func Internal() string {
var (
err error
addrs []net.Addr
inters []net.Interface
)
if inters, err = net.Interfaces(); err != nil {
return ""
}
for _, inter := range inters {
if !isUp(inter.Flags) {
continue
}
if !strings.HasPrefix(inter.Name, "lo") {
if addrs, err = inter.Addrs(); err != nil {
continue
}
for _, addr := range addrs {
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() {
if ipNet.IP.To4() != nil {
return ipNet.IP.String()
}
}
}
}
}
return ""
}