mirror of
https://github.com/therootcompany/golib.git
synced 2026-11-04 06:55:00 +00:00
fix(check-ip): fall back from geoip mirror
This commit is contained in:
parent
aa747348ae
commit
ee93b0839a
@ -294,10 +294,7 @@ func main() {
|
||||
}
|
||||
maxmindDir := filepath.Join(cfg.CacheDir, "maxmind")
|
||||
authHeader := http.Header{"Authorization": []string{cfg.GeoIPBasicAuth}}
|
||||
downloadBase := geoip.DownloadBase
|
||||
if cfg.GeoIPURL != "" {
|
||||
downloadBase = strings.TrimRight(cfg.GeoIPURL, "/")
|
||||
}
|
||||
newGeoSet := func(downloadBase string) *dataset.Set {
|
||||
cityCacher := httpcache.New(
|
||||
downloadBase+"/GeoLite2-City/download?suffix=tar.gz",
|
||||
filepath.Join(maxmindDir, geoip.TarGzName(geoip.CityEdition)))
|
||||
@ -308,13 +305,32 @@ func main() {
|
||||
filepath.Join(maxmindDir, geoip.TarGzName(geoip.ASNEdition)))
|
||||
asnCacher.Header = authHeader
|
||||
asnCacher.MaxAge = 3 * 24 * time.Hour
|
||||
geoSet := dataset.NewSet(cityCacher, asnCacher)
|
||||
return dataset.NewSet(cityCacher, asnCacher)
|
||||
}
|
||||
downloadBase := geoip.DownloadBase
|
||||
if cfg.GeoIPURL != "" {
|
||||
downloadBase = strings.TrimRight(cfg.GeoIPURL, "/")
|
||||
}
|
||||
geoSet := newGeoSet(downloadBase)
|
||||
cfg.geo = dataset.Add(geoSet, func(_ context.Context) (*geoip.Databases, error) {
|
||||
return geoip.Open(maxmindDir)
|
||||
})
|
||||
fmt.Fprint(os.Stderr, "Loading geoip... ")
|
||||
tGeo := time.Now()
|
||||
if err := geoSet.Load(context.Background()); err != nil {
|
||||
err = geoSet.Load(context.Background())
|
||||
if err != nil && cfg.GeoIPURL != "" && downloadBase != geoip.DownloadBase {
|
||||
fmt.Fprintln(os.Stderr)
|
||||
log.Printf("geoip mirror unavailable: %v; trying official download", err)
|
||||
downloadBase = geoip.DownloadBase
|
||||
geoSet = newGeoSet(downloadBase)
|
||||
cfg.geo = dataset.Add(geoSet, func(_ context.Context) (*geoip.Databases, error) {
|
||||
return geoip.Open(maxmindDir)
|
||||
})
|
||||
fmt.Fprint(os.Stderr, "Loading geoip from official source... ")
|
||||
tGeo = time.Now()
|
||||
err = geoSet.Load(context.Background())
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr)
|
||||
log.Printf("geoip unavailable: %v", err)
|
||||
} else {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user