Ver Fonte

feat(network): 增加管理接口链路本地地址校验警告

移除通用链路本地地址警告,改为针对管理接口未配置 169.254 地址时提示,防止客户端无法通过维护链路连接。
yangkaixiang há 1 mês atrás
pai
commit
67794beb94

+ 1 - 1
server/internal/config/config.go

@@ -6,7 +6,7 @@ import (
 	"net"
 )
 
-const ServerVersion = "20260511152102"
+const ServerVersion = "20260511154000"
 
 type Config struct {
 	HTTPHost         string

+ 39 - 0
server/internal/httpserver/server.go

@@ -5,6 +5,7 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
+	"net"
 	"net/http"
 	"os"
 	"strings"
@@ -217,6 +218,7 @@ func (s *Server) handleValidate(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 	result := s.validatorSvc.Validate(input)
+	s.addManagementAddressWarning(&result, input)
 	if !result.Valid {
 		writeJSON(w, http.StatusBadRequest, model.APIResponse{Code: 3001, Message: "配置校验失败", Data: result})
 		return
@@ -503,6 +505,7 @@ func (s *Server) validateConfigs(inputs []model.InterfaceConfig) model.ValidateR
 		return result
 	}
 	seen := make(map[string]struct{})
+	managementInterface := s.currentManagementInterface()
 	for _, input := range inputs {
 		name := strings.TrimSpace(input.Interface)
 		if name == "" {
@@ -522,6 +525,9 @@ func (s *Server) validateConfigs(inputs []model.InterfaceConfig) model.ValidateR
 			continue
 		}
 		item := s.validatorSvc.Validate(input)
+		if name == managementInterface {
+			addManagementAddressWarning(&item, input)
+		}
 		if !item.Valid {
 			result.Valid = false
 		}
@@ -535,6 +541,39 @@ func (s *Server) validateConfigs(inputs []model.InterfaceConfig) model.ValidateR
 	return result
 }
 
+func (s *Server) addManagementAddressWarning(result *model.ValidateResponse, input model.InterfaceConfig) {
+	managementInterface := s.currentManagementInterface()
+	if managementInterface == "" || strings.TrimSpace(input.Interface) != managementInterface {
+		return
+	}
+	addManagementAddressWarning(result, input)
+}
+
+func addManagementAddressWarning(result *model.ValidateResponse, input model.InterfaceConfig) {
+	if hasLinkLocalAddress(input) {
+		return
+	}
+	result.Warnings = append(result.Warnings, "直连接口未配置 169.254 链路本地地址,可能导致客户端无法通过维护链路发现或连接设备。")
+}
+
+func hasLinkLocalAddress(input model.InterfaceConfig) bool {
+	addresses := input.Addresses
+	if len(addresses) == 0 && strings.TrimSpace(input.IP) != "" {
+		addresses = []model.InterfaceAddressConfig{{IP: strings.TrimSpace(input.IP), Prefix: input.Prefix}}
+	}
+	for _, address := range addresses {
+		ip := net.ParseIP(strings.TrimSpace(address.IP))
+		if ip == nil {
+			continue
+		}
+		ipv4 := ip.To4()
+		if ipv4 != nil && ipv4[0] == 169 && ipv4[1] == 254 {
+			return true
+		}
+	}
+	return false
+}
+
 func (s *Server) rollbackAppliedConfig(taskID string, filePath string, backupPath string, reason string) {
 	s.log.Warn("apply confirmation failed, restoring netplan file", "task_id", taskID, "file", filePath, "reason", reason)
 	_ = s.netplanSvc.Restore(filePath, backupPath)

+ 0 - 4
server/internal/network/validator/validator.go

@@ -56,10 +56,6 @@ func (s *Service) Validate(input model.InterfaceConfig) model.ValidateResponse {
 		seenAddresses[key] = struct{}{}
 		mask := net.CIDRMask(address.Prefix, 32)
 		validNetworks = append(validNetworks, &net.IPNet{IP: ip.Mask(mask), Mask: mask})
-		ipv4 := ip.To4()
-		if ipv4[0] == 169 && ipv4[1] == 254 {
-			resp.Warnings = append(resp.Warnings, "目标接口使用的是链路本地地址,通常仅适合同链路通信。")
-		}
 	}
 	seenRoutes := make(map[string]struct{})
 	for _, route := range routes {