2018-03-23 12:10:14 +01:00
|
|
|
package geoip
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/alecthomas/kingpin"
|
|
|
|
"github.com/apex/log"
|
2018-05-03 14:59:55 +02:00
|
|
|
"github.com/ooni/probe-cli/internal/cli/root"
|
2020-11-13 21:16:43 +01:00
|
|
|
"github.com/ooni/probe-cli/internal/ooni"
|
2018-06-22 14:25:30 +02:00
|
|
|
"github.com/ooni/probe-cli/internal/output"
|
2018-03-23 12:10:14 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
cmd := root.Command("geoip", "Perform a geoip lookup")
|
|
|
|
cmd.Action(func(_ *kingpin.ParseContext) error {
|
2020-11-13 21:16:43 +01:00
|
|
|
return dogeoip(defaultconfig)
|
2018-03-23 12:10:14 +01:00
|
|
|
})
|
|
|
|
}
|
2020-11-13 21:16:43 +01:00
|
|
|
|
|
|
|
type dogeoipconfig struct {
|
|
|
|
Logger log.Interface
|
|
|
|
NewProbeCLI func() (ooni.ProbeCLI, error)
|
|
|
|
SectionTitle func(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultconfig = dogeoipconfig{
|
|
|
|
Logger: log.Log,
|
|
|
|
NewProbeCLI: root.NewProbeCLI,
|
|
|
|
SectionTitle: output.SectionTitle,
|
|
|
|
}
|
|
|
|
|
|
|
|
func dogeoip(config dogeoipconfig) error {
|
|
|
|
config.SectionTitle("GeoIP lookup")
|
|
|
|
probeCLI, err := config.NewProbeCLI()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
engine, err := probeCLI.NewProbeEngine()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer engine.Close()
|
|
|
|
|
|
|
|
err = engine.MaybeLookupLocation()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
config.Logger.WithFields(log.Fields{
|
|
|
|
"type": "table",
|
|
|
|
"asn": engine.ProbeASNString(),
|
|
|
|
"network_name": engine.ProbeNetworkName(),
|
|
|
|
"country_code": engine.ProbeCC(),
|
|
|
|
"ip": engine.ProbeIP(),
|
|
|
|
}).Info("Looked up your location")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|