package upnp import ( // "log" "errors" "net" "strings" ) //获取本机能联网的ip地址 func GetLocalIntenetIp() string { /* 获得所有本机地址 判断能联网的ip地址 */ conn, err := net.Dial("udp4", "google.com:80") if err != nil { panic(errors.New("Can not connect to network")) } defer conn.Close() return strings.Split(conn.LocalAddr().String(), ":")[0] } // 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 }