mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
enhance (network): interface detection logic
This commit is contained in:
parent
f0ba80a2a7
commit
4b44baf92c
2 changed files with 46 additions and 26 deletions
|
@ -52,8 +52,8 @@ func GetNetworkStat() (data *net.IOCountersStat, err error) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle container main interfaces like eth0 in container environments
|
// Check if this is a physical network interface
|
||||||
if isContainerInterface(iface.Name) {
|
if isPhysicalInterface(iface.Name) && len(iface.HardwareAddr) > 0 {
|
||||||
externalInterfaces[iface.Name] = true
|
externalInterfaces[iface.Name] = true
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -139,29 +139,58 @@ func isVirtualInterface(name string) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
// isContainerInterface checks if this is a main container interface
|
// isPhysicalInterface checks if the interface is a physical network interface
|
||||||
func isContainerInterface(name string) bool {
|
// including server, cloud VM, and container physical interfaces
|
||||||
// Common main container interface patterns
|
func isPhysicalInterface(name string) bool {
|
||||||
// eth0 is usually the main interface inside containers
|
// Common prefixes for physical network interfaces across different platforms
|
||||||
// en0, en1 are common physical interfaces on macOS
|
physicalPrefixes := []string{
|
||||||
// ens/enp/eno are common physical interfaces on Linux
|
"eth", // Common Linux Ethernet interface
|
||||||
containerPatterns := []string{
|
"en", // macOS and some Linux
|
||||||
"eth0", "en0", "en1",
|
"ens", // Predictable network interface names in systemd
|
||||||
"ens", "enp", "eno",
|
"enp", // Predictable network interface names in systemd (PCI)
|
||||||
"eth1", "eth2", // Potential physical interfaces
|
"eno", // Predictable network interface names in systemd (on-board)
|
||||||
"wlan", "wifi", "wl", // Wireless interfaces
|
"wlan", // Wireless interfaces
|
||||||
"bond0", // Bonded interfaces that might be external
|
"wifi", // Some wireless interfaces
|
||||||
|
"wl", // Shortened wireless interfaces
|
||||||
|
"bond", // Bonded interfaces
|
||||||
|
"em", // Some server network interfaces
|
||||||
|
"p", // Some specialized network cards
|
||||||
|
"lan", // Some network interfaces
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, pattern := range containerPatterns {
|
// Check for exact matches for common primary interfaces
|
||||||
if strings.HasPrefix(strings.ToLower(name), pattern) {
|
if name == "eth0" || name == "en0" || name == "em0" {
|
||||||
return true
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for common physical interface patterns
|
||||||
|
for _, prefix := range physicalPrefixes {
|
||||||
|
if strings.HasPrefix(strings.ToLower(name), prefix) {
|
||||||
|
// Check if the remaining part is numeric or empty
|
||||||
|
suffix := strings.TrimPrefix(strings.ToLower(name), prefix)
|
||||||
|
if suffix == "" || isNumericSuffix(suffix) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isNumericSuffix checks if a string is a numeric suffix or starts with a number
|
||||||
|
func isNumericSuffix(s string) bool {
|
||||||
|
if len(s) == 0 {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the first character is a digit
|
||||||
|
if s[0] >= '0' && s[0] <= '9' {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// isRealExternalIP checks if an IP is a genuine external (public) IP
|
// isRealExternalIP checks if an IP is a genuine external (public) IP
|
||||||
func isRealExternalIP(ip stdnet.IP, ipNet *stdnet.IPNet) bool {
|
func isRealExternalIP(ip stdnet.IP, ipNet *stdnet.IPNet) bool {
|
||||||
// Skip if it's not a global unicast address
|
// Skip if it's not a global unicast address
|
||||||
|
|
|
@ -62,15 +62,6 @@ func (c *ConfigPayload) mkCertificateDir() (err error) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err = os.Stat(c.CertificateDir); os.IsNotExist(err) {
|
|
||||||
err = os.MkdirAll(c.CertificateDir, 0755)
|
|
||||||
if err == nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// For windows, replace * with # (issue #403)
|
// For windows, replace * with # (issue #403)
|
||||||
c.CertificateDir = strings.ReplaceAll(c.CertificateDir, "*", "#")
|
c.CertificateDir = strings.ReplaceAll(c.CertificateDir, "*", "#")
|
||||||
if _, err = os.Stat(c.CertificateDir); os.IsNotExist(err) {
|
if _, err = os.Stat(c.CertificateDir); os.IsNotExist(err) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue