2021-09-05 14:49:38 +02:00
|
|
|
//go:build shaping
|
2021-02-02 12:05:47 +01:00
|
|
|
|
2022-05-24 18:23:42 +02:00
|
|
|
package netxlite
|
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-05-24 18:23:42 +02:00
|
|
|
func newMaybeShapingDialer(dialer model.Dialer) model.Dialer {
|
|
|
|
return &shapingDialer{dialer}
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
type shapingDialer struct {
|
2022-01-07 18:33:37 +01:00
|
|
|
model.Dialer
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext implements Dialer.DialContext
|
2021-06-09 09:42:31 +02:00
|
|
|
func (d *shapingDialer) DialContext(
|
2021-02-02 12:05:47 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
func (c *shapingConn) Read(p []byte) (int, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
return c.Conn.Read(p)
|
|
|
|
}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
func (c *shapingConn) Write(p []byte) (int, error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
return c.Conn.Write(p)
|
|
|
|
}
|