mirror of
https://github.com/therootcompany/golib.git
synced 2026-11-04 06:55:00 +00:00
IPv4 host + CIDR membership testing backed by sorted /32 binary search and a linear scan of CIDR ranges. CSV/text loader with bounded reads (maxBytes is required so callers pick a limit appropriate to their input source — 1 KB allowlists through 100+ MB threat feeds). Returns errors for unparseable IPs so callers can decide fail-open vs fail-closed; partial cohorts are returned alongside parse errors.
35 lines
666 B
Go
35 lines
666 B
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
)
|
|
|
|
const (
|
|
formatPretty = "pretty"
|
|
formatTSV = "tsv"
|
|
formatCSV = "csv"
|
|
formatJSON = "json"
|
|
)
|
|
|
|
// parseFormat resolves the --format flag value, auto-detecting based on
|
|
// stdout (pretty on TTY, tsv when piped) when s is empty.
|
|
func parseFormat(s string) (string, error) {
|
|
switch s {
|
|
case "":
|
|
if isTTYish(os.Stdout) {
|
|
return formatPretty, nil
|
|
}
|
|
return formatTSV, nil
|
|
case formatPretty, formatTSV, formatCSV, formatJSON:
|
|
return s, nil
|
|
default:
|
|
return "", fmt.Errorf("unknown format %q (want pretty, tsv, csv, json)", s)
|
|
}
|
|
}
|
|
|
|
type result struct {
|
|
IP string `json:"ip"`
|
|
Found bool `json:"found"`
|
|
}
|