2021-07-01 20:58:15 +02:00
|
|
|
package errorsx
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
2021-07-02 10:39:14 +02:00
|
|
|
"net"
|
2021-07-01 20:58:15 +02:00
|
|
|
|
|
|
|
"github.com/lucas-clemente/quic-go"
|
2021-09-07 17:09:30 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/errorsx"
|
2021-09-05 14:49:38 +02:00
|
|
|
"github.com/ooni/probe-cli/v3/internal/netxlite/quicx"
|
2021-07-01 20:58:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// QUICContextDialer is a dialer for QUIC using Context.
|
|
|
|
type QUICContextDialer interface {
|
|
|
|
// DialContext establishes a new QUIC session using the given
|
|
|
|
// network and address. The tlsConfig and the quicConfig arguments
|
|
|
|
// MUST NOT be nil. Returns either the session or an error.
|
|
|
|
DialContext(ctx context.Context, network, address string,
|
|
|
|
tlsConfig *tls.Config, quicConfig *quic.Config) (quic.EarlySession, error)
|
|
|
|
}
|
|
|
|
|
2021-07-02 10:39:14 +02:00
|
|
|
// QUICListener listens for QUIC connections.
|
|
|
|
type QUICListener interface {
|
|
|
|
// Listen creates a new listening UDPConn.
|
2021-08-23 16:49:22 +02:00
|
|
|
Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error)
|
2021-07-02 10:39:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorWrapperQUICListener is a QUICListener that wraps errors.
|
|
|
|
type ErrorWrapperQUICListener struct {
|
|
|
|
// QUICListener is the underlying listener.
|
|
|
|
QUICListener QUICListener
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ QUICListener = &ErrorWrapperQUICListener{}
|
|
|
|
|
|
|
|
// Listen implements QUICListener.Listen.
|
2021-08-23 16:49:22 +02:00
|
|
|
func (qls *ErrorWrapperQUICListener) Listen(addr *net.UDPAddr) (quicx.UDPLikeConn, error) {
|
2021-07-02 10:39:14 +02:00
|
|
|
pconn, err := qls.QUICListener.Listen(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, SafeErrWrapperBuilder{
|
|
|
|
Error: err,
|
2021-09-07 17:09:30 +02:00
|
|
|
Operation: errorsx.QUICListenOperation,
|
2021-07-02 10:39:14 +02:00
|
|
|
}.MaybeBuild()
|
|
|
|
}
|
|
|
|
return &errorWrapperUDPConn{pconn}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-23 16:49:22 +02:00
|
|
|
// errorWrapperUDPConn is a quicx.UDPLikeConn that wraps errors.
|
2021-07-02 10:39:14 +02:00
|
|
|
type errorWrapperUDPConn struct {
|
2021-08-23 16:49:22 +02:00
|
|
|
// UDPLikeConn is the underlying conn.
|
|
|
|
quicx.UDPLikeConn
|
2021-07-02 10:39:14 +02:00
|
|
|
}
|
|
|
|
|
2021-08-23 16:49:22 +02:00
|
|
|
var _ quicx.UDPLikeConn = &errorWrapperUDPConn{}
|
2021-07-02 10:39:14 +02:00
|
|
|
|
2021-08-23 16:49:22 +02:00
|
|
|
// WriteTo implements quicx.UDPLikeConn.WriteTo.
|
2021-07-02 10:39:14 +02:00
|
|
|
func (c *errorWrapperUDPConn) WriteTo(p []byte, addr net.Addr) (int, error) {
|
2021-08-23 16:49:22 +02:00
|
|
|
count, err := c.UDPLikeConn.WriteTo(p, addr)
|
2021-07-02 10:39:14 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, SafeErrWrapperBuilder{
|
|
|
|
Error: err,
|
2021-09-07 17:09:30 +02:00
|
|
|
Operation: errorsx.WriteToOperation,
|
2021-07-02 10:39:14 +02:00
|
|
|
}.MaybeBuild()
|
|
|
|
}
|
|
|
|
return count, nil
|
|
|
|
}
|
|
|
|
|
2021-08-23 16:49:22 +02:00
|
|
|
// ReadFrom implements quicx.UDPLikeConn.ReadFrom.
|
|
|
|
func (c *errorWrapperUDPConn) ReadFrom(b []byte) (int, net.Addr, error) {
|
|
|
|
n, addr, err := c.UDPLikeConn.ReadFrom(b)
|
2021-07-02 10:39:14 +02:00
|
|
|
if err != nil {
|
2021-08-23 16:49:22 +02:00
|
|
|
return 0, nil, SafeErrWrapperBuilder{
|
2021-07-02 10:39:14 +02:00
|
|
|
Error: err,
|
2021-09-07 17:09:30 +02:00
|
|
|
Operation: errorsx.ReadFromOperation,
|
2021-07-02 10:39:14 +02:00
|
|
|
}.MaybeBuild()
|
|
|
|
}
|
2021-08-23 16:49:22 +02:00
|
|
|
return n, addr, nil
|
2021-07-02 10:39:14 +02:00
|
|
|
}
|
|
|
|
|
2021-07-01 20:58:15 +02:00
|
|
|
// ErrorWrapperQUICDialer is a dialer that performs quic err wrapping
|
|
|
|
type ErrorWrapperQUICDialer struct {
|
|
|
|
Dialer QUICContextDialer
|
|
|
|
}
|
|
|
|
|
|
|
|
// DialContext implements ContextDialer.DialContext
|
|
|
|
func (d *ErrorWrapperQUICDialer) DialContext(
|
|
|
|
ctx context.Context, network string, host string,
|
|
|
|
tlsCfg *tls.Config, cfg *quic.Config) (quic.EarlySession, error) {
|
|
|
|
sess, err := d.Dialer.DialContext(ctx, network, host, tlsCfg, cfg)
|
|
|
|
if err != nil {
|
2021-09-07 17:23:24 +02:00
|
|
|
return nil, SafeErrWrapperBuilder{
|
|
|
|
Classifier: errorsx.ClassifyQUICHandshakeError,
|
|
|
|
Error: err,
|
|
|
|
Operation: errorsx.QUICHandshakeOperation,
|
|
|
|
}.MaybeBuild()
|
2021-07-01 20:58:15 +02:00
|
|
|
}
|
|
|
|
return sess, nil
|
|
|
|
}
|