2021-02-02 12:05:47 +01:00
|
|
|
package dialer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net"
|
|
|
|
|
2021-07-01 16:34:36 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/errorsx"
|
2021-02-02 12:05:47 +01:00
|
|
|
)
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
// errorWrapperDialer is a dialer that performs err wrapping
|
|
|
|
type errorWrapperDialer struct {
|
2021-02-02 12:05:47 +01:00
|
|
|
Dialer
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext implements Dialer.DialContext
|
2021-06-09 09:42:31 +02:00
|
|
|
func (d *errorWrapperDialer) 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)
|
2021-07-01 16:34:36 +02:00
|
|
|
err = errorsx.SafeErrWrapperBuilder{
|
2021-02-02 12:05:47 +01:00
|
|
|
Error: err,
|
2021-07-01 16:34:36 +02:00
|
|
|
Operation: errorsx.ConnectOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
}.MaybeBuild()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-06-09 09:42:31 +02:00
|
|
|
return &errorWrapperConn{Conn: conn}, nil
|
2021-02-02 12:05:47 +01:00
|
|
|
}
|
|
|
|
|
2021-06-09 09:42:31 +02:00
|
|
|
// errorWrapperConn is a net.Conn that performs error wrapping.
|
|
|
|
type errorWrapperConn struct {
|
2021-02-02 12:05:47 +01:00
|
|
|
net.Conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read implements net.Conn.Read
|
2021-06-09 09:42:31 +02:00
|
|
|
func (c *errorWrapperConn) Read(b []byte) (n int, err error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
n, err = c.Conn.Read(b)
|
2021-07-01 16:34:36 +02:00
|
|
|
err = errorsx.SafeErrWrapperBuilder{
|
2021-02-02 12:05:47 +01:00
|
|
|
Error: err,
|
2021-07-01 16:34:36 +02:00
|
|
|
Operation: errorsx.ReadOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
}.MaybeBuild()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write implements net.Conn.Write
|
2021-06-09 09:42:31 +02:00
|
|
|
func (c *errorWrapperConn) Write(b []byte) (n int, err error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
n, err = c.Conn.Write(b)
|
2021-07-01 16:34:36 +02:00
|
|
|
err = errorsx.SafeErrWrapperBuilder{
|
2021-02-02 12:05:47 +01:00
|
|
|
Error: err,
|
2021-07-01 16:34:36 +02:00
|
|
|
Operation: errorsx.WriteOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
}.MaybeBuild()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements net.Conn.Close
|
2021-06-09 09:42:31 +02:00
|
|
|
func (c *errorWrapperConn) Close() (err error) {
|
2021-02-02 12:05:47 +01:00
|
|
|
err = c.Conn.Close()
|
2021-07-01 16:34:36 +02:00
|
|
|
err = errorsx.SafeErrWrapperBuilder{
|
2021-02-02 12:05:47 +01:00
|
|
|
Error: err,
|
2021-07-01 16:34:36 +02:00
|
|
|
Operation: errorsx.CloseOperation,
|
2021-02-02 12:05:47 +01:00
|
|
|
}.MaybeBuild()
|
|
|
|
return
|
|
|
|
}
|