Make the download of geoip data files conditional

This commit is contained in:
Arturo Filastò 2018-06-22 11:17:51 +02:00
parent 1bba0c7899
commit 15c901ed68
2 changed files with 27 additions and 16 deletions

View File

@ -19,6 +19,10 @@ func init() {
return err
}
if err = ctx.MaybeDownloadDataFiles(); err != nil {
log.WithError(err).Error("failed to download data files")
}
geoipPath := utils.GeoIPDir(ctx.Home)
if *shouldUpdate {
utils.DownloadGeoIPDatabaseFiles(geoipPath)

39
ooni.go
View File

@ -3,6 +3,7 @@ package ooni
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"github.com/apex/log"
@ -56,8 +57,11 @@ func (c *Context) MaybeLocationLookup() error {
func (c *Context) LocationLookup() error {
var err error
geoipDir := utils.GeoIPDir(c.Home)
if err = c.MaybeDownloadDataFiles(); err != nil {
log.WithError(err).Error("failed to download data files")
}
geoipDir := utils.GeoIPDir(c.Home)
c.Location, err = utils.GeoIPLookup(geoipDir)
if err != nil {
return err
@ -77,6 +81,24 @@ func (c *Context) MaybeOnboarding() error {
return nil
}
// MaybeDownloadDataFiles will download geoip data files if they are not present
func (c *Context) MaybeDownloadDataFiles() error {
geoipDir := utils.GeoIPDir(c.Home)
if _, err := os.Stat(path.Join(geoipDir, "GeoLite2-Country.mmdb")); os.IsNotExist(err) {
log.Debugf("Downloading GeoIP database files")
if err := utils.DownloadGeoIPDatabaseFiles(geoipDir); err != nil {
return err
}
}
if _, err := os.Stat(path.Join(geoipDir, "GeoIP.dat")); os.IsNotExist(err) {
log.Debugf("Downloading legacy GeoIP database Files")
if err := utils.DownloadLegacyGeoIPDatabaseFiles(geoipDir); err != nil {
return err
}
}
return nil
}
// Init the OONI manager
func (c *Context) Init() error {
var err error
@ -128,28 +150,13 @@ func NewContext(configPath string, homePath string) *Context {
// MaybeInitializeHome does the setup for a new OONI Home
func MaybeInitializeHome(home string) error {
firstRun := false
for _, d := range utils.RequiredDirs(home) {
if _, e := os.Stat(d); e != nil {
firstRun = true
if err := os.MkdirAll(d, 0700); err != nil {
return err
}
}
}
if firstRun == true {
log.Info("This is the first time you are running OONI Probe. Downloading some files.")
geoipDir := utils.GeoIPDir(home)
log.Debugf("Downloading GeoIP database files")
if err := utils.DownloadGeoIPDatabaseFiles(geoipDir); err != nil {
return err
}
log.Debugf("Downloading legacy GeoIP database Files")
if err := utils.DownloadLegacyGeoIPDatabaseFiles(geoipDir); err != nil {
return err
}
}
return nil
}