Files
tendermint/privval/utils.go
M. J. Fromberger 08099ff669 privval: restrict listeners to TCP and Unix domain sockets (#8670)
Front load the protocol type check so we do not wind up creating listeners of
types that are not usable for this interface (for example, UDP).

Fixes #8647.
2022-06-02 10:20:00 -07:00

53 lines
1.5 KiB
Go

package privval
import (
"errors"
"fmt"
"net"
"github.com/tendermint/tendermint/crypto/ed25519"
"github.com/tendermint/tendermint/libs/log"
tmnet "github.com/tendermint/tendermint/libs/net"
)
// IsConnTimeout returns a boolean indicating whether the error is known to
// report that a connection timeout occurred. This detects both fundamental
// network timeouts, as well as ErrConnTimeout errors.
func IsConnTimeout(err error) bool {
_, ok := errors.Unwrap(err).(timeoutError)
switch {
case errors.As(err, &EndpointTimeoutError{}):
return true
case ok:
return true
default:
return false
}
}
// NewSignerListener creates a new SignerListenerEndpoint using the corresponding listen address
func NewSignerListener(listenAddr string, logger log.Logger) (*SignerListenerEndpoint, error) {
protocol, address := tmnet.ProtocolAndAddress(listenAddr)
if protocol != "unix" && protocol != "tcp" { //nolint:goconst
return nil, fmt.Errorf("unsupported address family %q, want unix or tcp", protocol)
}
ln, err := net.Listen(protocol, address)
if err != nil {
return nil, err
}
var listener net.Listener
switch protocol {
case "unix":
listener = NewUnixListener(ln)
case "tcp":
// TODO: persist this key so external signer can actually authenticate us
listener = NewTCPListener(ln, ed25519.GenPrivKey())
default:
panic("invalid protocol: " + protocol) // semantically unreachable
}
return NewSignerListenerEndpoint(logger.With("module", "privval"), listener), nil
}