2022-05-31 21:53:01 +02:00
|
|
|
package tracex
|
2021-02-02 12:05:47 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
"time"
|
|
|
|
|
2022-01-07 18:33:37 +01:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/model"
|
2021-09-28 12:42:01 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2022-05-31 21:53:01 +02:00
|
|
|
// SaverDialer saves events occurring during the dial
|
|
|
|
type SaverDialer struct {
|
2022-01-07 18:33:37 +01:00
|
|
|
model.Dialer
|
2022-05-31 21:53:01 +02:00
|
|
|
Saver *Saver
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext implements Dialer.DialContext
|
2022-05-31 21:53:01 +02:00
|
|
|
func (d *SaverDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
start := time.Now()
|
|
|
|
conn, err := d.Dialer.DialContext(ctx, network, address)
|
|
|
|
stop := time.Now()
|
2022-05-31 21:53:01 +02:00
|
|
|
d.Saver.Write(Event{
|
2021-02-02 12:05:47 +01:00
|
|
|
Address: address,
|
|
|
|
Duration: stop.Sub(start),
|
|
|
|
Err: err,
|
2021-09-28 12:42:01 +02:00
|
|
|
Name: netxlite.ConnectOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
Proto: network,
|
|
|
|
Time: stop,
|
|
|
|
})
|
|
|
|
return conn, err
|
|
|
|
}
|
|
|
|
|
2022-05-31 21:53:01 +02:00
|
|
|
// SaverConnDialer wraps the returned connection such that we
|
2021-02-02 12:05:47 +01:00
|
|
|
// collect all the read/write events that occur.
|
2022-05-31 21:53:01 +02:00
|
|
|
type SaverConnDialer struct {
|
2022-01-07 18:33:37 +01:00
|
|
|
model.Dialer
|
2022-05-31 21:53:01 +02:00
|
|
|
Saver *Saver
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext implements Dialer.DialContext
|
2022-05-31 21:53:01 +02:00
|
|
|
func (d *SaverConnDialer) DialContext(ctx context.Context, network, address string) (net.Conn, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
conn, err := d.Dialer.DialContext(ctx, network, address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-09 09:42:31 +02:00
|
|
|
return &saverConn{saver: d.Saver, Conn: conn}, nil
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type saverConn struct {
|
|
|
|
net.Conn
|
2022-05-31 21:53:01 +02:00
|
|
|
saver *Saver
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
func (c *saverConn) Read(p []byte) (int, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
start := time.Now()
|
|
|
|
count, err := c.Conn.Read(p)
|
|
|
|
stop := time.Now()
|
2022-05-31 21:53:01 +02:00
|
|
|
c.saver.Write(Event{
|
2021-02-02 12:05:47 +01:00
|
|
|
Data: p[:count],
|
|
|
|
Duration: stop.Sub(start),
|
|
|
|
Err: err,
|
|
|
|
NumBytes: count,
|
2021-09-28 12:42:01 +02:00
|
|
|
Name: netxlite.ReadOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
Time: stop,
|
|
|
|
})
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
func (c *saverConn) Write(p []byte) (int, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
start := time.Now()
|
|
|
|
count, err := c.Conn.Write(p)
|
|
|
|
stop := time.Now()
|
2022-05-31 21:53:01 +02:00
|
|
|
c.saver.Write(Event{
|
2021-02-02 12:05:47 +01:00
|
|
|
Data: p[:count],
|
|
|
|
Duration: stop.Sub(start),
|
|
|
|
Err: err,
|
|
|
|
NumBytes: count,
|
2021-09-28 12:42:01 +02:00
|
|
|
Name: netxlite.WriteOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
Time: stop,
|
|
|
|
})
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2022-05-31 21:53:01 +02:00
|
|
|
var _ model.Dialer = &SaverDialer{}
|
|
|
|
var _ model.Dialer = &SaverConnDialer{}
|
2021-06-09 09:42:31 +02:00
|
|
|
var _ net.Conn = &saverConn{}
|