-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnet.go
83 lines (70 loc) · 1.76 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
-------------------------------------------------
Author : Zhang Fan
date: 2020/12/11
Description :
-------------------------------------------------
*/
package zutils
import (
"net"
)
var Net = new(netUtil)
type netUtil struct{}
// 获取所有能检测到的本地ip, 不会返回 127.*
func (u *netUtil) GetLocalIPs() []string {
ips := u.GetLocalNetIPs()
ret := make([]string, len(ips))
for i := range ips {
ret = append(ret, ips[i].String())
}
return ret
}
// 获取所有能检测到的本地ip, 返回第一个匹配 prefix 的ip, 否则返回空字符串
func (u *netUtil) GetLocalIPMatchPrefix(prefix string) string {
ips := u.GetLocalIPs()
le := len(prefix)
if le == 0 {
return ips[0]
}
for _, ip := range ips {
if len(ip) >= le && ip[:le] == prefix {
return ip
}
}
return ""
}
func (u *netUtil) GetLocalNetIPs() []net.IP {
var ips []net.IP
// 获取所有网卡接口信息
ifaces, err := net.Interfaces()
if err != nil {
return nil
}
for _, iface := range ifaces {
// 排除本地回环接口和隧道接口
if iface.Flags&net.FlagLoopback != 0 || iface.Flags&net.FlagPointToPoint != 0 {
continue
}
// 获取当前网卡接口的所有 IP 地址信息
addrs, err := iface.Addrs()
if err != nil {
continue
}
// 遍历当前网卡接口的所有 IP 地址
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if !ok {
continue
}
// 排除本地 IP 地址和无效 IP 地址
if ipNet.IP.IsLoopback() || ipNet.IP.IsLinkLocalUnicast() || ipNet.IP.IsLinkLocalMulticast() ||
ipNet.IP.IsInterfaceLocalMulticast() || ipNet.IP.IsMulticast() || ipNet.IP.IsUnspecified() {
continue
}
// 输出符合条件的 IP 地址
ips = append(ips, ipNet.IP)
}
}
return ips
}