ooni-probe-cli/internal/netxlite/shaping_shaping.go
Simone Basso 6924d1ad81
refactor: only use shaping dialer for ndt7 and dash (#754)
See https://github.com/ooni/probe/issues/2112 for context.

While there, run `go fix -fix buildtag ./...`
2022-05-24 18:23:42 +02:00

44 lines
820 B
Go

//go:build shaping
package netxlite
import (
"context"
"net"
"time"
"github.com/ooni/probe-cli/v3/internal/model"
)
func newMaybeShapingDialer(dialer model.Dialer) model.Dialer {
return &shapingDialer{dialer}
}
type shapingDialer struct {
model.Dialer
}
// DialContext implements Dialer.DialContext
func (d *shapingDialer) DialContext(
ctx context.Context, network, address string) (net.Conn, error) {
conn, err := d.Dialer.DialContext(ctx, network, address)
if err != nil {
return nil, err
}
return &shapingConn{Conn: conn}, nil
}
type shapingConn struct {
net.Conn
}
func (c *shapingConn) Read(p []byte) (int, error) {
time.Sleep(100 * time.Millisecond)
return c.Conn.Read(p)
}
func (c *shapingConn) Write(p []byte) (int, error) {
time.Sleep(100 * time.Millisecond)
return c.Conn.Write(p)
}