2014-06-26 19:57:22 +08:00
|
|
|
package upnp
|
|
|
|
|
|
|
|
import (
|
2014-06-27 20:40:41 +08:00
|
|
|
// "log"
|
|
|
|
"errors"
|
2014-06-26 19:57:22 +08:00
|
|
|
"net"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
//获取本机能联网的ip地址
|
|
|
|
func GetLocalIntenetIp() string {
|
|
|
|
/*
|
|
|
|
获得所有本机地址
|
|
|
|
判断能联网的ip地址
|
|
|
|
*/
|
|
|
|
|
|
|
|
conn, err := net.Dial("udp", "google.com:80")
|
|
|
|
if err != nil {
|
2016-12-21 12:39:02 +08:00
|
|
|
panic(errors.New("Can not connect to network"))
|
2014-06-26 19:57:22 +08:00
|
|
|
}
|
|
|
|
defer conn.Close()
|
2014-06-27 20:40:41 +08:00
|
|
|
return strings.Split(conn.LocalAddr().String(), ":")[0]
|
2014-06-26 19:57:22 +08:00
|
|
|
}
|
2014-07-04 15:24:14 +08:00
|
|
|
|
|
|
|
// This returns the list of local ip addresses which other hosts can connect
|
|
|
|
// to (NOTE: Loopback ip is ignored).
|
|
|
|
func GetLocalIPs() ([]*net.IP, error) {
|
|
|
|
addrs, err := net.InterfaceAddrs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ips := make([]*net.IP, 0)
|
|
|
|
for _, addr := range addrs {
|
|
|
|
ipnet, ok := addr.(*net.IPNet)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if ipnet.IP.IsLoopback() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
ips = append(ips, &ipnet.IP)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips, nil
|
|
|
|
}
|