From a6b95f50c96deebaba8f11881af865da3bcc7c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Filast=C3=B2?= Date: Mon, 25 Jun 2018 17:14:29 +0200 Subject: [PATCH] Move onboard into a separate package --- internal/cli/onboard/onboard.go | 136 +----------------------------- internal/onboard/onboard.go | 141 ++++++++++++++++++++++++++++++++ ooni.go | 20 +---- 3 files changed, 145 insertions(+), 152 deletions(-) create mode 100644 internal/onboard/onboard.go diff --git a/internal/cli/onboard/onboard.go b/internal/cli/onboard/onboard.go index aea1000..7970976 100644 --- a/internal/cli/onboard/onboard.go +++ b/internal/cli/onboard/onboard.go @@ -1,14 +1,9 @@ package onboard import ( - "fmt" - "github.com/alecthomas/kingpin" - "github.com/apex/log" "github.com/ooni/probe-cli/internal/cli/root" - "github.com/ooni/probe-cli/internal/colors" - "github.com/ooni/probe-cli/internal/output" - "gopkg.in/AlecAivazis/survey.v1" + "github.com/ooni/probe-cli/internal/onboard" ) func init() { @@ -20,133 +15,6 @@ func init() { return err } - output.SectionTitle("What is OONI Probe?") - - fmt.Println() - output.Paragraph("Your tool for detecting internet censorship!") - fmt.Println() - output.Paragraph("OONI Probe checks whether your provider blocks access to sites and services. Run OONI Probe to collect evidence of internet censorship and to measure your network performance.") - fmt.Println() - output.PressEnterToContinue("Press 'Enter' to continue...") - - output.SectionTitle("Heads Up") - fmt.Println() - output.Bullet("Anyone monitoring your internet activity (such as your government or ISP) may be able to see that you are running OONI Probe.") - fmt.Println() - output.Bullet("The network data you will collect will automatically be published (unless you opt-out in the settings).") - fmt.Println() - output.Bullet("You may test objectionable sites.") - fmt.Println() - output.Bullet("Read the documentation to learn more.") - fmt.Println() - output.PressEnterToContinue("Press 'Enter' to continue...") - - output.SectionTitle("Pop Quiz!") - output.Paragraph("") - answer := "" - quiz1 := &survey.Select{ - Message: "Anyone monitoring my internet activity may be able to see that I am running OONI Probe.", - Options: []string{"true", "false"}, - Default: "true", - } - survey.AskOne(quiz1, &answer, nil) - if answer != "true" { - output.Paragraph(colors.Red("Actually...")) - output.Paragraph("OONI Probe is not a privacy tool. Therefore, anyone monitoring your internet activity may be able to see which software you are running.") - } else { - output.Paragraph(colors.Blue("Good job!")) - } - answer = "" - quiz2 := &survey.Select{ - Message: "The network data I will collect will automatically be published (unless I opt-out in the settings).", - Options: []string{"true", "false"}, - Default: "true", - } - survey.AskOne(quiz2, &answer, nil) - if answer != "true" { - output.Paragraph(colors.Red("Actually...")) - output.Paragraph("The network data you will collect will automatically be published to increase transparency of internet censorship (unless you opt-out in the settings).") - } else { - output.Paragraph(colors.Blue("Well done!")) - } - - changeDefaults := false - prompt := &survey.Confirm{ - Message: "Do you want to change the default settings?", - Default: false, - } - survey.AskOne(prompt, &changeDefaults, nil) - - settings := struct { - IncludeIP bool - IncludeNetwork bool - IncludeCountry bool - UploadResults bool - SendCrashReports bool - }{} - settings.IncludeIP = false - settings.IncludeNetwork = true - settings.IncludeCountry = true - settings.UploadResults = true - settings.SendCrashReports = true - - if changeDefaults == true { - var qs = []*survey.Question{ - { - Name: "IncludeIP", - Prompt: &survey.Confirm{Message: "Should we include your IP?"}, - }, - { - Name: "IncludeNetwork", - Prompt: &survey.Confirm{ - Message: "Can we include your network name?", - Default: true, - }, - }, - { - Name: "IncludeCountry", - Prompt: &survey.Confirm{ - Message: "Can we include your country name?", - Default: true, - }, - }, - { - Name: "UploadResults", - Prompt: &survey.Confirm{ - Message: "Can we upload your results?", - Default: true, - }, - }, - { - Name: "SendCrashReports", - Prompt: &survey.Confirm{ - Message: "Can we send crash reports to OONI?", - Default: true, - }, - }, - } - - err := survey.Ask(qs, &settings) - if err != nil { - log.WithError(err).Error("there was an error in parsing your responses") - return err - } - } - - ctx.Config.Lock() - ctx.Config.InformedConsent = true - ctx.Config.Advanced.IncludeCountry = settings.IncludeCountry - ctx.Config.Sharing.IncludeIP = settings.IncludeIP - ctx.Config.Sharing.IncludeASN = settings.IncludeNetwork - ctx.Config.Sharing.UploadResults = settings.UploadResults - ctx.Config.Sharing.SendCrashReports = settings.SendCrashReports - ctx.Config.Unlock() - - if err := ctx.Config.Write(); err != nil { - log.WithError(err).Error("failed to write config file") - return err - } - - return nil + return onboard.Onboarding(ctx.Config) }) } diff --git a/internal/onboard/onboard.go b/internal/onboard/onboard.go new file mode 100644 index 0000000..b112f09 --- /dev/null +++ b/internal/onboard/onboard.go @@ -0,0 +1,141 @@ +package onboard + +import ( + "fmt" + + "github.com/apex/log" + "github.com/ooni/probe-cli/config" + "github.com/ooni/probe-cli/internal/colors" + "github.com/ooni/probe-cli/internal/output" + survey "gopkg.in/AlecAivazis/survey.v1" +) + +func Onboarding(config *config.Config) error { + output.SectionTitle("What is OONI Probe?") + + fmt.Println() + output.Paragraph("Your tool for detecting internet censorship!") + fmt.Println() + output.Paragraph("OONI Probe checks whether your provider blocks access to sites and services. Run OONI Probe to collect evidence of internet censorship and to measure your network performance.") + fmt.Println() + output.PressEnterToContinue("Press 'Enter' to continue...") + + output.SectionTitle("Heads Up") + fmt.Println() + output.Bullet("Anyone monitoring your internet activity (such as your government or ISP) may be able to see that you are running OONI Probe.") + fmt.Println() + output.Bullet("The network data you will collect will automatically be published (unless you opt-out in the settings).") + fmt.Println() + output.Bullet("You may test objectionable sites.") + fmt.Println() + output.Bullet("Read the documentation to learn more.") + fmt.Println() + output.PressEnterToContinue("Press 'Enter' to continue...") + + output.SectionTitle("Pop Quiz!") + output.Paragraph("") + answer := "" + quiz1 := &survey.Select{ + Message: "Anyone monitoring my internet activity may be able to see that I am running OONI Probe.", + Options: []string{"true", "false"}, + Default: "true", + } + survey.AskOne(quiz1, &answer, nil) + if answer != "true" { + output.Paragraph(colors.Red("Actually...")) + output.Paragraph("OONI Probe is not a privacy tool. Therefore, anyone monitoring your internet activity may be able to see which software you are running.") + } else { + output.Paragraph(colors.Blue("Good job!")) + } + answer = "" + quiz2 := &survey.Select{ + Message: "The network data I will collect will automatically be published (unless I opt-out in the settings).", + Options: []string{"true", "false"}, + Default: "true", + } + survey.AskOne(quiz2, &answer, nil) + if answer != "true" { + output.Paragraph(colors.Red("Actually...")) + output.Paragraph("The network data you will collect will automatically be published to increase transparency of internet censorship (unless you opt-out in the settings).") + } else { + output.Paragraph(colors.Blue("Well done!")) + } + + changeDefaults := false + prompt := &survey.Confirm{ + Message: "Do you want to change the default settings?", + Default: false, + } + survey.AskOne(prompt, &changeDefaults, nil) + + settings := struct { + IncludeIP bool + IncludeNetwork bool + IncludeCountry bool + UploadResults bool + SendCrashReports bool + }{} + settings.IncludeIP = false + settings.IncludeNetwork = true + settings.IncludeCountry = true + settings.UploadResults = true + settings.SendCrashReports = true + + if changeDefaults == true { + var qs = []*survey.Question{ + { + Name: "IncludeIP", + Prompt: &survey.Confirm{Message: "Should we include your IP?"}, + }, + { + Name: "IncludeNetwork", + Prompt: &survey.Confirm{ + Message: "Can we include your network name?", + Default: true, + }, + }, + { + Name: "IncludeCountry", + Prompt: &survey.Confirm{ + Message: "Can we include your country name?", + Default: true, + }, + }, + { + Name: "UploadResults", + Prompt: &survey.Confirm{ + Message: "Can we upload your results?", + Default: true, + }, + }, + { + Name: "SendCrashReports", + Prompt: &survey.Confirm{ + Message: "Can we send crash reports to OONI?", + Default: true, + }, + }, + } + + err := survey.Ask(qs, &settings) + if err != nil { + log.WithError(err).Error("there was an error in parsing your responses") + return err + } + } + + config.Lock() + config.InformedConsent = true + config.Advanced.IncludeCountry = settings.IncludeCountry + config.Sharing.IncludeIP = settings.IncludeIP + config.Sharing.IncludeASN = settings.IncludeNetwork + config.Sharing.UploadResults = settings.UploadResults + config.Sharing.SendCrashReports = settings.SendCrashReports + config.Unlock() + + if err := config.Write(); err != nil { + log.WithError(err).Error("failed to write config file") + return err + } + return nil +} diff --git a/ooni.go b/ooni.go index 399accc..df2c7a9 100644 --- a/ooni.go +++ b/ooni.go @@ -11,27 +11,11 @@ import ( "github.com/ooni/probe-cli/config" "github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/legacy" + "github.com/ooni/probe-cli/internal/onboard" "github.com/ooni/probe-cli/utils" "github.com/pkg/errors" ) -// Onboarding process -func Onboarding(c *config.Config) error { - log.Info("Onboarding starting") - - // To prevent races we always must acquire the config file lock before - // changing it. - c.Lock() - c.InformedConsent = true - c.Unlock() - - if err := c.Write(); err != nil { - log.Warnf("Failed to save informed consent: %v", err) - return err - } - return nil -} - // Context for OONI Probe type Context struct { Config *config.Config @@ -74,7 +58,7 @@ func (c *Context) LocationLookup() error { // config option is set to false func (c *Context) MaybeOnboarding() error { if c.Config.InformedConsent == false { - if err := Onboarding(c.Config); err != nil { + if err := onboard.Onboarding(c.Config); err != nil { return errors.Wrap(err, "onboarding") } }