-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.go
66 lines (59 loc) · 1.35 KB
/
net.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package netutil
import (
"net"
"strings"
"time"
)
// GetExternalIP get external ip
func GetExternalIP() (string, error) {
data, err := GetHTTP("https://myexternalip.com/raw", 10*time.Second, nil)
if err != nil {
return "", err
}
return data, nil
}
// IsPublicIP is public ip
func IsPublicIP(IP net.IP) bool {
if IP.IsLoopback() || IP.IsLinkLocalMulticast() || IP.IsLinkLocalUnicast() {
return false
}
if ip4 := IP.To4(); ip4 != nil {
switch true {
case ip4[0] == 10:
return false
case ip4[0] == 172 && ip4[1] >= 16 && ip4[1] <= 31:
return false
case ip4[0] == 192 && ip4[1] == 168:
return false
default:
return true
}
}
return false
}
// GetLocalIPv4s get local ipv4
func GetLocalIPv4s() ([]string, error) {
address, err := net.InterfaceAddrs()
if err != nil {
return nil, err
}
var ips = make([]string, 0, len(address))
for _, addr := range address {
// 检查ip地址判断是否回环地址
if ipNet, ok := addr.(*net.IPNet); ok && !ipNet.IP.IsLoopback() && ipNet.IP.To4() != nil {
ips = append(ips, ipNet.IP.String())
}
}
return ips, nil
}
// GetLocalIP get local ip
func GetLocalIP() (string, error) {
conn, err := net.Dial("udp", "8.8.8.8:80")
if err != nil {
return "", err
}
defer conn.Close()
localAddr := conn.LocalAddr().String()
idx := strings.LastIndex(localAddr, ":")
return localAddr[0:idx], nil
}