feat(scan): несколько CIDR в одном поле, перечисление через запятую

Made-with: Cursor
This commit is contained in:
Луценко Андрей Анатольевич
2026-04-10 15:39:52 +10:00
parent a36edbc26b
commit 2388b5f3ff
3 changed files with 33 additions and 7 deletions
+11 -5
View File
@@ -14,7 +14,7 @@ const maxCIDRSExpanded = 16384
var cidrRangeRE = regexp.MustCompile(`^\s*(?P<left>.+?/\d+)\s*-\s*(?P<right>.+?/\d+)\s*$`)
// expandCIDRSFromRequest раскрывает каждую строку: один CIDR или диапазон "start/end mask - end/end mask".
// expandCIDRSFromRequest раскрывает каждую строку: перечисление через запятую, один CIDR или диапазон a/b - c/d.
func expandCIDRSFromRequest(inputs []string) ([]string, error) {
var all []string
for _, raw := range inputs {
@@ -22,11 +22,17 @@ func expandCIDRSFromRequest(inputs []string) ([]string, error) {
if raw == "" {
continue
}
part, err := expandCIDREntry(raw)
if err != nil {
return nil, err
for _, piece := range strings.Split(raw, ",") {
piece = strings.TrimSpace(piece)
if piece == "" {
continue
}
part, err := expandCIDREntry(piece)
if err != nil {
return nil, err
}
all = append(all, part...)
}
all = append(all, part...)
}
if len(all) == 0 {
return nil, errors.New("cidrs must not be empty")
+20
View File
@@ -53,3 +53,23 @@ func TestExpandCIDRSFromRequestDedup(t *testing.T) {
t.Fatalf("дедуп: %v", out)
}
}
func TestExpandCIDRSFromRequestCommaList(t *testing.T) {
out, err := expandCIDRSFromRequest([]string{"172.16.0.0/24, 192.168.160.0/24 ,192.168.170.0/24"})
if err != nil {
t.Fatal(err)
}
if len(out) != 3 {
t.Fatalf("ожидали 3 подсети, получили %d: %v", len(out), out)
}
}
func TestExpandCIDRSFromRequestCommaAndRange(t *testing.T) {
out, err := expandCIDRSFromRequest([]string{"10.0.0.0/30,192.168.1.0/24-192.168.2.0/24"})
if err != nil {
t.Fatal(err)
}
if len(out) != 1+2 {
t.Fatalf("ожидали /30 + 2×/24, получили %d: %v", len(out), out)
}
}