Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(torsf): collect tor logs, select rendezvous method, count bytes #683

Merged
merged 11 commits into from
Feb 7, 2022
Merged
13 changes: 13 additions & 0 deletions internal/bytecounter/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,16 @@ func (c *Conn) Write(p []byte) (int, error) {
c.Counter.CountBytesSent(count)
return count, err
}

// Wrap returns a new conn that uses the given counter.
func Wrap(conn net.Conn, counter *Counter) net.Conn {
return &Conn{Conn: conn, Counter: counter}
}

// MaybeWrap is like wrap if counter is not nil, otherwise it's a no-op.
func MaybeWrap(conn net.Conn, counter *Counter) net.Conn {
if counter == nil {
return conn
}
return Wrap(conn, counter)
}
31 changes: 31 additions & 0 deletions internal/bytecounter/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,34 @@ func TestConnWorksOnFailure(t *testing.T) {
t.Fatal("unexpected number of bytes sent")
}
}

func TestWrap(t *testing.T) {
conn := &mocks.Conn{}
counter := New()
nconn := Wrap(conn, counter)
_, good := nconn.(*Conn)
if !good {
t.Fatal("did not wrap")
}
}

func TestMaybeWrap(t *testing.T) {
t.Run("with nil counter", func(t *testing.T) {
conn := &mocks.Conn{}
nconn := MaybeWrap(conn, nil)
_, good := nconn.(*mocks.Conn)
if !good {
t.Fatal("did not wrap")
}
})

t.Run("with legit counter", func(t *testing.T) {
conn := &mocks.Conn{}
counter := New()
nconn := MaybeWrap(conn, counter)
_, good := nconn.(*Conn)
if !good {
t.Fatal("did not wrap")
}
})
}
40 changes: 40 additions & 0 deletions internal/bytecounter/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package bytecounter

import (
"context"
"net"
)

type byteCounterSessionKey struct{}

// ContextSessionByteCounter retrieves the session byte counter from the context
func ContextSessionByteCounter(ctx context.Context) *Counter {
counter, _ := ctx.Value(byteCounterSessionKey{}).(*Counter)
return counter
}

// WithSessionByteCounter assigns the session byte counter to the context.
func WithSessionByteCounter(ctx context.Context, counter *Counter) context.Context {
return context.WithValue(ctx, byteCounterSessionKey{}, counter)
}

type byteCounterExperimentKey struct{}

// ContextExperimentByteCounter retrieves the experiment byte counter from the context
func ContextExperimentByteCounter(ctx context.Context) *Counter {
counter, _ := ctx.Value(byteCounterExperimentKey{}).(*Counter)
return counter
}

// WithExperimentByteCounter assigns the experiment byte counter to the context.
func WithExperimentByteCounter(ctx context.Context, counter *Counter) context.Context {
return context.WithValue(ctx, byteCounterExperimentKey{}, counter)
}

// WrapWithContextByteCounters wraps a conn with the byte counters
// that have previosuly been configured into a context.
func WrapWithContextByteCounters(ctx context.Context, conn net.Conn) net.Conn {
bassosimone marked this conversation as resolved.
Show resolved Hide resolved
conn = MaybeWrap(conn, ContextExperimentByteCounter(ctx))
conn = MaybeWrap(conn, ContextSessionByteCounter(ctx))
return conn
}
61 changes: 61 additions & 0 deletions internal/bytecounter/context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package bytecounter

import (
"context"
"net"
"testing"

"github.com/ooni/probe-cli/v3/internal/model/mocks"
)

func TestSessionByteCounter(t *testing.T) {
counter := New()
ctx := context.Background()
ctx = WithSessionByteCounter(ctx, counter)
outer := ContextSessionByteCounter(ctx)
if outer != counter {
t.Fatal("unexpected result")
}
}

func TestExperimentByteCounter(t *testing.T) {
counter := New()
ctx := context.Background()
ctx = WithExperimentByteCounter(ctx, counter)
outer := ContextExperimentByteCounter(ctx)
if outer != counter {
t.Fatal("unexpected result")
}
}

func TestWrapWithContextByteCounters(t *testing.T) {
var conn net.Conn = &mocks.Conn{
MockRead: func(b []byte) (int, error) {
return len(b), nil
},
MockWrite: func(b []byte) (int, error) {
return len(b), nil
},
}
sessCounter := New()
expCounter := New()
ctx := context.Background()
ctx = WithSessionByteCounter(ctx, sessCounter)
ctx = WithExperimentByteCounter(ctx, expCounter)
conn = WrapWithContextByteCounters(ctx, conn)
buf := make([]byte, 128)
conn.Read(buf)
conn.Write(buf)
if sessCounter.Received.Load() != 128 {
t.Fatal("invalid value")
}
if sessCounter.Sent.Load() != 128 {
t.Fatal("invalid value")
}
if expCounter.Received.Load() != 128 {
t.Fatal("invalid value")
}
if expCounter.Sent.Load() != 128 {
t.Fatal("invalid value")
}
}
43 changes: 0 additions & 43 deletions internal/cmd/ptxclient/ptxclient.go

This file was deleted.

5 changes: 2 additions & 3 deletions internal/engine/experiment.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
"github.com/ooni/probe-cli/v3/internal/engine/netx/dialer"
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
"github.com/ooni/probe-cli/v3/internal/model"
Expand Down Expand Up @@ -161,8 +160,8 @@ func (e *Experiment) MeasureAsync(
if err != nil {
return nil, err
}
ctx = dialer.WithSessionByteCounter(ctx, e.session.byteCounter)
ctx = dialer.WithExperimentByteCounter(ctx, e.byteCounter)
ctx = bytecounter.WithSessionByteCounter(ctx, e.session.byteCounter)
ctx = bytecounter.WithExperimentByteCounter(ctx, e.byteCounter)
var async model.ExperimentMeasurerAsync
if v, okay := e.measurer.(model.ExperimentMeasurerAsync); okay {
async = v
Expand Down
1 change: 1 addition & 0 deletions internal/engine/experiment/torsf/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/torsf
31 changes: 31 additions & 0 deletions internal/engine/experiment/torsf/testdata/tor.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Feb 04 15:04:29.000 [notice] Tor 0.4.6.9 opening new log file.
Feb 04 15:04:29.360 [notice] We compiled with OpenSSL 101010cf: OpenSSL 1.1.1l FIPS 24 Aug 2021 and we are running with OpenSSL 101010cf: 1.1.1l. These two versions should be binary compatible.
Feb 04 15:04:29.363 [notice] Tor 0.4.6.9 running on Linux with Libevent 2.1.12-stable, OpenSSL 1.1.1l, Zlib 1.2.11, Liblzma 5.2.5, Libzstd 1.5.2 and Glibc 2.34 as libc.
Feb 04 15:04:29.363 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Feb 04 15:04:29.363 [warn] Tor was compiled with zstd 1.5.1, but is running with zstd 1.5.2. For safety, we'll avoid using advanced zstd functionality.
Feb 04 15:04:29.363 [notice] Read configuration file "/home/sbs/.miniooni/tunnel/torsf/tor/torrc-2981077975".
hellais marked this conversation as resolved.
Show resolved Hide resolved
Feb 04 15:04:29.366 [notice] Opening Control listener on 127.0.0.1:0
Feb 04 15:04:29.367 [notice] Control listener listening on port 41423.
Feb 04 15:04:29.367 [notice] Opened Control listener connection (ready) on 127.0.0.1:41423
Feb 04 15:04:29.367 [notice] DisableNetwork is set. Tor will not make or accept non-control network connections. Shutting down all existing connections.
Feb 04 15:04:29.000 [notice] Parsing GEOIP IPv4 file /usr/share/tor/geoip.
Feb 04 15:04:29.000 [notice] Parsing GEOIP IPv6 file /usr/share/tor/geoip6.
Feb 04 15:04:29.000 [notice] Bootstrapped 0% (starting): Starting
Feb 04 15:04:29.000 [notice] Starting with guard context "bridges"
Feb 04 15:04:29.000 [notice] new bridge descriptor 'flakey4' (cached): $2B280B23E1107BB62ABFC40DDCC8824814F80A72~flakey4 [1zOHpg+FxqQfi/6jDLtCpHHqBTH8gjYmCKXkus1D5Ko] at 192.0.2.3
Feb 04 15:04:29.000 [notice] Delaying directory fetches: DisableNetwork is set.
Feb 04 15:04:29.000 [notice] New control connection opened from 127.0.0.1.
Feb 04 15:04:29.000 [notice] Opening Socks listener on 127.0.0.1:0
Feb 04 15:04:29.000 [notice] Socks listener listening on port 42089.
Feb 04 15:04:29.000 [notice] Opened Socks listener connection (ready) on 127.0.0.1:42089
Feb 04 15:04:29.000 [notice] Tor 0.4.6.9 opening log file.
Feb 04 15:04:29.000 [notice] Bootstrapped 1% (conn_pt): Connecting to pluggable transport
Feb 04 15:04:30.000 [notice] Bootstrapped 2% (conn_done_pt): Connected to pluggable transport
Feb 04 15:04:30.000 [notice] Bootstrapped 10% (conn_done): Connected to a relay
Feb 04 15:06:20.000 [notice] Bootstrapped 14% (handshake): Handshaking with a relay
Feb 04 15:06:24.000 [notice] Bootstrapped 15% (handshake_done): Handshake with a relay done
Feb 04 15:06:24.000 [notice] Bootstrapped 75% (enough_dirinfo): Loaded enough directory info to build circuits
Feb 04 15:06:24.000 [notice] Bootstrapped 95% (circuit_create): Establishing a Tor circuit
Feb 04 15:06:26.000 [notice] new bridge descriptor 'flakey4' (fresh): $2B280B23E1107BB62ABFC40DDCC8824814F80A72~flakey4 [1zOHpg+FxqQfi/6jDLtCpHHqBTH8gjYmCKXkus1D5Ko] at 192.0.2.3
Feb 04 15:06:39.000 [notice] Bootstrapped 100% (done): Done
Feb 04 15:06:39.000 [notice] Catching signal TERM, exiting cleanly.
Loading