AJ ONeal 8731eaf10b
refactor: decouple gitdataset/ipcohort for multi-file repos
gitshallow: fix double-fetch (pull already fetches), drop redundant -C flags
gitdataset: split into GitDataset[T] (file+atomic) and GitRepo (git+multi-dataset)
  - NewDataset for file-only use, AddDataset to register with a GitRepo
  - one clone/fetch per repo regardless of how many datasets it has
ipcohort: split Cohort into hosts (sorted /32, binary search) + nets (CIDRs, linear)
  - fixes false negatives when broad CIDRs (e.g. /8) precede specific entries
  - fixes Parse() sort-before-copy order bug
  - ReadAll always sorts; unsorted param removed (was dead code)
2026-04-19 22:34:25 -06:00

56 lines
1.2 KiB
Go

package main
import (
"fmt"
"os"
"path/filepath"
"github.com/therootcompany/golib/fs/dataset"
"github.com/therootcompany/golib/net/ipcohort"
)
func main() {
if len(os.Args) < 3 {
fmt.Fprintf(os.Stderr, "Usage: %s <blacklist.csv> <ip-address> [git-url]\n", os.Args[0])
os.Exit(1)
}
dataPath := os.Args[1]
ipStr := os.Args[2]
gitURL := ""
if len(os.Args) >= 4 {
gitURL = os.Args[3]
}
var blacklist *dataset.File[ipcohort.Cohort]
if gitURL != "" {
repoDir := filepath.Dir(dataPath)
relPath := filepath.Base(dataPath)
repo := dataset.NewRepo(gitURL, repoDir)
blacklist = dataset.AddFile(repo, relPath, ipcohort.LoadFile)
fmt.Fprintf(os.Stderr, "Syncing %q ...\n", repoDir)
if err := repo.Init(); err != nil {
fmt.Fprintf(os.Stderr, "error: git sync: %v\n", err)
os.Exit(1)
}
} else {
blacklist = dataset.NewFile(dataPath, ipcohort.LoadFile)
fmt.Fprintf(os.Stderr, "Loading %q ...\n", dataPath)
if err := blacklist.Reload(); err != nil {
fmt.Fprintf(os.Stderr, "error: load: %v\n", err)
os.Exit(1)
}
}
c := blacklist.Load()
fmt.Fprintf(os.Stderr, "Loaded %d entries\n", c.Size())
if c.Contains(ipStr) {
fmt.Printf("%s is BLOCKED\n", ipStr)
os.Exit(1)
}
fmt.Printf("%s is allowed\n", ipStr)
}