@@ -2,6 +2,7 @@ package analytic
2
2
3
3
import (
4
4
stdnet "net"
5
+ "strings"
5
6
6
7
"github.com/shirou/gopsutil/v4/net"
7
8
"github.com/uozi-tech/cosy/logger"
@@ -46,6 +47,17 @@ func GetNetworkStat() (data *net.IOCountersStat, err error) {
46
47
continue
47
48
}
48
49
50
+ // Skip common virtual interfaces by name pattern
51
+ if isVirtualInterface (iface .Name ) {
52
+ continue
53
+ }
54
+
55
+ // Handle container main interfaces like eth0 in container environments
56
+ if isContainerInterface (iface .Name ) {
57
+ externalInterfaces [iface .Name ] = true
58
+ continue
59
+ }
60
+
49
61
// Get addresses for this interface
50
62
addrs , err := iface .Addrs ()
51
63
if err != nil {
@@ -106,6 +118,50 @@ func GetNetworkStat() (data *net.IOCountersStat, err error) {
106
118
}, nil
107
119
}
108
120
121
+ // isVirtualInterface checks if the interface is a virtual one based on name patterns
122
+ func isVirtualInterface (name string ) bool {
123
+ // Common virtual interface name patterns
124
+ virtualPatterns := []string {
125
+ "veth" , "virbr" , "vnet" , "vmnet" , "vboxnet" , "docker" ,
126
+ "br-" , "bridge" , "tun" , "tap" , "bond" , "dummy" ,
127
+ "vpn" , "ipsec" , "gre" , "sit" , "vlan" , "virt" ,
128
+ "wg" , "vmk" , "ib" , "vxlan" , "geneve" , "ovs" ,
129
+ "hyperv" , "hyper-v" , "awdl" , "llw" , "utun" ,
130
+ "vpn" , "zt" , "zerotier" , "wireguard" ,
131
+ }
132
+
133
+ for _ , pattern := range virtualPatterns {
134
+ if strings .Contains (strings .ToLower (name ), pattern ) {
135
+ return true
136
+ }
137
+ }
138
+
139
+ return false
140
+ }
141
+
142
+ // isContainerInterface checks if this is a main container interface
143
+ func isContainerInterface (name string ) bool {
144
+ // Common main container interface patterns
145
+ // eth0 is usually the main interface inside containers
146
+ // en0, en1 are common physical interfaces on macOS
147
+ // ens/enp/eno are common physical interfaces on Linux
148
+ containerPatterns := []string {
149
+ "eth0" , "en0" , "en1" ,
150
+ "ens" , "enp" , "eno" ,
151
+ "eth1" , "eth2" , // Potential physical interfaces
152
+ "wlan" , "wifi" , "wl" , // Wireless interfaces
153
+ "bond0" , // Bonded interfaces that might be external
154
+ }
155
+
156
+ for _ , pattern := range containerPatterns {
157
+ if strings .HasPrefix (strings .ToLower (name ), pattern ) {
158
+ return true
159
+ }
160
+ }
161
+
162
+ return false
163
+ }
164
+
109
165
// isRealExternalIP checks if an IP is a genuine external (public) IP
110
166
func isRealExternalIP (ip stdnet.IP , ipNet * stdnet.IPNet ) bool {
111
167
// Skip if it's not a global unicast address
0 commit comments