37 lines
616 B
Go
37 lines
616 B
Go
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 ""
|
|
}
|