validator.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package validator
  2. import (
  3. "fmt"
  4. "net"
  5. "strings"
  6. "nettool/internal/model"
  7. )
  8. type Service struct{}
  9. func New() *Service { return &Service{} }
  10. func (s *Service) Validate(input model.InterfaceConfig) model.ValidateResponse {
  11. resp := model.ValidateResponse{Valid: false, Warnings: []string{}, Errors: []string{}}
  12. addresses := normalizedAddresses(input)
  13. routes := normalizedRoutes(input)
  14. if input.Interface == "" {
  15. resp.Errors = append(resp.Errors, "目标接口不能为空。")
  16. }
  17. for _, dns := range input.DNS {
  18. if dns == "" {
  19. continue
  20. }
  21. parsed := net.ParseIP(dns)
  22. if parsed == nil || parsed.To4() == nil {
  23. resp.Errors = append(resp.Errors, fmt.Sprintf("DNS 格式不正确:%s", dns))
  24. }
  25. }
  26. if input.Dhcp4 {
  27. resp.Valid = len(resp.Errors) == 0
  28. return resp
  29. }
  30. if len(addresses) == 0 {
  31. resp.Errors = append(resp.Errors, "至少需要填写一个 IP 地址。")
  32. }
  33. seenAddresses := make(map[string]struct{})
  34. validNetworks := make([]*net.IPNet, 0, len(addresses))
  35. for _, address := range addresses {
  36. ip := net.ParseIP(address.IP)
  37. if ip == nil || ip.To4() == nil {
  38. resp.Errors = append(resp.Errors, fmt.Sprintf("IP 地址格式不正确:%s", address.IP))
  39. continue
  40. }
  41. if address.Prefix < 0 || address.Prefix > 32 {
  42. resp.Errors = append(resp.Errors, fmt.Sprintf("前缀长度不正确:%s/%d", address.IP, address.Prefix))
  43. continue
  44. }
  45. if isNetworkOrBroadcastAddress(ip, address.Prefix) {
  46. resp.Errors = append(resp.Errors, fmt.Sprintf("接口 IP 不能填写网段地址或广播地址:%s/%d", address.IP, address.Prefix))
  47. continue
  48. }
  49. key := fmt.Sprintf("%s/%d", ip.String(), address.Prefix)
  50. if _, ok := seenAddresses[key]; ok {
  51. resp.Errors = append(resp.Errors, fmt.Sprintf("IP 地址重复:%s", key))
  52. continue
  53. }
  54. seenAddresses[key] = struct{}{}
  55. mask := net.CIDRMask(address.Prefix, 32)
  56. validNetworks = append(validNetworks, &net.IPNet{IP: ip.Mask(mask), Mask: mask})
  57. }
  58. seenRoutes := make(map[string]struct{})
  59. defaultRouteCount := 0
  60. for _, route := range routes {
  61. to := strings.TrimSpace(route.To)
  62. via := strings.TrimSpace(route.Via)
  63. if to == "" {
  64. resp.Errors = append(resp.Errors, "路由目标不能为空。")
  65. continue
  66. }
  67. if via == "" {
  68. resp.Errors = append(resp.Errors, fmt.Sprintf("路由 %s 的下一跳不能为空。", to))
  69. continue
  70. }
  71. if to == "default" {
  72. defaultRouteCount++
  73. if defaultRouteCount > 1 {
  74. resp.Errors = append(resp.Errors, "默认网关只能配置一个。")
  75. }
  76. } else {
  77. ip, ipNet, err := net.ParseCIDR(to)
  78. if err != nil || ip == nil || ip.To4() == nil || ipNet == nil {
  79. resp.Errors = append(resp.Errors, fmt.Sprintf("路由目标格式不正确:%s", to))
  80. } else {
  81. ipv4 := ip.To4()
  82. networkIP := ipv4.Mask(ipNet.Mask)
  83. prefix, _ := ipNet.Mask.Size()
  84. if !ipv4.Equal(networkIP) {
  85. resp.Errors = append(resp.Errors, fmt.Sprintf("路由目标必须填写目标网段地址(网络号):当前为 %s,应为 %s/%d", to, networkIP.String(), prefix))
  86. }
  87. }
  88. }
  89. gateway := net.ParseIP(via)
  90. if gateway == nil || gateway.To4() == nil {
  91. resp.Errors = append(resp.Errors, fmt.Sprintf("路由下一跳格式不正确:%s", via))
  92. continue
  93. }
  94. key := to + " via " + gateway.String()
  95. if _, ok := seenRoutes[key]; ok {
  96. resp.Errors = append(resp.Errors, fmt.Sprintf("路由重复:%s", key))
  97. continue
  98. }
  99. seenRoutes[key] = struct{}{}
  100. if len(validNetworks) > 0 && !containsGateway(validNetworks, gateway) {
  101. resp.Errors = append(resp.Errors, fmt.Sprintf("路由下一跳与任一目标接口 IP 都不在同一子网:%s", via))
  102. }
  103. }
  104. resp.Valid = len(resp.Errors) == 0
  105. return resp
  106. }
  107. func normalizedAddresses(input model.InterfaceConfig) []model.InterfaceAddressConfig {
  108. if len(input.Addresses) > 0 {
  109. return input.Addresses
  110. }
  111. if strings.TrimSpace(input.IP) == "" {
  112. return nil
  113. }
  114. return []model.InterfaceAddressConfig{{IP: strings.TrimSpace(input.IP), Prefix: input.Prefix}}
  115. }
  116. func normalizedRoutes(input model.InterfaceConfig) []model.InterfaceRouteConfig {
  117. if len(input.Routes) > 0 {
  118. return input.Routes
  119. }
  120. if strings.TrimSpace(input.Gateway) == "" {
  121. return nil
  122. }
  123. return []model.InterfaceRouteConfig{{To: "default", Via: strings.TrimSpace(input.Gateway)}}
  124. }
  125. func containsGateway(networks []*net.IPNet, gateway net.IP) bool {
  126. for _, network := range networks {
  127. if network.Contains(gateway) {
  128. return true
  129. }
  130. }
  131. return false
  132. }
  133. func isNetworkOrBroadcastAddress(ip net.IP, prefix int) bool {
  134. if prefix > 30 {
  135. return false
  136. }
  137. ipv4 := ip.To4()
  138. if ipv4 == nil {
  139. return false
  140. }
  141. mask := net.CIDRMask(prefix, 32)
  142. network := ipv4.Mask(mask)
  143. broadcast := make(net.IP, len(network))
  144. for i := range network {
  145. broadcast[i] = network[i] | ^mask[i]
  146. }
  147. return ipv4.Equal(network) || ipv4.Equal(broadcast)
  148. }