forked from prestonTao/upnp
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcommon.go
48 lines (40 loc) · 846 Bytes
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
}