mirror of
https://github.com/therootcompany/golib.git
synced 2026-04-24 12:48:00 +00:00
fix: check-ip fails on startup if data cannot be downloaded
This commit is contained in:
parent
34a54c2d66
commit
3e48e0a863
4
.gitignore
vendored
4
.gitignore
vendored
@ -10,8 +10,8 @@ env.*
|
||||
|
||||
# Project binaries
|
||||
dist/
|
||||
check-ip
|
||||
cmd/check-ip/check-ip
|
||||
/check-ip
|
||||
/cmd/check-ip/check-ip
|
||||
auth/csvauth/cmd/csvauth/csvauth
|
||||
cmd/auth-proxy/auth-proxy
|
||||
cmd/httplog/httplog
|
||||
|
||||
@ -115,16 +115,19 @@ func main() {
|
||||
if *geoipConf != "" {
|
||||
cfg, err := geoip.ParseConf(*geoipConf)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stderr, "warn: geoip-conf: %v\n", err)
|
||||
} else {
|
||||
fmt.Fprintf(os.Stderr, "error: geoip-conf: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
dbDir := cfg.DatabaseDirectory
|
||||
if dbDir == "" {
|
||||
if d, err := geoip.DefaultCacheDir(); err == nil {
|
||||
dbDir = d
|
||||
if dbDir, err = geoip.DefaultCacheDir(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error: geoip cache dir: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
if err := os.MkdirAll(dbDir, 0o755); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "warn: mkdir %s: %v\n", dbDir, err)
|
||||
fmt.Fprintf(os.Stderr, "error: mkdir %s: %v\n", dbDir, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
d := geoip.New(cfg.AccountID, cfg.LicenseKey)
|
||||
if resolvedCityPath == "" {
|
||||
@ -135,7 +138,6 @@ func main() {
|
||||
}
|
||||
cityDS = newGeoIPDataset(d, geoip.CityEdition, resolvedCityPath)
|
||||
asnDS = newGeoIPDataset(d, geoip.ASNEdition, resolvedASNPath)
|
||||
}
|
||||
} else {
|
||||
// Manual paths: no auto-download, just open existing files.
|
||||
if resolvedCityPath != "" {
|
||||
@ -148,12 +150,14 @@ func main() {
|
||||
|
||||
if cityDS != nil {
|
||||
if err := cityDS.Init(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "warn: city DB: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "error: city DB: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
if asnDS != nil {
|
||||
if err := asnDS.Init(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "warn: ASN DB: %v\n", err)
|
||||
fmt.Fprintf(os.Stderr, "error: ASN DB: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
@ -210,31 +214,25 @@ func newGeoIPDataset(d *geoip.Downloader, edition, path string) *dataset.Dataset
|
||||
func containsInbound(ip string,
|
||||
whitelist, inbound *dataset.View[ipcohort.Cohort],
|
||||
) bool {
|
||||
if whitelist != nil {
|
||||
if wl := whitelist.Load(); wl != nil && wl.Contains(ip) {
|
||||
if whitelist != nil && whitelist.Load().Contains(ip) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if inbound == nil {
|
||||
return false
|
||||
}
|
||||
c := inbound.Load()
|
||||
return c != nil && c.Contains(ip)
|
||||
return inbound.Load().Contains(ip)
|
||||
}
|
||||
|
||||
func containsOutbound(ip string,
|
||||
whitelist, outbound *dataset.View[ipcohort.Cohort],
|
||||
) bool {
|
||||
if whitelist != nil {
|
||||
if wl := whitelist.Load(); wl != nil && wl.Contains(ip) {
|
||||
if whitelist != nil && whitelist.Load().Contains(ip) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
if outbound == nil {
|
||||
return false
|
||||
}
|
||||
c := outbound.Load()
|
||||
return c != nil && c.Contains(ip)
|
||||
return outbound.Load().Contains(ip)
|
||||
}
|
||||
|
||||
func printGeoInfo(ipStr string, cityDS, asnDS *dataset.Dataset[geoip2.Reader]) {
|
||||
@ -245,7 +243,7 @@ func printGeoInfo(ipStr string, cityDS, asnDS *dataset.Dataset[geoip2.Reader]) {
|
||||
stdIP := ip.AsSlice()
|
||||
|
||||
if cityDS != nil {
|
||||
if r := cityDS.Load(); r != nil {
|
||||
r := cityDS.Load()
|
||||
if rec, err := r.City(stdIP); err == nil {
|
||||
city := rec.City.Names["en"]
|
||||
country := rec.Country.Names["en"]
|
||||
@ -267,17 +265,15 @@ func printGeoInfo(ipStr string, cityDS, asnDS *dataset.Dataset[geoip2.Reader]) {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if asnDS != nil {
|
||||
if r := asnDS.Load(); r != nil {
|
||||
r := asnDS.Load()
|
||||
if rec, err := r.ASN(stdIP); err == nil && rec.AutonomousSystemNumber != 0 {
|
||||
fmt.Printf(" ASN: AS%d %s\n",
|
||||
rec.AutonomousSystemNumber, rec.AutonomousSystemOrganization)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func cohortSize(ds *dataset.View[ipcohort.Cohort]) int {
|
||||
if ds == nil {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user