2018-02-07 19:02:18 +01:00
|
|
|
package ooni
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-06-22 11:17:51 +02:00
|
|
|
"path"
|
2018-02-07 19:02:18 +01:00
|
|
|
|
2018-02-12 16:45:13 +01:00
|
|
|
"github.com/apex/log"
|
2018-05-03 14:59:55 +02:00
|
|
|
"github.com/ooni/probe-cli/config"
|
2018-06-25 17:49:17 +02:00
|
|
|
"github.com/ooni/probe-cli/internal/bindata"
|
2018-05-03 14:59:55 +02:00
|
|
|
"github.com/ooni/probe-cli/internal/database"
|
|
|
|
"github.com/ooni/probe-cli/internal/legacy"
|
2018-06-25 17:14:29 +02:00
|
|
|
"github.com/ooni/probe-cli/internal/onboard"
|
2018-05-03 14:59:55 +02:00
|
|
|
"github.com/ooni/probe-cli/utils"
|
2018-02-07 19:02:18 +01:00
|
|
|
"github.com/pkg/errors"
|
2018-09-05 18:40:37 +02:00
|
|
|
"upper.io/db.v3/lib/sqlbuilder"
|
2018-02-07 19:02:18 +01:00
|
|
|
)
|
|
|
|
|
2018-09-17 16:38:41 +02:00
|
|
|
const Version = "3.0.0-dev.1"
|
2018-07-14 16:58:08 +02:00
|
|
|
|
2018-02-12 17:29:03 +01:00
|
|
|
// Context for OONI Probe
|
|
|
|
type Context struct {
|
2018-06-22 10:29:47 +02:00
|
|
|
Config *config.Config
|
2018-09-05 18:40:37 +02:00
|
|
|
DB sqlbuilder.Database
|
2018-03-23 12:41:06 +01:00
|
|
|
Location *utils.LocationInfo
|
2018-09-17 16:16:56 +02:00
|
|
|
IsBatch bool
|
2018-03-23 12:41:06 +01:00
|
|
|
|
|
|
|
Home string
|
|
|
|
TempDir string
|
2018-03-23 12:10:14 +01:00
|
|
|
|
|
|
|
dbPath string
|
|
|
|
configPath string
|
2018-02-12 16:45:13 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 12:41:06 +01:00
|
|
|
// MaybeLocationLookup will lookup the location of the user unless it's already cached
|
|
|
|
func (c *Context) MaybeLocationLookup() error {
|
|
|
|
if c.Location == nil {
|
|
|
|
return c.LocationLookup()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LocationLookup lookup the location of the user via geoip
|
|
|
|
func (c *Context) LocationLookup() error {
|
|
|
|
var err error
|
|
|
|
|
2018-06-22 11:17:51 +02:00
|
|
|
if err = c.MaybeDownloadDataFiles(); err != nil {
|
|
|
|
log.WithError(err).Error("failed to download data files")
|
|
|
|
}
|
2018-03-23 12:41:06 +01:00
|
|
|
|
2018-06-22 11:17:51 +02:00
|
|
|
geoipDir := utils.GeoIPDir(c.Home)
|
2018-03-27 15:09:34 +02:00
|
|
|
c.Location, err = utils.GeoIPLookup(geoipDir)
|
2018-03-23 12:41:06 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-22 11:01:15 +02:00
|
|
|
// MaybeOnboarding will run the onboarding process only if the informed consent
|
|
|
|
// config option is set to false
|
|
|
|
func (c *Context) MaybeOnboarding() error {
|
|
|
|
if c.Config.InformedConsent == false {
|
2018-09-17 16:16:56 +02:00
|
|
|
if c.IsBatch == true {
|
|
|
|
return errors.New("cannot run onboarding in batch mode")
|
|
|
|
}
|
2018-06-25 17:14:29 +02:00
|
|
|
if err := onboard.Onboarding(c.Config); err != nil {
|
2018-06-22 11:01:15 +02:00
|
|
|
return errors.Wrap(err, "onboarding")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-22 11:17:51 +02:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2018-02-12 16:45:13 +01:00
|
|
|
// Init the OONI manager
|
2018-02-12 17:29:03 +01:00
|
|
|
func (c *Context) Init() error {
|
2018-03-23 12:10:14 +01:00
|
|
|
var err error
|
|
|
|
|
|
|
|
if err = legacy.MaybeMigrateHome(); err != nil {
|
2018-02-12 16:45:13 +01:00
|
|
|
return errors.Wrap(err, "migrating home")
|
|
|
|
}
|
2018-03-23 12:10:14 +01:00
|
|
|
|
2018-03-23 15:38:25 +01:00
|
|
|
if err = MaybeInitializeHome(c.Home); err != nil {
|
2018-03-23 12:10:14 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.configPath != "" {
|
|
|
|
log.Debugf("Reading config file from %s", c.configPath)
|
2018-06-22 10:29:47 +02:00
|
|
|
c.Config, err = config.ReadConfig(c.configPath)
|
2018-03-23 12:10:14 +01:00
|
|
|
} else {
|
|
|
|
log.Debug("Reading default config file")
|
2018-06-25 17:49:17 +02:00
|
|
|
c.Config, err = InitDefaultConfig(c.Home)
|
2018-03-23 12:10:14 +01:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-03-27 15:09:34 +02:00
|
|
|
c.dbPath = utils.DBDir(c.Home, "main")
|
2018-03-23 12:10:14 +01:00
|
|
|
log.Debugf("Connecting to database sqlite3://%s", c.dbPath)
|
|
|
|
db, err := database.Connect(c.dbPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.DB = db
|
|
|
|
|
2018-03-19 13:20:42 +01:00
|
|
|
tempDir, err := ioutil.TempDir("", "ooni")
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "creating TempDir")
|
|
|
|
}
|
|
|
|
c.TempDir = tempDir
|
|
|
|
|
2018-02-12 16:45:13 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-03-23 12:10:14 +01:00
|
|
|
// NewContext instance.
|
|
|
|
func NewContext(configPath string, homePath string) *Context {
|
2018-02-12 17:29:03 +01:00
|
|
|
return &Context{
|
2018-03-23 12:10:14 +01:00
|
|
|
Home: homePath,
|
2018-06-22 10:29:47 +02:00
|
|
|
Config: &config.Config{},
|
2018-03-23 12:10:14 +01:00
|
|
|
configPath: configPath,
|
2018-02-12 16:45:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-23 15:38:25 +01:00
|
|
|
// MaybeInitializeHome does the setup for a new OONI Home
|
|
|
|
func MaybeInitializeHome(home string) error {
|
2018-03-27 15:09:34 +02:00
|
|
|
for _, d := range utils.RequiredDirs(home) {
|
|
|
|
if _, e := os.Stat(d); e != nil {
|
|
|
|
if err := os.MkdirAll(d, 0700); err != nil {
|
2018-03-23 12:10:14 +01:00
|
|
|
return err
|
2018-03-19 13:20:42 +01:00
|
|
|
}
|
2018-03-08 20:46:31 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-23 12:10:14 +01:00
|
|
|
return nil
|
2018-03-08 20:46:31 +01:00
|
|
|
}
|
|
|
|
|
2018-06-25 17:49:17 +02:00
|
|
|
// InitDefaultConfig reads the config from common locations or creates it if
|
|
|
|
// missing.
|
|
|
|
func InitDefaultConfig(home string) (*config.Config, error) {
|
|
|
|
var (
|
|
|
|
err error
|
|
|
|
c *config.Config
|
|
|
|
configPath = utils.ConfigPath(home)
|
|
|
|
)
|
|
|
|
|
|
|
|
c, err = config.ReadConfig(configPath)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
log.Debugf("writing default config to %s", configPath)
|
|
|
|
var data []byte
|
|
|
|
data, err = bindata.Asset("data/default-config.json")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
err = ioutil.WriteFile(
|
|
|
|
configPath,
|
|
|
|
data,
|
|
|
|
0644,
|
|
|
|
)
|
2018-02-12 16:45:13 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-25 17:49:17 +02:00
|
|
|
return InitDefaultConfig(home)
|
2018-02-12 16:45:13 +01:00
|
|
|
}
|
2018-06-25 17:49:17 +02:00
|
|
|
return nil, err
|
2018-02-12 16:45:13 +01:00
|
|
|
}
|
2018-03-08 11:25:40 +01:00
|
|
|
|
2018-06-25 17:49:17 +02:00
|
|
|
return c, nil
|
2018-02-07 19:02:18 +01:00
|
|
|
}
|