ooni-probe-cli/internal/cli/geoip/geoip.go

47 lines
1021 B
Go
Raw Normal View History

2018-03-23 12:10:14 +01:00
package geoip
import (
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/ooni/probe-cli/internal/cli/root"
"github.com/ooni/probe-cli/utils"
2018-03-23 12:10:14 +01:00
)
func init() {
cmd := root.Command("geoip", "Perform a geoip lookup")
shouldUpdate := cmd.Flag("update", "Update the geoip database").Bool()
cmd.Action(func(_ *kingpin.ParseContext) error {
log.Info("geoip")
ctx, err := root.Init()
if err != nil {
return err
}
if err = ctx.MaybeDownloadDataFiles(); err != nil {
log.WithError(err).Error("failed to download data files")
}
geoipPath := utils.GeoIPDir(ctx.Home)
2018-03-23 12:10:14 +01:00
if *shouldUpdate {
utils.DownloadGeoIPDatabaseFiles(geoipPath)
utils.DownloadLegacyGeoIPDatabaseFiles(geoipPath)
2018-03-23 12:10:14 +01:00
}
loc, err := utils.GeoIPLookup(geoipPath)
if err != nil {
return err
}
log.WithFields(log.Fields{
"asn": loc.ASN,
"network_name": loc.NetworkName,
"country_code": loc.CountryCode,
"ip": loc.IP,
}).Info("Looked up your location")
return nil
})
}