1
0
espelhamento de https://github.com/therootcompany/golib.git sincronizado 2026-11-04 06:55:00 +00:00
AJ ONeal bea4c3de0a
feat(net/ipcohort): IP block/allow cohort filter
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.
2026-05-02 15:41:38 -06:00

35 linhas
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"`
}