mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 20:58:00 +00:00
- geoip.Databases: wraps city+ASN datasets with nil-safe Init/Run/PrintInfo - geoip.(*Downloader).NewDatabases: builds Databases from downloader - cmd/check-ip/geo.go: setupGeo() handles conf parsing, dir creation, DB path resolution - cmd/check-ip/blacklist.go: isBlocked() + cohortSize() moved here - cmd/check-ip/main.go: flags, source selection, init, check, print — nothing else
41 lines
1.1 KiB
Go
41 lines
1.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/therootcompany/golib/net/geoip"
|
|
)
|
|
|
|
// setupGeo parses geoip-conf (if given) and returns a Databases ready to Init.
|
|
// Returns nil if no geoip flags were provided.
|
|
func setupGeo(confPath, cityPath, asnPath string) (*geoip.Databases, error) {
|
|
if confPath == "" && cityPath == "" && asnPath == "" {
|
|
return nil, nil
|
|
}
|
|
if confPath != "" {
|
|
cfg, err := geoip.ParseConf(confPath)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("geoip-conf: %w", err)
|
|
}
|
|
dbDir := cfg.DatabaseDirectory
|
|
if dbDir == "" {
|
|
if dbDir, err = geoip.DefaultCacheDir(); err != nil {
|
|
return nil, fmt.Errorf("geoip cache dir: %w", err)
|
|
}
|
|
}
|
|
if err := os.MkdirAll(dbDir, 0o755); err != nil {
|
|
return nil, fmt.Errorf("mkdir %s: %w", dbDir, err)
|
|
}
|
|
if cityPath == "" {
|
|
cityPath = filepath.Join(dbDir, geoip.CityEdition+".mmdb")
|
|
}
|
|
if asnPath == "" {
|
|
asnPath = filepath.Join(dbDir, geoip.ASNEdition+".mmdb")
|
|
}
|
|
return geoip.New(cfg.AccountID, cfg.LicenseKey).NewDatabases(cityPath, asnPath), nil
|
|
}
|
|
return geoip.NewDatabases(cityPath, asnPath), nil
|
|
}
|