From b28c6462f19d4b0f1a4855ae2a7af5979d55f835 Mon Sep 17 00:00:00 2001 From: zeaslity Date: Fri, 28 Feb 2025 11:19:29 +0800 Subject: [PATCH] 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 --- agent-wdd/cmd/Info.go | 2 + agent-wdd/cmd/Zsh.go | 1 + agent-wdd/config/Config.go | 6 +- agent-wdd/config/Network.go | 103 +++++++++++++++++++++++++++++++++-- agent-wdd/utils/HttpUtils.go | 1 + 5 files changed, 106 insertions(+), 7 deletions(-) create mode 100644 agent-wdd/utils/HttpUtils.go diff --git a/agent-wdd/cmd/Info.go b/agent-wdd/cmd/Info.go index 222ca6e..fde87c3 100644 --- a/agent-wdd/cmd/Info.go +++ b/agent-wdd/cmd/Info.go @@ -2,6 +2,7 @@ package cmd import ( "agent-wdd/config" + "github.com/spf13/cobra" ) @@ -26,6 +27,7 @@ func addInfoSubcommands(cmd *cobra.Command) { Run: func(cmd *cobra.Command, args []string) { network := config.ConfigCache.Agent.Network network.Gather() + network.SaveConfig() }, } diff --git a/agent-wdd/cmd/Zsh.go b/agent-wdd/cmd/Zsh.go index 949c655..0a7686f 100644 --- a/agent-wdd/cmd/Zsh.go +++ b/agent-wdd/cmd/Zsh.go @@ -25,6 +25,7 @@ func addZshSubcommands(cmd *cobra.Command) { if config.ConfigCache.Agent.Network.Public.IPv4 == "" { network := config.ConfigCache.Agent.Network network.Gather() + network.SaveConfig() } // 下载并安装zsh和git diff --git a/agent-wdd/config/Config.go b/agent-wdd/config/Config.go index 0b66646..858b852 100644 --- a/agent-wdd/config/Config.go +++ b/agent-wdd/config/Config.go @@ -57,12 +57,12 @@ type OS struct { OsType string `yaml:"os_type"` Kernel string `yaml:"kernel"` Arch string `yaml:"arch"` - IsUbuntuType bool `yaml:"is_ubuntu_type",comment:"是否是ubuntu类型的操作系统"` - PackInit bool `yaml:"pack_init",comment:"是否初始化,ubuntu需要"` + IsUbuntuType bool `yaml:"is_ubuntu_type" comment:"是否是ubuntu类型的操作系统"` + PackInit bool `yaml:"pack_init" comment:"是否初始化,ubuntu需要"` } type Network struct { - Internet int `yaml:"internet",comment:"是否能够上网 外网为9 国内为7 不能上网为1"` + Internet int `yaml:"internet" comment:"是否能够上网 外网为9 国内为7 不能上网为1"` Public PublicInfo `yaml:"public"` Interfaces []Interface `yaml:"interfaces"` } diff --git a/agent-wdd/config/Network.go b/agent-wdd/config/Network.go index 1ddd76f..a526893 100644 --- a/agent-wdd/config/Network.go +++ b/agent-wdd/config/Network.go @@ -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, 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) + } + } } diff --git a/agent-wdd/utils/HttpUtils.go b/agent-wdd/utils/HttpUtils.go new file mode 100644 index 0000000..d4b585b --- /dev/null +++ b/agent-wdd/utils/HttpUtils.go @@ -0,0 +1 @@ +package utils