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

50 lines
1.1 KiB
Go
Raw Normal View History

2018-03-23 12:10:14 +01:00
package geoip
import (
2018-06-22 14:25:30 +02:00
"fmt"
2018-03-23 12:10:14 +01:00
"github.com/alecthomas/kingpin"
"github.com/apex/log"
"github.com/ooni/probe-cli/internal/cli/root"
2018-06-22 14:25:30 +02:00
"github.com/ooni/probe-cli/internal/output"
"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 {
2018-06-22 14:25:30 +02:00
output.SectionTitle("GeoIP lookup")
2018-03-23 12:10:14 +01:00
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)
}
loc, err := utils.GeoIPLookup(geoipPath)
if err != nil {
return err
}
log.WithFields(log.Fields{
2018-06-22 14:25:30 +02:00
"type": "table",
"asn": fmt.Sprintf("AS%d", loc.ASN),
2018-03-23 12:10:14 +01:00
"network_name": loc.NetworkName,
"country_code": loc.CountryCode,
"ip": loc.IP,
}).Info("Looked up your location")
return nil
})
}