Enhance Network Interface Detection and Configuration Management

- Implemented robust network interface detection with `GetInterfaces()` function
- Added validation for network interface names using regex patterns
- Updated `Network` struct to improve YAML tag formatting
- Modified `Gather()` and `SaveConfig()` methods to streamline network configuration
- Removed redundant `SaveConfig()` calls in various methods
- Added comprehensive network interface name validation logic
This commit is contained in:
zeaslity
2025-02-28 11:19:29 +08:00
parent c10554c218
commit b28c6462f1
5 changed files with 106 additions and 7 deletions

View File

@@ -3,8 +3,11 @@ package config
import (
"agent-wdd/log"
"encoding/json"
"fmt"
"io"
"net"
"net/http"
"regexp"
"time"
)
@@ -37,6 +40,61 @@ func (network *Network) Gather() {
network.Public = pub
//获取本机网卡相关的内容
network.Interfaces = GetInterfaces()
}
// GetInterfaces 获取本机网卡相关的内容
func GetInterfaces() []Interface {
interfaces := []Interface{}
// 获取所有网卡信息
netInterfaces, err := net.Interfaces()
if err != nil {
log.Error("获取网卡信息失败: %v", err)
return interfaces
}
for _, netInterface := range netInterfaces {
// 过滤掉没有地址的网卡
addrs, err := netInterface.Addrs()
if err != nil || len(addrs) == 0 {
continue
}
// 检查网卡名称是否有效
if !isValidNICName(netInterface.Name) {
continue
}
// 创建 Interface 对象
iface := Interface{
Name: netInterface.Name,
MAC: netInterface.HardwareAddr.String(),
}
// 获取 IPv4 和 IPv6 地址
for _, addr := range addrs {
ipNet, ok := addr.(*net.IPNet)
if !ok {
continue
}
if ipNet.IP.To4() != nil {
iface.IPv4 = ipNet.IP.String()
} else if ipNet.IP.To16() != nil {
iface.IPv6 = ipNet.IP.String()
}
}
interfaces = append(interfaces, iface)
}
return interfaces
}
func (network *Network) SaveConfig() {
SaveConfig()
}
// CanConnectInternet 判定主机能够上网 外网为9 国内为7 不能上网为1
@@ -51,7 +109,6 @@ func CanConnectInternet() int {
// 持久化保存
ConfigCache.Agent.Network.Internet = internetCode
SaveConfig()
}
return internetCode
@@ -92,7 +149,6 @@ func (p PublicInfo) GetPublicInfo() {
ConfigCache.Agent.Network.Public = fakePublicInfo
SaveConfig()
return
}
@@ -147,6 +203,45 @@ func (p PublicInfo) GetPublicInfo() {
ConfigCache.Agent.Network.Public = p
SaveConfig()
}
func isValidNICName(name string) bool {
// 定义传统命名规则正则表达式
// eth0, eth1, wlan0, wlan1 等
traditionalPattern := `^(eth|wlan)[0-9]+$`
// 定义现代命名规则正则表达式
// 支持 eno1, ens3, enp0s3, enx<MAC>, wlp2s0 等格式
modernPattern := `^(en|wl)(o[0-9]+|s[0-9]+|p[0-9]+s[0-9]+|x[0-9a-fA-F]{12})$`
// 编译正则表达式
traditionalRegex := regexp.MustCompile(traditionalPattern)
modernRegex := regexp.MustCompile(modernPattern)
// 检查是否匹配传统命名或现代命名规则
return traditionalRegex.MatchString(name) || modernRegex.MatchString(name)
}
func main() {
// 测试用例
testCases := []string{
"eth0", // 传统以太网命名
"wlan1", // 传统无线网卡命名
"eno1", // 板载网卡
"ens3", // 插槽网卡
"enp0s3", // PCI网卡
"enx0a1b2c3d4e5f", // 基于MAC地址的命名
"wlp2s0", // 无线PCI网卡
"eth", // 无效:缺少数字
"enp0", // 无效:不符合现代规则
"abc123", // 无效:完全不匹配
}
for _, tc := range testCases {
if isValidNICName(tc) {
fmt.Printf("%s: 有效网卡名称\n", tc)
} else {
fmt.Printf("%s: 无效网卡名称\n", tc)
}
}
}