refactor: move ErrorWrapperTLSHandshaker to errorsx (#418)

Part of https://github.com/ooni/probe/issues/1505
This commit is contained in:
Simone Basso 2021-07-01 18:00:09 +02:00 committed by GitHub
commit 863899469e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 84 additions and 50 deletions

31
internal/errorsx/tls.go Normal file
View file

@ -0,0 +1,31 @@
package errorsx
import (
"context"
"crypto/tls"
"net"
)
// TLSHandshaker is the generic TLS handshaker
type TLSHandshaker interface {
Handshake(ctx context.Context, conn net.Conn, config *tls.Config) (
net.Conn, tls.ConnectionState, error)
}
// ErrorWrapperTLSHandshaker wraps the returned error to be an OONI error
type ErrorWrapperTLSHandshaker struct {
TLSHandshaker
}
// Handshake implements TLSHandshaker.Handshake
func (h *ErrorWrapperTLSHandshaker) Handshake(
ctx context.Context, conn net.Conn, config *tls.Config,
) (net.Conn, tls.ConnectionState, error) {
tlsconn, state, err := h.TLSHandshaker.Handshake(ctx, conn, config)
err = SafeErrWrapperBuilder{
Classifier: ClassifyTLSFailure,
Error: err,
Operation: TLSHandshakeOperation,
}.MaybeBuild()
return tlsconn, state, err
}