fix: check-ip fails on startup if data cannot be downloaded

This commit is contained in:
AJ ONeal 2026-04-20 12:08:03 -06:00
parent 34a54c2d66
commit 3e48e0a863
No known key found for this signature in database
2 changed files with 56 additions and 60 deletions

4
.gitignore vendored
View File

@ -10,8 +10,8 @@ env.*
# Project binaries # Project binaries
dist/ dist/
check-ip /check-ip
cmd/check-ip/check-ip /cmd/check-ip/check-ip
auth/csvauth/cmd/csvauth/csvauth auth/csvauth/cmd/csvauth/csvauth
cmd/auth-proxy/auth-proxy cmd/auth-proxy/auth-proxy
cmd/httplog/httplog cmd/httplog/httplog

View File

@ -115,27 +115,29 @@ func main() {
if *geoipConf != "" { if *geoipConf != "" {
cfg, err := geoip.ParseConf(*geoipConf) cfg, err := geoip.ParseConf(*geoipConf)
if err != nil { if err != nil {
fmt.Fprintf(os.Stderr, "warn: geoip-conf: %v\n", err) fmt.Fprintf(os.Stderr, "error: geoip-conf: %v\n", err)
} else { os.Exit(1)
dbDir := cfg.DatabaseDirectory
if dbDir == "" {
if d, err := geoip.DefaultCacheDir(); err == nil {
dbDir = d
}
}
if err := os.MkdirAll(dbDir, 0o755); err != nil {
fmt.Fprintf(os.Stderr, "warn: mkdir %s: %v\n", dbDir, err)
}
d := geoip.New(cfg.AccountID, cfg.LicenseKey)
if resolvedCityPath == "" {
resolvedCityPath = filepath.Join(dbDir, geoip.CityEdition+".mmdb")
}
if resolvedASNPath == "" {
resolvedASNPath = filepath.Join(dbDir, geoip.ASNEdition+".mmdb")
}
cityDS = newGeoIPDataset(d, geoip.CityEdition, resolvedCityPath)
asnDS = newGeoIPDataset(d, geoip.ASNEdition, resolvedASNPath)
} }
dbDir := cfg.DatabaseDirectory
if dbDir == "" {
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, "error: mkdir %s: %v\n", dbDir, err)
os.Exit(1)
}
d := geoip.New(cfg.AccountID, cfg.LicenseKey)
if resolvedCityPath == "" {
resolvedCityPath = filepath.Join(dbDir, geoip.CityEdition+".mmdb")
}
if resolvedASNPath == "" {
resolvedASNPath = filepath.Join(dbDir, geoip.ASNEdition+".mmdb")
}
cityDS = newGeoIPDataset(d, geoip.CityEdition, resolvedCityPath)
asnDS = newGeoIPDataset(d, geoip.ASNEdition, resolvedASNPath)
} else { } else {
// Manual paths: no auto-download, just open existing files. // Manual paths: no auto-download, just open existing files.
if resolvedCityPath != "" { if resolvedCityPath != "" {
@ -148,12 +150,14 @@ func main() {
if cityDS != nil { if cityDS != nil {
if err := cityDS.Init(); err != 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 asnDS != nil {
if err := asnDS.Init(); err != 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, func containsInbound(ip string,
whitelist, inbound *dataset.View[ipcohort.Cohort], whitelist, inbound *dataset.View[ipcohort.Cohort],
) bool { ) bool {
if whitelist != nil { if whitelist != nil && whitelist.Load().Contains(ip) {
if wl := whitelist.Load(); wl != nil && wl.Contains(ip) { return false
return false
}
} }
if inbound == nil { if inbound == nil {
return false return false
} }
c := inbound.Load() return inbound.Load().Contains(ip)
return c != nil && c.Contains(ip)
} }
func containsOutbound(ip string, func containsOutbound(ip string,
whitelist, outbound *dataset.View[ipcohort.Cohort], whitelist, outbound *dataset.View[ipcohort.Cohort],
) bool { ) bool {
if whitelist != nil { if whitelist != nil && whitelist.Load().Contains(ip) {
if wl := whitelist.Load(); wl != nil && wl.Contains(ip) { return false
return false
}
} }
if outbound == nil { if outbound == nil {
return false return false
} }
c := outbound.Load() return outbound.Load().Contains(ip)
return c != nil && c.Contains(ip)
} }
func printGeoInfo(ipStr string, cityDS, asnDS *dataset.Dataset[geoip2.Reader]) { func printGeoInfo(ipStr string, cityDS, asnDS *dataset.Dataset[geoip2.Reader]) {
@ -245,36 +243,34 @@ func printGeoInfo(ipStr string, cityDS, asnDS *dataset.Dataset[geoip2.Reader]) {
stdIP := ip.AsSlice() stdIP := ip.AsSlice()
if cityDS != nil { if cityDS != nil {
if r := cityDS.Load(); r != nil { r := cityDS.Load()
if rec, err := r.City(stdIP); err == nil { if rec, err := r.City(stdIP); err == nil {
city := rec.City.Names["en"] city := rec.City.Names["en"]
country := rec.Country.Names["en"] country := rec.Country.Names["en"]
iso := rec.Country.IsoCode iso := rec.Country.IsoCode
var parts []string var parts []string
if city != "" { if city != "" {
parts = append(parts, city) parts = append(parts, city)
} }
if len(rec.Subdivisions) > 0 { if len(rec.Subdivisions) > 0 {
if sub := rec.Subdivisions[0].Names["en"]; sub != "" && sub != city { if sub := rec.Subdivisions[0].Names["en"]; sub != "" && sub != city {
parts = append(parts, sub) parts = append(parts, sub)
}
}
if country != "" {
parts = append(parts, fmt.Sprintf("%s (%s)", country, iso))
}
if len(parts) > 0 {
fmt.Printf(" Location: %s\n", strings.Join(parts, ", "))
} }
} }
if country != "" {
parts = append(parts, fmt.Sprintf("%s (%s)", country, iso))
}
if len(parts) > 0 {
fmt.Printf(" Location: %s\n", strings.Join(parts, ", "))
}
} }
} }
if asnDS != nil { if asnDS != nil {
if r := asnDS.Load(); r != nil { r := asnDS.Load()
if rec, err := r.ASN(stdIP); err == nil && rec.AutonomousSystemNumber != 0 { if rec, err := r.ASN(stdIP); err == nil && rec.AutonomousSystemNumber != 0 {
fmt.Printf(" ASN: AS%d %s\n", fmt.Printf(" ASN: AS%d %s\n",
rec.AutonomousSystemNumber, rec.AutonomousSystemOrganization) rec.AutonomousSystemNumber, rec.AutonomousSystemOrganization)
}
} }
} }
} }