mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 12:48:00 +00:00
Blocklist: - Add -inbound, -outbound, -whitelist flags for explicit file paths - buildSources() replaces the old constructor trio; explicit flags always win - -data-dir and -git still work as defaults for the bitwire-it layout GeoIP: - Auto-discover GeoIP.conf from ./GeoIP.conf then ~/.config/maxmind/GeoIP.conf - If no conf found and no -city-db/-asn-db given: geoip disabled silently - If no conf but paths given: use those files (Init fails if absent)
66 lines
1.9 KiB
Go
66 lines
1.9 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/therootcompany/golib/net/geoip"
|
|
)
|
|
|
|
// discoverConf looks for GeoIP.conf in the current directory and then
|
|
// at ~/.config/maxmind/GeoIP.conf. Returns the path or "".
|
|
func discoverConf() string {
|
|
if _, err := os.Stat("GeoIP.conf"); err == nil {
|
|
return "GeoIP.conf"
|
|
}
|
|
if home, err := os.UserHomeDir(); err == nil {
|
|
p := filepath.Join(home, ".config", "maxmind", "GeoIP.conf")
|
|
if _, err := os.Stat(p); err == nil {
|
|
return p
|
|
}
|
|
}
|
|
return ""
|
|
}
|
|
|
|
// setupGeo returns a Databases ready to Init, or nil if geoip is not configured.
|
|
//
|
|
// - confPath="" → auto-discover GeoIP.conf from cwd and ~/.config/maxmind/
|
|
// - conf found → auto-download; cityPath/asnPath override the default locations
|
|
// - conf absent → cityPath and asnPath must point to existing .mmdb files
|
|
// - no conf and no paths → geoip disabled (returns nil)
|
|
func setupGeo(confPath, cityPath, asnPath string) (*geoip.Databases, error) {
|
|
if confPath == "" {
|
|
confPath = discoverConf()
|
|
}
|
|
|
|
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
|
|
}
|
|
|
|
if cityPath == "" && asnPath == "" {
|
|
return nil, nil
|
|
}
|
|
// Explicit paths only — no auto-download. Init will fail if files are absent.
|
|
return geoip.NewDatabases(cityPath, asnPath), nil
|
|
}
|