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:
@@ -2,6 +2,7 @@ package cmd
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"agent-wdd/config"
|
"agent-wdd/config"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -26,6 +27,7 @@ func addInfoSubcommands(cmd *cobra.Command) {
|
|||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
network := config.ConfigCache.Agent.Network
|
network := config.ConfigCache.Agent.Network
|
||||||
network.Gather()
|
network.Gather()
|
||||||
|
network.SaveConfig()
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ func addZshSubcommands(cmd *cobra.Command) {
|
|||||||
if config.ConfigCache.Agent.Network.Public.IPv4 == "" {
|
if config.ConfigCache.Agent.Network.Public.IPv4 == "" {
|
||||||
network := config.ConfigCache.Agent.Network
|
network := config.ConfigCache.Agent.Network
|
||||||
network.Gather()
|
network.Gather()
|
||||||
|
network.SaveConfig()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 下载并安装zsh和git
|
// 下载并安装zsh和git
|
||||||
|
|||||||
@@ -57,12 +57,12 @@ type OS struct {
|
|||||||
OsType string `yaml:"os_type"`
|
OsType string `yaml:"os_type"`
|
||||||
Kernel string `yaml:"kernel"`
|
Kernel string `yaml:"kernel"`
|
||||||
Arch string `yaml:"arch"`
|
Arch string `yaml:"arch"`
|
||||||
IsUbuntuType bool `yaml:"is_ubuntu_type",comment:"是否是ubuntu类型的操作系统"`
|
IsUbuntuType bool `yaml:"is_ubuntu_type" comment:"是否是ubuntu类型的操作系统"`
|
||||||
PackInit bool `yaml:"pack_init",comment:"是否初始化,ubuntu需要"`
|
PackInit bool `yaml:"pack_init" comment:"是否初始化,ubuntu需要"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Network struct {
|
type Network struct {
|
||||||
Internet int `yaml:"internet",comment:"是否能够上网 外网为9 国内为7 不能上网为1"`
|
Internet int `yaml:"internet" comment:"是否能够上网 外网为9 国内为7 不能上网为1"`
|
||||||
Public PublicInfo `yaml:"public"`
|
Public PublicInfo `yaml:"public"`
|
||||||
Interfaces []Interface `yaml:"interfaces"`
|
Interfaces []Interface `yaml:"interfaces"`
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,8 +3,11 @@ package config
|
|||||||
import (
|
import (
|
||||||
"agent-wdd/log"
|
"agent-wdd/log"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -37,6 +40,61 @@ func (network *Network) Gather() {
|
|||||||
network.Public = pub
|
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
|
// CanConnectInternet 判定主机能够上网 外网为9 国内为7 不能上网为1
|
||||||
@@ -51,7 +109,6 @@ func CanConnectInternet() int {
|
|||||||
|
|
||||||
// 持久化保存
|
// 持久化保存
|
||||||
ConfigCache.Agent.Network.Internet = internetCode
|
ConfigCache.Agent.Network.Internet = internetCode
|
||||||
SaveConfig()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return internetCode
|
return internetCode
|
||||||
@@ -92,7 +149,6 @@ func (p PublicInfo) GetPublicInfo() {
|
|||||||
|
|
||||||
ConfigCache.Agent.Network.Public = fakePublicInfo
|
ConfigCache.Agent.Network.Public = fakePublicInfo
|
||||||
|
|
||||||
SaveConfig()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -147,6 +203,45 @@ func (p PublicInfo) GetPublicInfo() {
|
|||||||
|
|
||||||
ConfigCache.Agent.Network.Public = p
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
1
agent-wdd/utils/HttpUtils.go
Normal file
1
agent-wdd/utils/HttpUtils.go
Normal file
@@ -0,0 +1 @@
|
|||||||
|
package utils
|
||||||
Reference in New Issue
Block a user