|
@@ -2,6 +2,7 @@ package analytic
|
|
|
|
|
|
import (
|
|
|
stdnet "net"
|
|
|
+ "strings"
|
|
|
|
|
|
"github.com/shirou/gopsutil/v4/net"
|
|
|
"github.com/uozi-tech/cosy/logger"
|
|
@@ -46,6 +47,17 @@ func GetNetworkStat() (data *net.IOCountersStat, err error) {
|
|
|
continue
|
|
|
}
|
|
|
|
|
|
+ // Skip common virtual interfaces by name pattern
|
|
|
+ if isVirtualInterface(iface.Name) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ // Handle container main interfaces like eth0 in container environments
|
|
|
+ if isContainerInterface(iface.Name) {
|
|
|
+ externalInterfaces[iface.Name] = true
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
// Get addresses for this interface
|
|
|
addrs, err := iface.Addrs()
|
|
|
if err != nil {
|
|
@@ -106,6 +118,50 @@ func GetNetworkStat() (data *net.IOCountersStat, err error) {
|
|
|
}, nil
|
|
|
}
|
|
|
|
|
|
+// isVirtualInterface checks if the interface is a virtual one based on name patterns
|
|
|
+func isVirtualInterface(name string) bool {
|
|
|
+ // Common virtual interface name patterns
|
|
|
+ virtualPatterns := []string{
|
|
|
+ "veth", "virbr", "vnet", "vmnet", "vboxnet", "docker",
|
|
|
+ "br-", "bridge", "tun", "tap", "bond", "dummy",
|
|
|
+ "vpn", "ipsec", "gre", "sit", "vlan", "virt",
|
|
|
+ "wg", "vmk", "ib", "vxlan", "geneve", "ovs",
|
|
|
+ "hyperv", "hyper-v", "awdl", "llw", "utun",
|
|
|
+ "vpn", "zt", "zerotier", "wireguard",
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, pattern := range virtualPatterns {
|
|
|
+ if strings.Contains(strings.ToLower(name), pattern) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
+// isContainerInterface checks if this is a main container interface
|
|
|
+func isContainerInterface(name string) bool {
|
|
|
+ // Common main container interface patterns
|
|
|
+ // eth0 is usually the main interface inside containers
|
|
|
+ // en0, en1 are common physical interfaces on macOS
|
|
|
+ // ens/enp/eno are common physical interfaces on Linux
|
|
|
+ containerPatterns := []string{
|
|
|
+ "eth0", "en0", "en1",
|
|
|
+ "ens", "enp", "eno",
|
|
|
+ "eth1", "eth2", // Potential physical interfaces
|
|
|
+ "wlan", "wifi", "wl", // Wireless interfaces
|
|
|
+ "bond0", // Bonded interfaces that might be external
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, pattern := range containerPatterns {
|
|
|
+ if strings.HasPrefix(strings.ToLower(name), pattern) {
|
|
|
+ return true
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return false
|
|
|
+}
|
|
|
+
|
|
|
// isRealExternalIP checks if an IP is a genuine external (public) IP
|
|
|
func isRealExternalIP(ip stdnet.IP, ipNet *stdnet.IPNet) bool {
|
|
|
// Skip if it's not a global unicast address
|