Files
findos/internal/clouddetect/loader.go
Dev 4d51c65060
Some checks failed
Go CI / test (push) Has been cancelled
up
2025-09-13 12:30:01 +03:00

51 lines
1.2 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
}