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,27 +294,43 @@ func main() {
|
|||||||
}
|
}
|
||||||
maxmindDir := filepath.Join(cfg.CacheDir, "maxmind")
|
maxmindDir := filepath.Join(cfg.CacheDir, "maxmind")
|
||||||
authHeader := http.Header{"Authorization": []string{cfg.GeoIPBasicAuth}}
|
authHeader := http.Header{"Authorization": []string{cfg.GeoIPBasicAuth}}
|
||||||
|
newGeoSet := func(downloadBase string) *dataset.Set {
|
||||||
|
cityCacher := httpcache.New(
|
||||||
|
downloadBase+"/GeoLite2-City/download?suffix=tar.gz",
|
||||||
|
filepath.Join(maxmindDir, geoip.TarGzName(geoip.CityEdition)))
|
||||||
|
cityCacher.Header = authHeader
|
||||||
|
cityCacher.MaxAge = 3 * 24 * time.Hour
|
||||||
|
asnCacher := httpcache.New(
|
||||||
|
downloadBase+"/GeoLite2-ASN/download?suffix=tar.gz",
|
||||||
|
filepath.Join(maxmindDir, geoip.TarGzName(geoip.ASNEdition)))
|
||||||
|
asnCacher.Header = authHeader
|
||||||
|
asnCacher.MaxAge = 3 * 24 * time.Hour
|
||||||
|
return dataset.NewSet(cityCacher, asnCacher)
|
||||||
|
}
|
||||||
downloadBase := geoip.DownloadBase
|
downloadBase := geoip.DownloadBase
|
||||||
if cfg.GeoIPURL != "" {
|
if cfg.GeoIPURL != "" {
|
||||||
downloadBase = strings.TrimRight(cfg.GeoIPURL, "/")
|
downloadBase = strings.TrimRight(cfg.GeoIPURL, "/")
|
||||||
}
|
}
|
||||||
cityCacher := httpcache.New(
|
geoSet := newGeoSet(downloadBase)
|
||||||
downloadBase+"/GeoLite2-City/download?suffix=tar.gz",
|
|
||||||
filepath.Join(maxmindDir, geoip.TarGzName(geoip.CityEdition)))
|
|
||||||
cityCacher.Header = authHeader
|
|
||||||
cityCacher.MaxAge = 3 * 24 * time.Hour
|
|
||||||
asnCacher := httpcache.New(
|
|
||||||
downloadBase+"/GeoLite2-ASN/download?suffix=tar.gz",
|
|
||||||
filepath.Join(maxmindDir, geoip.TarGzName(geoip.ASNEdition)))
|
|
||||||
asnCacher.Header = authHeader
|
|
||||||
asnCacher.MaxAge = 3 * 24 * time.Hour
|
|
||||||
geoSet := dataset.NewSet(cityCacher, asnCacher)
|
|
||||||
cfg.geo = dataset.Add(geoSet, func(_ context.Context) (*geoip.Databases, error) {
|
cfg.geo = dataset.Add(geoSet, func(_ context.Context) (*geoip.Databases, error) {
|
||||||
return geoip.Open(maxmindDir)
|
return geoip.Open(maxmindDir)
|
||||||
})
|
})
|
||||||
fmt.Fprint(os.Stderr, "Loading geoip... ")
|
fmt.Fprint(os.Stderr, "Loading geoip... ")
|
||||||
tGeo := time.Now()
|
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)
|
fmt.Fprintln(os.Stderr)
|
||||||
log.Printf("geoip unavailable: %v", err)
|
log.Printf("geoip unavailable: %v", err)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user