mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 20:58:00 +00:00
refactor: check-ip uses CheckIPConfig struct + flag.FlagSet, adds -V/help
This commit is contained in:
parent
f5f992ae94
commit
8c9924e559
@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"errors"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@ -23,26 +24,59 @@ const (
|
|||||||
outboundNetworkURL = "https://github.com/bitwire-it/ipblocklist/raw/refs/heads/main/tables/outbound/networks.txt"
|
outboundNetworkURL = "https://github.com/bitwire-it/ipblocklist/raw/refs/heads/main/tables/outbound/networks.txt"
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
type CheckIPConfig struct {
|
||||||
dataDir := flag.String("data-dir", "", "blacklist cache dir (default ~/.cache/bitwire-it)")
|
DataDir string
|
||||||
gitURL := flag.String("git", "", "git URL to clone/pull blacklist from")
|
GitURL string
|
||||||
whitelist := flag.String("whitelist", "", "path to whitelist file")
|
Whitelist string
|
||||||
inbound := flag.String("inbound", "", "comma-separated paths to inbound blacklist files")
|
Inbound string
|
||||||
outbound := flag.String("outbound", "", "comma-separated paths to outbound blacklist files")
|
Outbound string
|
||||||
geoipConf := flag.String("geoip-conf", "", "path to GeoIP.conf (auto-discovered if absent)")
|
GeoIPConf string
|
||||||
cityDB := flag.String("city-db", "", "path to GeoLite2-City.mmdb (skips auto-download)")
|
CityDB string
|
||||||
asnDB := flag.String("asn-db", "", "path to GeoLite2-ASN.mmdb (skips auto-download)")
|
ASNDB string
|
||||||
flag.Usage = func() {
|
}
|
||||||
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <ip-address>\n", os.Args[0])
|
|
||||||
flag.PrintDefaults()
|
|
||||||
}
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
if flag.NArg() != 1 {
|
func main() {
|
||||||
flag.Usage()
|
cfg := CheckIPConfig{}
|
||||||
|
|
||||||
|
fs := flag.NewFlagSet(os.Args[0], flag.ContinueOnError)
|
||||||
|
fs.StringVar(&cfg.DataDir, "data-dir", "", "blacklist cache dir (default ~/.cache/bitwire-it)")
|
||||||
|
fs.StringVar(&cfg.GitURL, "git", "", "git URL to clone/pull blacklist from")
|
||||||
|
fs.StringVar(&cfg.Whitelist, "whitelist", "", "path to whitelist file")
|
||||||
|
fs.StringVar(&cfg.Inbound, "inbound", "", "comma-separated paths to inbound blacklist files")
|
||||||
|
fs.StringVar(&cfg.Outbound, "outbound", "", "comma-separated paths to outbound blacklist files")
|
||||||
|
fs.StringVar(&cfg.GeoIPConf, "geoip-conf", "", "path to GeoIP.conf (auto-discovered if absent)")
|
||||||
|
fs.StringVar(&cfg.CityDB, "city-db", "", "path to GeoLite2-City.mmdb (skips auto-download)")
|
||||||
|
fs.StringVar(&cfg.ASNDB, "asn-db", "", "path to GeoLite2-ASN.mmdb (skips auto-download)")
|
||||||
|
fs.Usage = func() {
|
||||||
|
fmt.Fprintf(os.Stderr, "Usage: %s [flags] <ip-address>\n", os.Args[0])
|
||||||
|
fs.PrintDefaults()
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(os.Args) > 1 {
|
||||||
|
switch os.Args[1] {
|
||||||
|
case "-V", "-version", "--version", "version":
|
||||||
|
fmt.Fprintf(os.Stdout, "check-ip\n")
|
||||||
|
os.Exit(0)
|
||||||
|
case "help", "-help", "--help":
|
||||||
|
fmt.Fprintf(os.Stdout, "check-ip\n\n")
|
||||||
|
fs.SetOutput(os.Stdout)
|
||||||
|
fs.Usage()
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := fs.Parse(os.Args[1:]); err != nil {
|
||||||
|
if errors.Is(err, flag.ErrHelp) {
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
ipStr := flag.Arg(0)
|
|
||||||
|
if fs.NArg() != 1 {
|
||||||
|
fs.Usage()
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
ipStr := fs.Arg(0)
|
||||||
|
|
||||||
// -- Blacklist ----------------------------------------------------------
|
// -- Blacklist ----------------------------------------------------------
|
||||||
|
|
||||||
@ -53,20 +87,20 @@ func main() {
|
|||||||
)
|
)
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case *inbound != "" || *outbound != "":
|
case cfg.Inbound != "" || cfg.Outbound != "":
|
||||||
syncer = dataset.NopSyncer{}
|
syncer = dataset.NopSyncer{}
|
||||||
inboundPaths = splitPaths(*inbound)
|
inboundPaths = splitPaths(cfg.Inbound)
|
||||||
outboundPaths = splitPaths(*outbound)
|
outboundPaths = splitPaths(cfg.Outbound)
|
||||||
|
|
||||||
case *gitURL != "":
|
case cfg.GitURL != "":
|
||||||
dir := cacheDir(*dataDir, "bitwire-it")
|
dir := cacheDir(cfg.DataDir, "bitwire-it")
|
||||||
gr := gitshallow.New(*gitURL, dir, 1, "")
|
gr := gitshallow.New(cfg.GitURL, dir, 1, "")
|
||||||
syncer = gr
|
syncer = gr
|
||||||
inboundPaths = []string{gr.FilePath("tables/inbound/single_ips.txt"), gr.FilePath("tables/inbound/networks.txt")}
|
inboundPaths = []string{gr.FilePath("tables/inbound/single_ips.txt"), gr.FilePath("tables/inbound/networks.txt")}
|
||||||
outboundPaths = []string{gr.FilePath("tables/outbound/single_ips.txt"), gr.FilePath("tables/outbound/networks.txt")}
|
outboundPaths = []string{gr.FilePath("tables/outbound/single_ips.txt"), gr.FilePath("tables/outbound/networks.txt")}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
dir := cacheDir(*dataDir, "bitwire-it")
|
dir := cacheDir(cfg.DataDir, "bitwire-it")
|
||||||
inSingle := httpcache.New(inboundSingleURL, filepath.Join(dir, "inbound_single_ips.txt"))
|
inSingle := httpcache.New(inboundSingleURL, filepath.Join(dir, "inbound_single_ips.txt"))
|
||||||
inNetwork := httpcache.New(inboundNetworkURL, filepath.Join(dir, "inbound_networks.txt"))
|
inNetwork := httpcache.New(inboundNetworkURL, filepath.Join(dir, "inbound_networks.txt"))
|
||||||
outSingle := httpcache.New(outboundSingleURL, filepath.Join(dir, "outbound_single_ips.txt"))
|
outSingle := httpcache.New(outboundSingleURL, filepath.Join(dir, "outbound_single_ips.txt"))
|
||||||
@ -88,8 +122,8 @@ func main() {
|
|||||||
inboundDS.Load().Size(), outboundDS.Load().Size())
|
inboundDS.Load().Size(), outboundDS.Load().Size())
|
||||||
|
|
||||||
var whitelistDS *dataset.Dataset[ipcohort.Cohort]
|
var whitelistDS *dataset.Dataset[ipcohort.Cohort]
|
||||||
if *whitelist != "" {
|
if cfg.Whitelist != "" {
|
||||||
whitelistDS = dataset.New(dataset.NopSyncer{}, loadCohort(splitPaths(*whitelist)...))
|
whitelistDS = dataset.New(dataset.NopSyncer{}, loadCohort(splitPaths(cfg.Whitelist)...))
|
||||||
if err := whitelistDS.Init(); err != nil {
|
if err := whitelistDS.Init(); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error: whitelist: %v\n", err)
|
fmt.Fprintf(os.Stderr, "error: whitelist: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@ -98,7 +132,7 @@ func main() {
|
|||||||
|
|
||||||
// -- GeoIP (optional) --------------------------------------------------
|
// -- GeoIP (optional) --------------------------------------------------
|
||||||
|
|
||||||
geo, err := geoip.OpenDatabases(*geoipConf, *cityDB, *asnDB)
|
geo, err := geoip.OpenDatabases(cfg.GeoIPConf, cfg.CityDB, cfg.ASNDB)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
fmt.Fprintf(os.Stderr, "error: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user