netutils2: add MyIP() for finding default source ip

This commit is contained in:
Yousong Zhou
2019-03-15 12:09:28 +00:00
parent df9d1cd92c
commit 15e4708f71
+16
View File
@@ -40,6 +40,22 @@ func IsTcpPortUsed(addr string, port int) bool {
}
}
// MyIP returns source ip used to communicate with udp:114.114.114.114:53
func MyIP() (ip string, err error) {
conn, err := net.Dial("udp4", "114.114.114.114:53")
if err != nil {
return
}
defer conn.Close()
addr, ok := conn.LocalAddr().(*net.UDPAddr)
if !ok {
err = fmt.Errorf("not a net.UDPAddr: %#v", conn.LocalAddr())
return
}
ip = addr.IP.String()
return
}
func GetPrivatePrefixes(privatePrefixes []string) []string {
if privatePrefixes != nil {
return privatePrefixes