up
Some checks failed
Go CI / test (push) Has been cancelled

This commit is contained in:
Dev
2025-09-13 12:30:01 +03:00
commit 4d51c65060
14 changed files with 1227 additions and 0 deletions

View File

@@ -0,0 +1,50 @@
package clouddetect
import (
"bufio"
"net"
"os"
"strings"
)
// LoadCIDRs reads the cloud_ranges.txt file and parses the CIDR blocks for each provider.
// The file format is simple: a provider name followed by a colon, then one CIDR per line.
// Blank lines and lines starting with '#' are ignored.
func LoadCIDRs(filePath string) (map[string][]*net.IPNet, error) {
f, err := os.Open(filePath)
if err != nil {
return nil, err
}
defer f.Close()
cidrs := make(map[string][]*net.IPNet)
var currentProvider string
scanner := bufio.NewScanner(f)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
if line == "" || strings.HasPrefix(line, "#") {
continue
}
// Provider header, e.g., "Cloudflare:"
if strings.HasSuffix(line, ":") {
currentProvider = strings.TrimSuffix(line, ":")
continue
}
// CIDR entry
if currentProvider == "" {
// Skip CIDR without a provider header.
continue
}
_, ipnet, err := net.ParseCIDR(line)
if err != nil {
// Invalid CIDR skip it.
continue
}
cidrs[currentProvider] = append(cidrs[currentProvider], ipnet)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return cidrs, nil
}