From 1eb63bc4b6e72a8a2f7de19859f5dff6161c6638 Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Sun, 4 Apr 2021 11:23:03 +0200 Subject: [PATCH] refactor(tunnel): remove dependecy from logger (#292) Part of https://github.com/ooni/probe/issues/985 --- internal/engine/session.go | 8 ++++++++ internal/engine/tunnel/tunnel.go | 7 ------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/internal/engine/session.go b/internal/engine/session.go index d6a5869..eb444e3 100644 --- a/internal/engine/session.go +++ b/internal/engine/session.go @@ -336,6 +336,11 @@ func (s *Session) MaybeStartTunnel(ctx context.Context, name string) error { // TODO(bassosimone): see if we can unify tunnelMu and mu. s.tunnelMu.Lock() defer s.tunnelMu.Unlock() + if name == "" { + // There is no point in continuing if we know + // we don't need to do anything. + return nil + } if s.tunnel != nil && s.tunnelName == name { // We've been asked more than once to start the same tunnel. return nil @@ -343,6 +348,8 @@ func (s *Session) MaybeStartTunnel(ctx context.Context, name string) error { if s.proxyURL != nil && name == "" { // The user configured a proxy and here we're not actually trying // to start any tunnel since `name` is empty. + // TODO(bassosimone): this if branch is probably useless now + // because we stop above when name is "". return nil } if s.proxyURL != nil || s.tunnel != nil { @@ -350,6 +357,7 @@ func (s *Session) MaybeStartTunnel(ctx context.Context, name string) error { // sets a proxy, the second check for s.tunnel is for robustness. return ErrAlreadyUsingProxy } + s.logger.Infof("starting '%s' tunnel; please be patient...", name) tunnel, err := tunnel.Start(ctx, &tunnel.Config{ Name: name, Session: s, diff --git a/internal/engine/tunnel/tunnel.go b/internal/engine/tunnel/tunnel.go index e203f70..3af1bde 100644 --- a/internal/engine/tunnel/tunnel.go +++ b/internal/engine/tunnel/tunnel.go @@ -7,8 +7,6 @@ import ( "errors" "net/url" "time" - - "github.com/ooni/probe-cli/v3/internal/engine/model" ) // Session is the way in which this package sees a Session. @@ -17,7 +15,6 @@ type Session interface { TempDir() string TorArgs() []string TorBinary() string - Logger() model.Logger } // Tunnel is a tunnel used by the session @@ -30,17 +27,13 @@ type Tunnel interface { // Start starts a new tunnel by name or returns an error. Note that if you // pass to this function the "" tunnel, you get back nil, nil. func Start(ctx context.Context, config *Config) (Tunnel, error) { - logger := config.Session.Logger() switch config.Name { case "": - logger.Debugf("no tunnel has been requested") return enforceNilContract(nil, nil) case "psiphon": - logger.Infof("starting %s tunnel; please be patient...", config.Name) tun, err := psiphonStart(ctx, config) return enforceNilContract(tun, err) case "tor": - logger.Infof("starting %s tunnel; please be patient...", config.Name) tun, err := torStart(ctx, config) return enforceNilContract(tun, err) default: