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 }