-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathnet.go
43 lines (37 loc) · 762 Bytes
/
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
package rocketmq
import (
"net"
"strings"
)
func GetLocalIp4() (ip string) {
interfaces, err := net.Interfaces()
if err != nil {
return
}
for _, face := range interfaces {
if strings.Contains(face.Name, "lo") {
continue
}
addrs, err := face.Addrs()
if err != nil {
return
}
for _, addr := range addrs {
if ipnet, ok := addr.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
currIp := ipnet.IP.String()
if !strings.Contains(currIp, ":") && currIp != "127.0.0.1" && !isIntranetIpv4(currIp) {
ip = currIp
}
}
}
}
}
return
}
func isIntranetIpv4(ip string) bool {
if strings.HasPrefix(ip, "192.168.") || strings.HasPrefix(ip, "169.254.") {
return true
}
return false
}