Skip to content

Commit 4b44baf

Browse files
committed
enhance (network): interface detection logic
1 parent f0ba80a commit 4b44baf

File tree

2 files changed

+46
-26
lines changed

2 files changed

+46
-26
lines changed

internal/analytic/network.go

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ func GetNetworkStat() (data *net.IOCountersStat, err error) {
5252
continue
5353
}
5454

55-
// Handle container main interfaces like eth0 in container environments
56-
if isContainerInterface(iface.Name) {
55+
// Check if this is a physical network interface
56+
if isPhysicalInterface(iface.Name) && len(iface.HardwareAddr) > 0 {
5757
externalInterfaces[iface.Name] = true
5858
continue
5959
}
@@ -139,29 +139,58 @@ func isVirtualInterface(name string) bool {
139139
return false
140140
}
141141

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
142+
// isPhysicalInterface checks if the interface is a physical network interface
143+
// including server, cloud VM, and container physical interfaces
144+
func isPhysicalInterface(name string) bool {
145+
// Common prefixes for physical network interfaces across different platforms
146+
physicalPrefixes := []string{
147+
"eth", // Common Linux Ethernet interface
148+
"en", // macOS and some Linux
149+
"ens", // Predictable network interface names in systemd
150+
"enp", // Predictable network interface names in systemd (PCI)
151+
"eno", // Predictable network interface names in systemd (on-board)
152+
"wlan", // Wireless interfaces
153+
"wifi", // Some wireless interfaces
154+
"wl", // Shortened wireless interfaces
155+
"bond", // Bonded interfaces
156+
"em", // Some server network interfaces
157+
"p", // Some specialized network cards
158+
"lan", // Some network interfaces
154159
}
155160

156-
for _, pattern := range containerPatterns {
157-
if strings.HasPrefix(strings.ToLower(name), pattern) {
158-
return true
161+
// Check for exact matches for common primary interfaces
162+
if name == "eth0" || name == "en0" || name == "em0" {
163+
return true
164+
}
165+
166+
// Check for common physical interface patterns
167+
for _, prefix := range physicalPrefixes {
168+
if strings.HasPrefix(strings.ToLower(name), prefix) {
169+
// Check if the remaining part is numeric or empty
170+
suffix := strings.TrimPrefix(strings.ToLower(name), prefix)
171+
if suffix == "" || isNumericSuffix(suffix) {
172+
return true
173+
}
159174
}
160175
}
161176

162177
return false
163178
}
164179

180+
// isNumericSuffix checks if a string is a numeric suffix or starts with a number
181+
func isNumericSuffix(s string) bool {
182+
if len(s) == 0 {
183+
return false
184+
}
185+
186+
// Check if the first character is a digit
187+
if s[0] >= '0' && s[0] <= '9' {
188+
return true
189+
}
190+
191+
return false
192+
}
193+
165194
// isRealExternalIP checks if an IP is a genuine external (public) IP
166195
func isRealExternalIP(ip stdnet.IP, ipNet *stdnet.IPNet) bool {
167196
// Skip if it's not a global unicast address

internal/cert/payload.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,15 +62,6 @@ func (c *ConfigPayload) mkCertificateDir() (err error) {
6262
return nil
6363
}
6464

65-
if _, err = os.Stat(c.CertificateDir); os.IsNotExist(err) {
66-
err = os.MkdirAll(c.CertificateDir, 0755)
67-
if err == nil {
68-
return nil
69-
}
70-
} else {
71-
return nil
72-
}
73-
7465
// For windows, replace * with # (issue #403)
7566
c.CertificateDir = strings.ReplaceAll(c.CertificateDir, "*", "#")
7667
if _, err = os.Stat(c.CertificateDir); os.IsNotExist(err) {

0 commit comments

Comments
 (0)