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
|
# 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
|
||||||
|
|||||||
@ -115,16 +115,19 @@ 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
|
dbDir := cfg.DatabaseDirectory
|
||||||
if dbDir == "" {
|
if dbDir == "" {
|
||||||
if d, err := geoip.DefaultCacheDir(); err == nil {
|
if dbDir, err = geoip.DefaultCacheDir(); err != nil {
|
||||||
dbDir = d
|
fmt.Fprintf(os.Stderr, "error: geoip cache dir: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if err := os.MkdirAll(dbDir, 0o755); err != nil {
|
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)
|
d := geoip.New(cfg.AccountID, cfg.LicenseKey)
|
||||||
if resolvedCityPath == "" {
|
if resolvedCityPath == "" {
|
||||||
@ -135,7 +138,6 @@ func main() {
|
|||||||
}
|
}
|
||||||
cityDS = newGeoIPDataset(d, geoip.CityEdition, resolvedCityPath)
|
cityDS = newGeoIPDataset(d, geoip.CityEdition, resolvedCityPath)
|
||||||
asnDS = newGeoIPDataset(d, geoip.ASNEdition, resolvedASNPath)
|
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,7 +243,7 @@ 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"]
|
||||||
@ -267,17 +265,15 @@ func printGeoInfo(ipStr string, cityDS, asnDS *dataset.Dataset[geoip2.Reader]) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func cohortSize(ds *dataset.View[ipcohort.Cohort]) int {
|
func cohortSize(ds *dataset.View[ipcohort.Cohort]) int {
|
||||||
if ds == nil {
|
if ds == nil {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user