2022-05-31 21:53:01 +02:00
|
|
|
package tracex
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-06-01 07:44:54 +02:00
|
|
|
//
|
|
|
|
// TCP and connected UDP sockets
|
|
|
|
//
|
|
|
|
|
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-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
// DialerSaver saves events occurring during the dial
|
|
|
|
type DialerSaver struct {
|
2022-06-01 07:44:54 +02:00
|
|
|
// Dialer is the underlying dialer,
|
|
|
|
Dialer model.Dialer
|
|
|
|
|
|
|
|
// Saver saves events.
|
2022-05-31 21:53:01 +02:00
|
|
|
Saver *Saver
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 08:31:20 +02:00
|
|
|
// NewConnectObserver returns a DialerWrapper that observes the
|
|
|
|
// connect event. This function will return nil, which is a valid
|
|
|
|
// DialerWrapper for netxlite.WrapDialer, if Saver is nil.
|
|
|
|
func (s *Saver) NewConnectObserver() model.DialerWrapper {
|
|
|
|
if s == nil {
|
|
|
|
return nil // valid DialerWrapper according to netxlite's docs
|
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
return &dialerConnectObserver{
|
2022-06-01 08:31:20 +02:00
|
|
|
saver: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
type dialerConnectObserver struct {
|
2022-06-01 08:31:20 +02:00
|
|
|
saver *Saver
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
var _ model.DialerWrapper = &dialerConnectObserver{}
|
2022-06-01 08:31:20 +02:00
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (w *dialerConnectObserver) WrapDialer(d model.Dialer) model.Dialer {
|
|
|
|
return &DialerSaver{
|
2022-06-01 08:31:20 +02:00
|
|
|
Dialer: d,
|
|
|
|
Saver: w.saver,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
// DialContext implements Dialer.DialContext
|
2022-06-01 23:15:47 +02:00
|
|
|
func (d *DialerSaver) 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-06-01 14:32:16 +02:00
|
|
|
d.Saver.Write(&EventConnectOperation{&EventValue{
|
2021-02-02 12:05:47 +01:00
|
|
|
Address: address,
|
|
|
|
Duration: stop.Sub(start),
|
|
|
|
Err: err,
|
|
|
|
Proto: network,
|
|
|
|
Time: stop,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2021-02-02 12:05:47 +01:00
|
|
|
return conn, err
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (d *DialerSaver) CloseIdleConnections() {
|
2022-06-01 07:44:54 +02:00
|
|
|
d.Dialer.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
// DialerConnSaver wraps the returned connection such that we
|
2021-02-02 12:05:47 +01:00
|
|
|
// collect all the read/write events that occur.
|
2022-06-01 23:15:47 +02:00
|
|
|
type DialerConnSaver struct {
|
2022-06-01 07:44:54 +02:00
|
|
|
// Dialer is the underlying dialer
|
|
|
|
Dialer model.Dialer
|
|
|
|
|
|
|
|
// Saver saves events
|
2022-05-31 21:53:01 +02:00
|
|
|
Saver *Saver
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 08:31:20 +02:00
|
|
|
// NewReadWriteObserver returns a DialerWrapper that observes the
|
|
|
|
// I/O events. This function will return nil, which is a valid
|
|
|
|
// DialerWrapper for netxlite.WrapDialer, if Saver is nil.
|
|
|
|
func (s *Saver) NewReadWriteObserver() model.DialerWrapper {
|
|
|
|
if s == nil {
|
|
|
|
return nil // valid DialerWrapper according to netxlite's docs
|
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
return &dialerReadWriteObserver{
|
2022-06-01 08:31:20 +02:00
|
|
|
saver: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
type dialerReadWriteObserver struct {
|
2022-06-01 08:31:20 +02:00
|
|
|
saver *Saver
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
var _ model.DialerWrapper = &dialerReadWriteObserver{}
|
2022-06-01 08:31:20 +02:00
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (w *dialerReadWriteObserver) WrapDialer(d model.Dialer) model.Dialer {
|
|
|
|
return &DialerConnSaver{
|
2022-06-01 08:31:20 +02:00
|
|
|
Dialer: d,
|
|
|
|
Saver: w.saver,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-02 12:05:47 +01:00
|
|
|
// DialContext implements Dialer.DialContext
|
2022-06-01 23:15:47 +02:00
|
|
|
func (d *DialerConnSaver) 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
|
|
|
|
}
|
2022-06-01 23:15:47 +02:00
|
|
|
return &dialerConnWrapper{saver: d.Saver, Conn: conn}, nil
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (d *DialerConnSaver) CloseIdleConnections() {
|
2022-06-01 07:44:54 +02:00
|
|
|
d.Dialer.CloseIdleConnections()
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
type dialerConnWrapper struct {
|
2021-02-02 12:05:47 +01:00
|
|
|
net.Conn
|
2022-05-31 21:53:01 +02:00
|
|
|
saver *Saver
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (c *dialerConnWrapper) Read(p []byte) (int, error) {
|
|
|
|
proto := c.Conn.RemoteAddr().Network()
|
|
|
|
remoteAddr := c.Conn.RemoteAddr().String()
|
2021-02-02 12:05:47 +01:00
|
|
|
start := time.Now()
|
|
|
|
count, err := c.Conn.Read(p)
|
|
|
|
stop := time.Now()
|
2022-06-01 14:32:16 +02:00
|
|
|
c.saver.Write(&EventReadOperation{&EventValue{
|
2022-06-01 23:15:47 +02:00
|
|
|
Address: remoteAddr,
|
2021-02-02 12:05:47 +01:00
|
|
|
Data: p[:count],
|
|
|
|
Duration: stop.Sub(start),
|
|
|
|
Err: err,
|
|
|
|
NumBytes: count,
|
2022-06-01 23:15:47 +02:00
|
|
|
Proto: proto,
|
2021-02-02 12:05:47 +01:00
|
|
|
Time: stop,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2021-02-02 12:05:47 +01:00
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
func (c *dialerConnWrapper) Write(p []byte) (int, error) {
|
|
|
|
proto := c.Conn.RemoteAddr().Network()
|
|
|
|
remoteAddr := c.Conn.RemoteAddr().String()
|
2021-02-02 12:05:47 +01:00
|
|
|
start := time.Now()
|
|
|
|
count, err := c.Conn.Write(p)
|
|
|
|
stop := time.Now()
|
2022-06-01 14:32:16 +02:00
|
|
|
c.saver.Write(&EventWriteOperation{&EventValue{
|
2022-06-01 23:15:47 +02:00
|
|
|
Address: remoteAddr,
|
2021-02-02 12:05:47 +01:00
|
|
|
Data: p[:count],
|
|
|
|
Duration: stop.Sub(start),
|
|
|
|
Err: err,
|
|
|
|
NumBytes: count,
|
2022-06-01 23:15:47 +02:00
|
|
|
Proto: proto,
|
2021-02-02 12:05:47 +01:00
|
|
|
Time: stop,
|
2022-06-01 14:32:16 +02:00
|
|
|
}})
|
2021-02-02 12:05:47 +01:00
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2022-06-01 23:15:47 +02:00
|
|
|
var _ model.Dialer = &DialerSaver{}
|
|
|
|
var _ model.Dialer = &DialerConnSaver{}
|
|
|
|
var _ net.Conn = &dialerConnWrapper{}
|