golib/cmd/check-ip/main.go

169 lines
5.7 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/therootcompany/golib/net/dataset"
"github.com/therootcompany/golib/net/gitshallow"
"github.com/therootcompany/golib/net/httpcache"
"github.com/therootcompany/golib/net/ipcohort"
)
const (
inboundSingleURL = "https://github.com/bitwire-it/ipblocklist/raw/refs/heads/main/tables/inbound/single_ips.txt"
inboundNetworkURL = "https://github.com/bitwire-it/ipblocklist/raw/refs/heads/main/tables/inbound/networks.txt"
outboundSingleURL = "https://github.com/bitwire-it/ipblocklist/raw/refs/heads/main/tables/outbound/single_ips.txt"
outboundNetworkURL = "https://github.com/bitwire-it/ipblocklist/raw/refs/heads/main/tables/outbound/networks.txt"
)
func main() {
dataDir := flag.String("data-dir", "", "blacklist cache dir (default ~/.cache/bitwire-it)")
gitURL := flag.String("git", "", "git URL to clone/pull blacklist from")
whitelist := flag.String("whitelist", "", "path to whitelist file")
inbound := flag.String("inbound", "", "comma-separated paths to inbound blacklist files")
outbound := flag.String("outbound", "", "comma-separated paths to outbound blacklist files")
geoipConf := flag.String("geoip-conf", "", "path to GeoIP.conf (auto-discovered if absent)")
cityDB := flag.String("city-db", "", "path to GeoLite2-City.mmdb (skips auto-download)")
asnDB := flag.String("asn-db", "", "path to GeoLite2-ASN.mmdb (skips auto-download)")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <ip-address>\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
if flag.NArg() != 1 {
flag.Usage()
os.Exit(1)
}
ipStr := flag.Arg(0)
// -- Blacklist ----------------------------------------------------------
var inboundDS, outboundDS *dataset.Dataset[ipcohort.Cohort]
switch {
case *inbound != "" || *outbound != "":
inboundDS = cohortDataset(dataset.NopSyncer{}, splitPaths(*inbound)...)
outboundDS = cohortDataset(dataset.NopSyncer{}, splitPaths(*outbound)...)
case *gitURL != "":
dir := cacheDir(*dataDir, "bitwire-it")
gr := gitshallow.New(*gitURL, dir, 1, "")
inSingle := gr.File("tables/inbound/single_ips.txt")
inNetwork := gr.File("tables/inbound/networks.txt")
outSingle := gr.File("tables/outbound/single_ips.txt")
outNetwork:= gr.File("tables/outbound/networks.txt")
// Each File.Fetch deduplicates the git pull via the shared Repo.
inboundDS = cohortDataset(dataset.MultiSyncer{inSingle, inNetwork}, inSingle.Path(), inNetwork.Path())
outboundDS = cohortDataset(dataset.MultiSyncer{outSingle, outNetwork}, outSingle.Path(), outNetwork.Path())
default:
dir := cacheDir(*dataDir, "bitwire-it")
inSingle := httpcache.New(inboundSingleURL, filepath.Join(dir, "inbound_single_ips.txt"))
inNetwork := httpcache.New(inboundNetworkURL, filepath.Join(dir, "inbound_networks.txt"))
outSingle := httpcache.New(outboundSingleURL, filepath.Join(dir, "outbound_single_ips.txt"))
outNetwork:= httpcache.New(outboundNetworkURL, filepath.Join(dir, "outbound_networks.txt"))
inboundDS = cohortDataset(dataset.MultiSyncer{inSingle, inNetwork}, inSingle.Path, inNetwork.Path)
outboundDS = cohortDataset(dataset.MultiSyncer{outSingle, outNetwork}, outSingle.Path, outNetwork.Path)
}
var whitelistDS *dataset.Dataset[ipcohort.Cohort]
if *whitelist != "" {
whitelistDS = cohortDataset(dataset.NopSyncer{}, splitPaths(*whitelist)...)
}
for _, ds := range []*dataset.Dataset[ipcohort.Cohort]{whitelistDS, inboundDS, outboundDS} {
if ds == nil {
continue
}
if err := ds.Init(); err != nil {
fmt.Fprintf(os.Stderr, "error: blacklist: %v\n", err)
os.Exit(1)
}
}
fmt.Fprintf(os.Stderr, "Loaded inbound=%d outbound=%d\n",
inboundDS.Load().Size(), outboundDS.Load().Size())
// -- GeoIP (optional) --------------------------------------------------
geo, err := setupGeo(*geoipConf, *cityDB, *asnDB)
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
if err := geo.Init(); err != nil {
fmt.Fprintf(os.Stderr, "error: geoip: %v\n", err)
os.Exit(1)
}
// -- Background refresh ------------------------------------------------
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for _, ds := range []*dataset.Dataset[ipcohort.Cohort]{whitelistDS, inboundDS, outboundDS} {
if ds != nil {
go ds.Run(ctx, 47*time.Minute)
}
}
geo.Run(ctx, 47*time.Minute)
// -- Check and report --------------------------------------------------
blockedIn := isBlocked(ipStr, whitelistDS, inboundDS)
blockedOut := isBlocked(ipStr, whitelistDS, outboundDS)
switch {
case blockedIn && blockedOut:
fmt.Printf("%s is BLOCKED (inbound + outbound)\n", ipStr)
case blockedIn:
fmt.Printf("%s is BLOCKED (inbound)\n", ipStr)
case blockedOut:
fmt.Printf("%s is BLOCKED (outbound)\n", ipStr)
default:
fmt.Printf("%s is allowed\n", ipStr)
}
geo.PrintInfo(os.Stdout, ipStr)
if blockedIn || blockedOut {
os.Exit(1)
}
}
// cohortDataset creates a Dataset that fetches via syncer and loads paths as a Cohort.
func cohortDataset(syncer dataset.Syncer, paths ...string) *dataset.Dataset[ipcohort.Cohort] {
return dataset.New(syncer, func() (*ipcohort.Cohort, error) {
return ipcohort.LoadFiles(paths...)
})
}
func isBlocked(ip string, whitelist, cohort *dataset.Dataset[ipcohort.Cohort]) bool {
if cohort == nil {
return false
}
if whitelist != nil && whitelist.Load().Contains(ip) {
return false
}
return cohort.Load().Contains(ip)
}
func cacheDir(override, sub string) string {
if override != "" {
return override
}
base, err := os.UserCacheDir()
if err != nil {
base = filepath.Join(os.Getenv("HOME"), ".cache")
}
return filepath.Join(base, sub)
}
func splitPaths(s string) []string {
return strings.Split(s, ",")
}