45 lines
885 B
Go
45 lines
885 B
Go
package ip
|
|
|
|
import (
|
|
"net"
|
|
"strings"
|
|
)
|
|
|
|
func External() (res []string) {
|
|
var (
|
|
err error
|
|
addrs []net.Addr
|
|
inters []net.Interface
|
|
)
|
|
if inters, err = net.Interfaces(); err != nil {
|
|
return
|
|
}
|
|
for _, inter := range inters {
|
|
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 {
|
|
if ipNet.IP.IsLoopback() || ipNet.IP.IsLinkLocalMulticast() || ipNet.IP.IsLinkLocalUnicast() {
|
|
continue
|
|
}
|
|
if ip4 := ipNet.IP.To4(); ip4 != nil {
|
|
switch true {
|
|
case ip4[0] == 10:
|
|
continue
|
|
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
|
|
continue
|
|
case ip4[0] == 192 && ip4[1] == 168:
|
|
continue
|
|
default:
|
|
res = append(res, ipNet.IP.String())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|