Merge pull request #83 from ooni/refactor-consolidate

Refactoring to reduce package count
This commit is contained in:
Arturo Filastò 2019-12-04 11:00:15 +02:00 committed by GitHub
commit 68d00c8e4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
26 changed files with 302 additions and 284 deletions

1
.gitignore vendored
View File

@ -1,4 +1,5 @@
/dist /dist
/ooni.cov /ooni.cov
/coverage.txt
*.njson *.njson
.DS_Store .DS_Store

View File

@ -5,6 +5,10 @@ services:
- docker - docker
env: env:
- OS_NAME: linux - OS_NAME: linux
before_script:
- go get golang.org/x/tools/cmd/cover
- go get github.com/mattn/goveralls
script: script:
- ./build.sh _travis-${TRAVIS_OS_NAME} - ./build.sh _travis-${TRAVIS_OS_NAME}
- $GOPATH/bin/goveralls -coverprofile=coverage.cov -service=travis-ci
- ./scripts/travis_test.sh - ./scripts/travis_test.sh

View File

@ -50,8 +50,9 @@ elif [ "$1" = "release" ]; then
elif [ "$1" = "_travis-linux" ]; then elif [ "$1" = "_travis-linux" ]; then
set -x set -x
$0 linux $0 linux
# TODO -race does not work on alpine. See: https://travis-ci.org/ooni/probe-cli/builds/619631256#L962
docker run -v `pwd`:/oonibuild -w /oonibuild -t oonibuild \ docker run -v `pwd`:/oonibuild -w /oonibuild -t oonibuild \
go test -v -coverprofile=ooni.cov ./... go test -v -coverprofile=coverage.cov -coverpkg=./... ./...
elif [ "$1" = "_travis-osx" ]; then elif [ "$1" = "_travis-osx" ]; then
set -x set -x
@ -60,7 +61,7 @@ elif [ "$1" = "_travis-osx" ]; then
brew upgrade brew upgrade
brew install measurement-kit brew install measurement-kit
$0 macos $0 macos
go test -v -coverprofile=ooni.cov ./... go test -v -race -coverprofile=coverage.cov -coverpkg=./... ./...
elif [ "$1" = "help" ]; then elif [ "$1" = "help" ]; then
echo "Usage: $0 linux | macos | release | windows" echo "Usage: $0 linux | macos | release | windows"

View File

@ -1,14 +1,164 @@
package onboard package onboard
import ( import (
"errors" "fmt"
"github.com/alecthomas/kingpin" "github.com/alecthomas/kingpin"
"github.com/apex/log" "github.com/apex/log"
"github.com/fatih/color"
ooni "github.com/ooni/probe-cli"
"github.com/ooni/probe-cli/config"
"github.com/ooni/probe-cli/internal/cli/root" "github.com/ooni/probe-cli/internal/cli/root"
"github.com/ooni/probe-cli/internal/onboard" "github.com/ooni/probe-cli/internal/output"
"github.com/pkg/errors"
"gopkg.in/AlecAivazis/survey.v1"
) )
// Onboarding start the interactive onboarding procedure
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(color.RedString("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(color.BlueString("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(color.RedString("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(color.BlueString("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.Sharing.IncludeCountry = settings.IncludeCountry
config.Advanced.SendCrashReports = settings.SendCrashReports
config.Sharing.IncludeIP = settings.IncludeIP
config.Sharing.IncludeASN = settings.IncludeNetwork
config.Sharing.UploadResults = settings.UploadResults
config.Unlock()
if err := config.Write(); err != nil {
log.WithError(err).Error("failed to write config file")
return err
}
return nil
}
// MaybeOnboarding will run the onboarding process only if the informed consent
// config option is set to false
func MaybeOnboarding(c *ooni.Context) error {
if c.Config.InformedConsent == false {
if c.IsBatch == true {
return errors.New("cannot run onboarding in batch mode")
}
if err := Onboarding(c.Config); err != nil {
return errors.Wrap(err, "onboarding")
}
}
return nil
}
func init() { func init() {
cmd := root.Command("onboard", "Starts the onboarding process") cmd := root.Command("onboard", "Starts the onboarding process")
@ -35,6 +185,6 @@ func init() {
return errors.New("cannot do onboarding in batch mode") return errors.New("cannot do onboarding in batch mode")
} }
return onboard.Onboarding(ctx.Config) return Onboarding(ctx.Config)
}) })
} }

View File

@ -7,14 +7,14 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/fatih/color" "github.com/fatih/color"
ooni "github.com/ooni/probe-cli" ooni "github.com/ooni/probe-cli"
"github.com/ooni/probe-cli/internal/cli/onboard"
"github.com/ooni/probe-cli/internal/cli/root" "github.com/ooni/probe-cli/internal/cli/root"
"github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/nettests" "github.com/ooni/probe-cli/nettests"
"github.com/ooni/probe-cli/nettests/groups"
) )
func runNettestGroup(tg string, ctx *ooni.Context, network *database.Network) error { func runNettestGroup(tg string, ctx *ooni.Context, network *database.Network) error {
group, ok := groups.NettestGroups[tg] group, ok := nettests.NettestGroups[tg]
if !ok { if !ok {
log.Errorf("No test group named %s", tg) log.Errorf("No test group named %s", tg)
return errors.New("invalid test group name") return errors.New("invalid test group name")
@ -50,7 +50,7 @@ func init() {
var ctx *ooni.Context var ctx *ooni.Context
var network *database.Network var network *database.Network
for name := range groups.NettestGroups { for name := range nettests.NettestGroups {
nettestGroupNamesBlue = append(nettestGroupNamesBlue, color.BlueString(name)) nettestGroupNamesBlue = append(nettestGroupNamesBlue, color.BlueString(name))
} }
@ -66,7 +66,7 @@ func init() {
return err return err
} }
if err = ctx.MaybeOnboarding(); err != nil { if err = onboard.MaybeOnboarding(ctx); err != nil {
log.WithError(err).Error("failed to perform onboarding") log.WithError(err).Error("failed to perform onboarding")
return err return err
} }
@ -129,7 +129,7 @@ func init() {
allCmd := cmd.Command("all", "").Default() allCmd := cmd.Command("all", "").Default()
allCmd.Action(func(_ *kingpin.ParseContext) error { allCmd.Action(func(_ *kingpin.ParseContext) error {
log.Infof("Running %s tests", color.BlueString("all")) log.Infof("Running %s tests", color.BlueString("all"))
for tg := range groups.NettestGroups { for tg := range nettests.NettestGroups {
if err := runNettestGroup(tg, ctx, network); err != nil { if err := runNettestGroup(tg, ctx, network); err != nil {
log.WithError(err).Errorf("failed to run %s", tg) log.WithError(err).Errorf("failed to run %s", tg)
} }

View File

@ -6,7 +6,7 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"github.com/ooni/probe-cli/internal/shutil" "github.com/ooni/probe-cli/utils/shutil"
"github.com/pkg/errors" "github.com/pkg/errors"
"upper.io/db.v3/lib/sqlbuilder" "upper.io/db.v3/lib/sqlbuilder"
) )

View File

@ -11,7 +11,7 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/fatih/color" "github.com/fatih/color"
colorable "github.com/mattn/go-colorable" colorable "github.com/mattn/go-colorable"
"github.com/ooni/probe-cli/internal/util" "github.com/ooni/probe-cli/utils"
) )
// Default handler outputting to stderr. // Default handler outputting to stderr.
@ -67,7 +67,7 @@ func logSectionTitle(w io.Writer, f log.Fields) error {
title := f.Get("title").(string) title := f.Get("title").(string)
fmt.Fprintf(w, "┏"+strings.Repeat("━", colWidth+2)+"┓\n") fmt.Fprintf(w, "┏"+strings.Repeat("━", colWidth+2)+"┓\n")
fmt.Fprintf(w, "┃ %s ┃\n", util.RightPad(title, colWidth)) fmt.Fprintf(w, "┃ %s ┃\n", utils.RightPad(title, colWidth))
fmt.Fprintf(w, "┗"+strings.Repeat("━", colWidth+2)+"┛\n") fmt.Fprintf(w, "┗"+strings.Repeat("━", colWidth+2)+"┛\n")
return nil return nil
} }
@ -84,7 +84,7 @@ func logTable(w io.Writer, f log.Fields) error {
continue continue
} }
line := fmt.Sprintf("%s: %s", color.Sprint(name), f.Get(name)) line := fmt.Sprintf("%s: %s", color.Sprint(name), f.Get(name))
lineLength := util.EscapeAwareRuneCountInString(line) lineLength := utils.EscapeAwareRuneCountInString(line)
lines = append(lines, line) lines = append(lines, line)
if colWidth < lineLength { if colWidth < lineLength {
colWidth = lineLength colWidth = lineLength
@ -94,7 +94,7 @@ func logTable(w io.Writer, f log.Fields) error {
fmt.Fprintf(w, "┏"+strings.Repeat("━", colWidth+2)+"┓\n") fmt.Fprintf(w, "┏"+strings.Repeat("━", colWidth+2)+"┓\n")
for _, line := range lines { for _, line := range lines {
fmt.Fprintf(w, "┃ %s ┃\n", fmt.Fprintf(w, "┃ %s ┃\n",
util.RightPad(line, colWidth), utils.RightPad(line, colWidth),
) )
} }
fmt.Fprintf(w, "┗"+strings.Repeat("━", colWidth+2)+"┛\n") fmt.Fprintf(w, "┗"+strings.Repeat("━", colWidth+2)+"┛\n")

View File

@ -9,7 +9,7 @@ import (
"time" "time"
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/internal/util" "github.com/ooni/probe-cli/utils"
) )
func statusIcon(ok bool) string { func statusIcon(ok bool) string {
@ -35,7 +35,7 @@ func logTestKeys(w io.Writer, testKeys string) error {
} }
for _, line := range testKeysLines { for _, line := range testKeysLines {
fmt.Fprintf(w, fmt.Sprintf("│ %s │\n", fmt.Fprintf(w, fmt.Sprintf("│ %s │\n",
util.RightPad(line, colWidth*2))) utils.RightPad(line, colWidth*2)))
} }
return nil return nil
} }
@ -71,22 +71,22 @@ func logMeasurementItem(w io.Writer, f log.Fields) error {
failureStr := fmt.Sprintf("success: %s", statusIcon(!isFailed)) failureStr := fmt.Sprintf("success: %s", statusIcon(!isFailed))
fmt.Fprintf(w, fmt.Sprintf("│ %s │\n", fmt.Fprintf(w, fmt.Sprintf("│ %s │\n",
util.RightPad( utils.RightPad(
fmt.Sprintf("#%d", rID), colWidth*2))) fmt.Sprintf("#%d", rID), colWidth*2)))
if url != "" { if url != "" {
fmt.Fprintf(w, fmt.Sprintf("│ %s │\n", fmt.Fprintf(w, fmt.Sprintf("│ %s │\n",
util.RightPad( utils.RightPad(
fmt.Sprintf("%s (%s)", url, urlCategoryCode), colWidth*2))) fmt.Sprintf("%s (%s)", url, urlCategoryCode), colWidth*2)))
} }
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n", fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
util.RightPad(testName, colWidth), utils.RightPad(testName, colWidth),
util.RightPad(anomalyStr, colWidth))) utils.RightPad(anomalyStr, colWidth)))
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n", fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
util.RightPad(failureStr, colWidth), utils.RightPad(failureStr, colWidth),
util.RightPad(uploadStr, colWidth))) utils.RightPad(uploadStr, colWidth)))
if testKeys != "" { if testKeys != "" {
if err := logTestKeys(w, testKeys); err != nil { if err := logTestKeys(w, testKeys); err != nil {
@ -116,15 +116,15 @@ func logMeasurementSummary(w io.Writer, f log.Fields) error {
networkName := f.Get("network_name").(string) networkName := f.Get("network_name").(string)
fmt.Fprintf(w, " │ %s │\n", fmt.Fprintf(w, " │ %s │\n",
util.RightPad(startTime.Format(time.RFC822), (colWidth+3)*3), utils.RightPad(startTime.Format(time.RFC822), (colWidth+3)*3),
) )
fmt.Fprintf(w, " │ %s │\n", fmt.Fprintf(w, " │ %s │\n",
util.RightPad(fmt.Sprintf("AS%d, %s (%s)", asn, networkName, countryCode), (colWidth+3)*3), utils.RightPad(fmt.Sprintf("AS%d, %s (%s)", asn, networkName, countryCode), (colWidth+3)*3),
) )
fmt.Fprintf(w, " │ %s %s %s │\n", fmt.Fprintf(w, " │ %s %s %s │\n",
util.RightPad(fmt.Sprintf("%.2fs", totalRuntime), colWidth), utils.RightPad(fmt.Sprintf("%.2fs", totalRuntime), colWidth),
util.RightPad(fmt.Sprintf("%d/%d anmls", anomalyCount, totalCount), colWidth), utils.RightPad(fmt.Sprintf("%d/%d anmls", anomalyCount, totalCount), colWidth),
util.RightPad(fmt.Sprintf("⬆ %s ⬇ %s", formatSize(dataUp), formatSize(dataDown)), colWidth+4)) utils.RightPad(fmt.Sprintf("⬆ %s ⬇ %s", formatSize(dataUp), formatSize(dataDown)), colWidth+4))
fmt.Fprintf(w, " └────────────────────────────────────────────────┘\n") fmt.Fprintf(w, " └────────────────────────────────────────────────┘\n")
return nil return nil

View File

@ -9,7 +9,7 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/internal/util" "github.com/ooni/probe-cli/utils"
) )
func formatSpeed(speed float64) string { func formatSpeed(speed float64) string {
@ -95,7 +95,7 @@ func logResultItem(w io.Writer, f log.Fields) error {
fmt.Fprintf(w, "┢"+strings.Repeat("━", colWidth*2+2)+"┪\n") fmt.Fprintf(w, "┢"+strings.Repeat("━", colWidth*2+2)+"┪\n")
} }
firstRow := util.RightPad(fmt.Sprintf("#%d - %s", rID, startTime.Format(time.RFC822)), colWidth*2) firstRow := utils.RightPad(fmt.Sprintf("#%d - %s", rID, startTime.Format(time.RFC822)), colWidth*2)
fmt.Fprintf(w, "┃ "+firstRow+" ┃\n") fmt.Fprintf(w, "┃ "+firstRow+" ┃\n")
fmt.Fprintf(w, "┡"+strings.Repeat("━", colWidth*2+2)+"┩\n") fmt.Fprintf(w, "┡"+strings.Repeat("━", colWidth*2+2)+"┩\n")
@ -105,14 +105,14 @@ func logResultItem(w io.Writer, f log.Fields) error {
f.Get("test_keys").(string)) f.Get("test_keys").(string))
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n", fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
util.RightPad(name, colWidth), utils.RightPad(name, colWidth),
util.RightPad(summary[0], colWidth))) utils.RightPad(summary[0], colWidth)))
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n", fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
util.RightPad(networkName, colWidth), utils.RightPad(networkName, colWidth),
util.RightPad(summary[1], colWidth))) utils.RightPad(summary[1], colWidth)))
fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n", fmt.Fprintf(w, fmt.Sprintf("│ %s %s│\n",
util.RightPad(asn, colWidth), utils.RightPad(asn, colWidth),
util.RightPad(summary[2], colWidth))) utils.RightPad(summary[2], colWidth)))
if index == totalCount-1 { if index == totalCount-1 {
if isDone == true { if isDone == true {
@ -139,9 +139,9 @@ func logResultSummary(w io.Writer, f log.Fields) error {
} }
// └┬──────────────┬──────────────┬──────────────┬ // └┬──────────────┬──────────────┬──────────────┬
fmt.Fprintf(w, " │ %s │ %s │ %s │\n", fmt.Fprintf(w, " │ %s │ %s │ %s │\n",
util.RightPad(fmt.Sprintf("%d tests", tests), 12), utils.RightPad(fmt.Sprintf("%d tests", tests), 12),
util.RightPad(fmt.Sprintf("%d nets", networks), 12), utils.RightPad(fmt.Sprintf("%d nets", networks), 12),
util.RightPad(fmt.Sprintf("⬆ %s ⬇ %s", formatSize(dataUp), formatSize(dataDown)), 16)) utils.RightPad(fmt.Sprintf("⬆ %s ⬇ %s", formatSize(dataUp), formatSize(dataDown)), 16))
fmt.Fprintf(w, " └──────────────┴──────────────┴──────────────────┘\n") fmt.Fprintf(w, " └──────────────┴──────────────┴──────────────────┘\n")
return nil return nil

View File

@ -1,141 +0,0 @@
package onboard
import (
"fmt"
"github.com/apex/log"
"github.com/fatih/color"
"github.com/ooni/probe-cli/config"
"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(color.RedString("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(color.BlueString("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(color.RedString("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(color.BlueString("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.Sharing.IncludeCountry = settings.IncludeCountry
config.Advanced.SendCrashReports = settings.SendCrashReports
config.Sharing.IncludeIP = settings.IncludeIP
config.Sharing.IncludeASN = settings.IncludeNetwork
config.Sharing.UploadResults = settings.UploadResults
config.Unlock()
if err := config.Write(); err != nil {
log.WithError(err).Error("failed to write config file")
return err
}
return nil
}

View File

@ -8,7 +8,7 @@ import (
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/internal/util" "github.com/ooni/probe-cli/utils"
) )
// MeasurementJSON prints the JSON of a measurement // MeasurementJSON prints the JSON of a measurement
@ -154,12 +154,12 @@ func SectionTitle(text string) {
func Paragraph(text string) { func Paragraph(text string) {
const width = 80 const width = 80
fmt.Println(util.WrapString(text, width)) fmt.Println(utils.WrapString(text, width))
} }
func Bullet(text string) { func Bullet(text string) {
const width = 80 const width = 80
fmt.Printf("• %s\n", util.WrapString(text, width)) fmt.Printf("• %s\n", utils.WrapString(text, width))
} }
func PressEnterToContinue(text string) { func PressEnterToContinue(text string) {

View File

@ -1,9 +1,7 @@
package performance package nettests
import ( import (
"github.com/pkg/errors" "github.com/pkg/errors"
"github.com/ooni/probe-cli/nettests"
) )
// Dash test implementation // Dash test implementation
@ -11,7 +9,7 @@ type Dash struct {
} }
// Run starts the test // Run starts the test
func (d Dash) Run(ctl *nettests.Controller) error { func (d Dash) Run(ctl *Controller) error {
builder, err := ctl.Ctx.Session.NewExperimentBuilder("dash") builder, err := ctl.Ctx.Session.NewExperimentBuilder("dash")
if err != nil { if err != nil {
return err return err

View File

@ -1,15 +1,11 @@
package im package nettests
import (
"github.com/ooni/probe-cli/nettests"
)
// FacebookMessenger test implementation // FacebookMessenger test implementation
type FacebookMessenger struct { type FacebookMessenger struct {
} }
// Run starts the test // Run starts the test
func (h FacebookMessenger) Run(ctl *nettests.Controller) error { func (h FacebookMessenger) Run(ctl *Controller) error {
builder, err := ctl.Ctx.Session.NewExperimentBuilder( builder, err := ctl.Ctx.Session.NewExperimentBuilder(
"facebook_messenger", "facebook_messenger",
) )

39
nettests/groups.go Normal file
View File

@ -0,0 +1,39 @@
package nettests
// NettestGroup base structure
type NettestGroup struct {
Label string
Nettests []Nettest
}
// NettestGroups that can be run by the user
var NettestGroups = map[string]NettestGroup{
"websites": NettestGroup{
Label: "Websites",
Nettests: []Nettest{
WebConnectivity{},
},
},
"performance": NettestGroup{
Label: "Performance",
Nettests: []Nettest{
Dash{},
NDT{},
},
},
"middlebox": NettestGroup{
Label: "Middleboxes",
Nettests: []Nettest{
HTTPInvalidRequestLine{},
HTTPHeaderFieldManipulation{},
},
},
"im": NettestGroup{
Label: "Instant Messaging",
Nettests: []Nettest{
FacebookMessenger{},
Telegram{},
WhatsApp{},
},
},
}

View File

@ -1,47 +0,0 @@
package groups
import (
"github.com/ooni/probe-cli/nettests"
"github.com/ooni/probe-cli/nettests/im"
"github.com/ooni/probe-cli/nettests/middlebox"
"github.com/ooni/probe-cli/nettests/performance"
"github.com/ooni/probe-cli/nettests/websites"
)
// NettestGroup base structure
type NettestGroup struct {
Label string
Nettests []nettests.Nettest
}
// NettestGroups that can be run by the user
var NettestGroups = map[string]NettestGroup{
"websites": NettestGroup{
Label: "Websites",
Nettests: []nettests.Nettest{
websites.WebConnectivity{},
},
},
"performance": NettestGroup{
Label: "Performance",
Nettests: []nettests.Nettest{
performance.Dash{},
performance.NDT{},
},
},
"middlebox": NettestGroup{
Label: "Middleboxes",
Nettests: []nettests.Nettest{
middlebox.HTTPInvalidRequestLine{},
middlebox.HTTPHeaderFieldManipulation{},
},
},
"im": NettestGroup{
Label: "Instant Messaging",
Nettests: []nettests.Nettest{
im.FacebookMessenger{},
im.Telegram{},
im.WhatsApp{},
},
},
}

View File

@ -1,9 +1,7 @@
package middlebox package nettests
import ( import (
"errors" "errors"
"github.com/ooni/probe-cli/nettests"
) )
// HTTPHeaderFieldManipulation test implementation // HTTPHeaderFieldManipulation test implementation
@ -11,7 +9,7 @@ type HTTPHeaderFieldManipulation struct {
} }
// Run starts the test // Run starts the test
func (h HTTPHeaderFieldManipulation) Run(ctl *nettests.Controller) error { func (h HTTPHeaderFieldManipulation) Run(ctl *Controller) error {
builder, err := ctl.Ctx.Session.NewExperimentBuilder( builder, err := ctl.Ctx.Session.NewExperimentBuilder(
"http_header_field_manipulation", "http_header_field_manipulation",
) )

View File

@ -1,9 +1,7 @@
package middlebox package nettests
import ( import (
"errors" "errors"
"github.com/ooni/probe-cli/nettests"
) )
// HTTPInvalidRequestLine test implementation // HTTPInvalidRequestLine test implementation
@ -11,7 +9,7 @@ type HTTPInvalidRequestLine struct {
} }
// Run starts the test // Run starts the test
func (h HTTPInvalidRequestLine) Run(ctl *nettests.Controller) error { func (h HTTPInvalidRequestLine) Run(ctl *Controller) error {
builder, err := ctl.Ctx.Session.NewExperimentBuilder( builder, err := ctl.Ctx.Session.NewExperimentBuilder(
"http_invalid_request_line", "http_invalid_request_line",
) )

View File

@ -1,7 +1,6 @@
package performance package nettests
import ( import (
"github.com/ooni/probe-cli/nettests"
"github.com/pkg/errors" "github.com/pkg/errors"
) )
@ -10,7 +9,7 @@ type NDT struct {
} }
// Run starts the test // Run starts the test
func (n NDT) Run(ctl *nettests.Controller) error { func (n NDT) Run(ctl *Controller) error {
builder, err := ctl.Ctx.Session.NewExperimentBuilder("ndt") builder, err := ctl.Ctx.Session.NewExperimentBuilder("ndt")
if err != nil { if err != nil {
return err return err

46
nettests/nettests_test.go Normal file
View File

@ -0,0 +1,46 @@
package nettests
import (
"io/ioutil"
"path"
"testing"
ooni "github.com/ooni/probe-cli"
"github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/utils/shutil"
)
func newTestingContext(t *testing.T) *ooni.Context {
homePath, err := ioutil.TempDir("", "ooniprobetests")
if err != nil {
t.Fatal(err)
}
configPath := path.Join(homePath, "config.json")
testingConfig := path.Join("..", "testdata", "testing-config.json")
shutil.Copy(testingConfig, configPath, false)
ctx := ooni.NewContext(configPath, homePath)
err = ctx.Init()
if err != nil {
t.Fatal(err)
}
return ctx
}
func TestCreateContext(t *testing.T) {
newTestingContext(t)
}
func TestRun(t *testing.T) {
ctx := newTestingContext(t)
network, err := database.CreateNetwork(ctx.DB, ctx.Session)
if err != nil {
t.Fatal(err)
}
res, err := database.CreateResult(ctx.DB, ctx.Home, "middlebox", network.ID)
if err != nil {
t.Fatal(err)
}
nt := HTTPInvalidRequestLine{}
ctl := NewController(nt, ctx, res)
nt.Run(ctl)
}

View File

@ -1,15 +1,11 @@
package im package nettests
import (
"github.com/ooni/probe-cli/nettests"
)
// Telegram test implementation // Telegram test implementation
type Telegram struct { type Telegram struct {
} }
// Run starts the test // Run starts the test
func (h Telegram) Run(ctl *nettests.Controller) error { func (h Telegram) Run(ctl *Controller) error {
builder, err := ctl.Ctx.Session.NewExperimentBuilder( builder, err := ctl.Ctx.Session.NewExperimentBuilder(
"telegram", "telegram",
) )

View File

@ -1,12 +1,11 @@
package websites package nettests
import ( import (
"github.com/apex/log" "github.com/apex/log"
"github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/nettests"
) )
func lookupURLs(ctl *nettests.Controller, limit int64) ([]string, map[int64]int64, error) { func lookupURLs(ctl *Controller, limit int64) ([]string, map[int64]int64, error) {
var urls []string var urls []string
urlIDMap := make(map[int64]int64) urlIDMap := make(map[int64]int64)
config := ctl.Ctx.Session.NewTestListsConfig() config := ctl.Ctx.Session.NewTestListsConfig()
@ -37,7 +36,7 @@ type WebConnectivity struct {
} }
// Run starts the test // Run starts the test
func (n WebConnectivity) Run(ctl *nettests.Controller) error { func (n WebConnectivity) Run(ctl *Controller) error {
urls, urlIDMap, err := lookupURLs(ctl, ctl.Ctx.Config.Nettests.WebsitesURLLimit) urls, urlIDMap, err := lookupURLs(ctl, ctl.Ctx.Config.Nettests.WebsitesURLLimit)
if err != nil { if err != nil {
return err return err

View File

@ -1,15 +1,11 @@
package im package nettests
import (
"github.com/ooni/probe-cli/nettests"
)
// WhatsApp test implementation // WhatsApp test implementation
type WhatsApp struct { type WhatsApp struct {
} }
// Run starts the test // Run starts the test
func (h WhatsApp) Run(ctl *nettests.Controller) error { func (h WhatsApp) Run(ctl *Controller) error {
builder, err := ctl.Ctx.Session.NewExperimentBuilder( builder, err := ctl.Ctx.Session.NewExperimentBuilder(
"whatsapp", "whatsapp",
) )

15
ooni.go
View File

@ -10,7 +10,6 @@ import (
"github.com/ooni/probe-cli/internal/database" "github.com/ooni/probe-cli/internal/database"
"github.com/ooni/probe-cli/internal/enginex" "github.com/ooni/probe-cli/internal/enginex"
"github.com/ooni/probe-cli/internal/legacy" "github.com/ooni/probe-cli/internal/legacy"
"github.com/ooni/probe-cli/internal/onboard"
"github.com/ooni/probe-cli/utils" "github.com/ooni/probe-cli/utils"
"github.com/ooni/probe-cli/version" "github.com/ooni/probe-cli/version"
engine "github.com/ooni/probe-engine" engine "github.com/ooni/probe-engine"
@ -37,20 +36,6 @@ func (c *Context) MaybeLocationLookup() error {
return c.Session.MaybeLookupLocation() return c.Session.MaybeLookupLocation()
} }
// 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 {
if c.IsBatch == true {
return errors.New("cannot run onboarding in batch mode")
}
if err := onboard.Onboarding(c.Config); err != nil {
return errors.Wrap(err, "onboarding")
}
}
return nil
}
// Init the OONI manager // Init the OONI manager
func (c *Context) Init() error { func (c *Context) Init() error {
var err error var err error

View File

@ -1,4 +1,4 @@
package util package utils
import ( import (
"testing" "testing"

View File

@ -1,4 +1,4 @@
package util package utils
import ( import (
"bytes" "bytes"