feat: create tunnel inside NewSession (#286)

* feat: create tunnel inside NewSession

We want to create the tunnel when we create the session. This change
allows us to nicely ignore the problem of creating a tunnel when we
already have a proxy, as well as the problem of locking. Everything is
happening, in fact, inside of the NewSession factory.

Modify miniooni such that --tunnel is just syntactic sugar for
--proxy, at least for now. We want, in the future, to teach the
tunnel to possibly use a socks5 proxy.

Because starting a tunnel is a slow operation, we need a context in
NewSession. This causes a bunch of places to change. Not really a big
deal except we need to propagate the changes.

Make sure that the mobile code can create a new session using a
proxy for all the APIs we support.

Make sure all tests are still green and we don't loose coverage of
the various ways in which this code could be used.

This change is part of https://github.com/ooni/probe/issues/985.

* changes after merge

* fix: only keep tests that can hopefully work

While there, identify other places where we should add more
tests or fix integration tests.

Part of https://github.com/ooni/probe/issues/985
This commit is contained in:
Simone Basso
2021-04-05 15:28:13 +02:00
committed by GitHub
parent a849213b59
commit c5ad5eedeb
18 changed files with 135 additions and 223 deletions
+19 -4
View File
@@ -140,6 +140,10 @@ func fatalIfFalse(cond bool, msg string) {
}
}
func fatalIfTrue(cond bool, msg string) {
fatalIfFalse(!cond, msg)
}
// Main is the main function of miniooni. This function parses the command line
// options and uses a global state. Use MainWithConfiguration if you want to avoid
// using any global state and relying on command line options.
@@ -273,6 +277,15 @@ of seconds after which to stop running Web Connectivity.
This error message will be removed after 2021-11-01.
`
// tunnelAndProxy is the text printed when the user specifies
// both the --tunnel and the --proxy options
const tunnelAndProxy = `USAGE ERROR: The --tunnel option and the --proxy
option cannot be specified at the same time. The --tunnel option is actually
just syntactic sugar for --proxy. Setting --tunnel=psiphon is currently the
equivalent of setting --proxy=psiphon:///. This MAY change in a future version
of miniooni, when we will allow a tunnel to use a proxy.
`
// MainWithConfiguration is the miniooni main with a specific configuration
// represented by the experiment name and the current options.
//
@@ -280,6 +293,11 @@ This error message will be removed after 2021-11-01.
// integrate this function to either handle the panic of ignore it.
func MainWithConfiguration(experimentName string, currentOptions Options) {
fatalIfFalse(currentOptions.Limit == 0, limitRemoved)
fatalIfTrue(currentOptions.Proxy != "" && currentOptions.Tunnel != "",
tunnelAndProxy)
if currentOptions.Tunnel != "" {
currentOptions.Proxy = fmt.Sprintf("%s:///", currentOptions.Tunnel)
}
ctx := context.Background()
@@ -354,7 +372,7 @@ func MainWithConfiguration(experimentName string, currentOptions Options) {
}}
}
sess, err := engine.NewSession(config)
sess, err := engine.NewSession(ctx, config)
fatalOnError(err, "cannot create measurement session")
defer func() {
sess.Close()
@@ -365,9 +383,6 @@ func MainWithConfiguration(experimentName string, currentOptions Options) {
}()
log.Debugf("miniooni temporary directory: %s", sess.TempDir())
err = sess.MaybeStartTunnel(context.Background(), currentOptions.Tunnel)
fatalOnError(err, "cannot start session tunnel")
log.Info("Looking up OONI backends; please be patient...")
err = sess.MaybeLookupBackends()
fatalOnError(err, "cannot lookup OONI backends")