2022-04-01 16:16:46 +00:00
|
|
|
package upgrader
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
2022-04-01 16:16:46 +00:00
|
|
|
"time"
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/connmgr"
|
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
ipnet "github.com/libp2p/go-libp2p/core/pnet"
|
2023-02-22 21:58:17 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
2022-11-04 13:57:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/sec"
|
|
|
|
"github.com/libp2p/go-libp2p/core/transport"
|
2022-08-19 16:34:07 +00:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/net/pnet"
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
manet "github.com/multiformats/go-multiaddr/net"
|
2023-02-22 21:58:17 +00:00
|
|
|
mss "github.com/multiformats/go-multistream"
|
2018-07-04 10:51:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ErrNilPeer is returned when attempting to upgrade an outbound connection
|
|
|
|
// without specifying a peer ID.
|
|
|
|
var ErrNilPeer = errors.New("nil peer")
|
|
|
|
|
|
|
|
// AcceptQueueLength is the number of connections to fully setup before not accepting any new connections
|
|
|
|
var AcceptQueueLength = 16
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
const (
|
|
|
|
defaultAcceptTimeout = 15 * time.Second
|
|
|
|
defaultNegotiateTimeout = 60 * time.Second
|
|
|
|
)
|
2022-04-01 16:16:46 +00:00
|
|
|
|
|
|
|
type Option func(*upgrader) error
|
|
|
|
|
|
|
|
func WithAcceptTimeout(t time.Duration) Option {
|
|
|
|
return func(u *upgrader) error {
|
|
|
|
u.acceptTimeout = t
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
type StreamMuxer struct {
|
|
|
|
ID protocol.ID
|
|
|
|
Muxer network.Multiplexer
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
// Upgrader is a multistream upgrader that can upgrade an underlying connection
|
|
|
|
// to a full transport connection (secure and multiplexed).
|
2022-04-01 16:16:46 +00:00
|
|
|
type upgrader struct {
|
|
|
|
psk ipnet.PSK
|
|
|
|
connGater connmgr.ConnectionGater
|
|
|
|
rcmgr network.ResourceManager
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
muxerMuxer *mss.MultistreamMuxer[protocol.ID]
|
|
|
|
muxers []StreamMuxer
|
|
|
|
muxerIDs []protocol.ID
|
|
|
|
|
|
|
|
security []sec.SecureTransport
|
|
|
|
securityMuxer *mss.MultistreamMuxer[protocol.ID]
|
|
|
|
securityIDs []protocol.ID
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
// AcceptTimeout is the maximum duration an Accept is allowed to take.
|
|
|
|
// This includes the time between accepting the raw network connection,
|
|
|
|
// protocol selection as well as the handshake, if applicable.
|
|
|
|
//
|
|
|
|
// If unset, the default value (15s) is used.
|
|
|
|
acceptTimeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ transport.Upgrader = &upgrader{}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
func New(security []sec.SecureTransport, muxers []StreamMuxer, psk ipnet.PSK, rcmgr network.ResourceManager, connGater connmgr.ConnectionGater, opts ...Option) (transport.Upgrader, error) {
|
2022-04-01 16:16:46 +00:00
|
|
|
u := &upgrader{
|
|
|
|
acceptTimeout: defaultAcceptTimeout,
|
2023-02-22 21:58:17 +00:00
|
|
|
rcmgr: rcmgr,
|
|
|
|
connGater: connGater,
|
|
|
|
psk: psk,
|
|
|
|
muxerMuxer: mss.NewMultistreamMuxer[protocol.ID](),
|
|
|
|
muxers: muxers,
|
|
|
|
security: security,
|
|
|
|
securityMuxer: mss.NewMultistreamMuxer[protocol.ID](),
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(u); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if u.rcmgr == nil {
|
2023-02-22 21:58:17 +00:00
|
|
|
u.rcmgr = &network.NullResourceManager{}
|
|
|
|
}
|
|
|
|
u.muxerIDs = make([]protocol.ID, 0, len(muxers))
|
|
|
|
for _, m := range muxers {
|
|
|
|
u.muxerMuxer.AddHandler(m.ID, nil)
|
|
|
|
u.muxerIDs = append(u.muxerIDs, m.ID)
|
|
|
|
}
|
|
|
|
u.securityIDs = make([]protocol.ID, 0, len(security))
|
|
|
|
for _, s := range security {
|
|
|
|
u.securityMuxer.AddHandler(s.ID(), nil)
|
|
|
|
u.securityIDs = append(u.securityIDs, s.ID())
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
|
|
|
return u, nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// UpgradeListener upgrades the passed multiaddr-net listener into a full libp2p-transport listener.
|
2022-04-01 16:16:46 +00:00
|
|
|
func (u *upgrader) UpgradeListener(t transport.Transport, list manet.Listener) transport.Listener {
|
2018-07-04 10:51:47 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
l := &listener{
|
|
|
|
Listener: list,
|
|
|
|
upgrader: u,
|
|
|
|
transport: t,
|
2022-04-01 16:16:46 +00:00
|
|
|
rcmgr: u.rcmgr,
|
2018-07-04 10:51:47 +00:00
|
|
|
threshold: newThreshold(AcceptQueueLength),
|
2019-06-09 07:24:20 +00:00
|
|
|
incoming: make(chan transport.CapableConn),
|
2018-07-04 10:51:47 +00:00
|
|
|
cancel: cancel,
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
|
|
|
go l.handleIncoming()
|
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
// Upgrade upgrades the multiaddr/net connection into a full libp2p-transport connection.
|
|
|
|
func (u *upgrader) Upgrade(ctx context.Context, t transport.Transport, maconn manet.Conn, dir network.Direction, p peer.ID, connScope network.ConnManagementScope) (transport.CapableConn, error) {
|
|
|
|
c, err := u.upgrade(ctx, t, maconn, dir, p, connScope)
|
|
|
|
if err != nil {
|
|
|
|
connScope.Done()
|
|
|
|
return nil, err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2022-04-01 16:16:46 +00:00
|
|
|
return c, nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 16:16:46 +00:00
|
|
|
func (u *upgrader) upgrade(ctx context.Context, t transport.Transport, maconn manet.Conn, dir network.Direction, p peer.ID, connScope network.ConnManagementScope) (transport.CapableConn, error) {
|
|
|
|
if dir == network.DirOutbound && p == "" {
|
|
|
|
return nil, ErrNilPeer
|
|
|
|
}
|
|
|
|
var stat network.ConnStats
|
2021-10-19 13:43:41 +00:00
|
|
|
if cs, ok := maconn.(network.ConnStat); ok {
|
|
|
|
stat = cs.Stat()
|
|
|
|
}
|
|
|
|
|
2018-07-04 10:51:47 +00:00
|
|
|
var conn net.Conn = maconn
|
2022-04-01 16:16:46 +00:00
|
|
|
if u.psk != nil {
|
|
|
|
pconn, err := pnet.NewProtectedConn(u.psk, conn)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
2023-02-22 21:58:17 +00:00
|
|
|
return nil, fmt.Errorf("failed to setup private network protector: %w", err)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
conn = pconn
|
2021-06-16 20:19:45 +00:00
|
|
|
} else if ipnet.ForcePrivateNetwork {
|
2021-10-19 13:43:41 +00:00
|
|
|
log.Error("tried to dial with no Private Network Protector but usage of Private Networks is forced by the environment")
|
2021-06-16 20:19:45 +00:00
|
|
|
return nil, ipnet.ErrNotInPrivateNetwork
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
isServer := dir == network.DirInbound
|
|
|
|
sconn, security, err := u.setupSecurity(ctx, conn, p, isServer)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
|
|
|
conn.Close()
|
2023-02-22 21:58:17 +00:00
|
|
|
return nil, fmt.Errorf("failed to negotiate security protocol: %w", err)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
// call the connection gater, if one is registered.
|
2022-04-01 16:16:46 +00:00
|
|
|
if u.connGater != nil && !u.connGater.InterceptSecured(dir, sconn.RemotePeer(), maconn) {
|
2021-06-16 20:19:45 +00:00
|
|
|
if err := maconn.Close(); err != nil {
|
2022-04-01 16:16:46 +00:00
|
|
|
log.Errorw("failed to close connection", "peer", p, "addr", maconn.RemoteMultiaddr(), "error", err)
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("gater rejected connection with peer %s and addr %s with direction %d",
|
2023-02-22 21:58:17 +00:00
|
|
|
sconn.RemotePeer(), maconn.RemoteMultiaddr(), dir)
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
2022-04-01 16:16:46 +00:00
|
|
|
// Only call SetPeer if it hasn't already been set -- this can happen when we don't know
|
|
|
|
// the peer in advance and in some bug scenarios.
|
|
|
|
if connScope.PeerScope() == nil {
|
|
|
|
if err := connScope.SetPeer(sconn.RemotePeer()); err != nil {
|
|
|
|
log.Debugw("resource manager blocked connection for peer", "peer", sconn.RemotePeer(), "addr", conn.RemoteAddr(), "error", err)
|
|
|
|
if err := maconn.Close(); err != nil {
|
|
|
|
log.Errorw("failed to close connection", "peer", p, "addr", maconn.RemoteMultiaddr(), "error", err)
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("resource manager connection with peer %s and addr %s with direction %d",
|
2023-02-22 21:58:17 +00:00
|
|
|
sconn.RemotePeer(), maconn.RemoteMultiaddr(), dir)
|
2022-04-01 16:16:46 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
muxer, smconn, err := u.setupMuxer(ctx, sconn, isServer, connScope.PeerScope())
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
2019-06-09 07:24:20 +00:00
|
|
|
sconn.Close()
|
2023-02-22 21:58:17 +00:00
|
|
|
return nil, fmt.Errorf("failed to negotiate stream multiplexer: %w", err)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
tc := &transportConn{
|
2023-04-07 18:23:07 +00:00
|
|
|
MuxedConn: smconn,
|
|
|
|
ConnMultiaddrs: maconn,
|
|
|
|
ConnSecurity: sconn,
|
|
|
|
transport: t,
|
|
|
|
stat: stat,
|
|
|
|
scope: connScope,
|
|
|
|
muxer: muxer,
|
|
|
|
security: security,
|
|
|
|
usedEarlyMuxerNegotiation: sconn.ConnState().UsedEarlyMuxerNegotiation,
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
return tc, nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
func (u *upgrader) setupSecurity(ctx context.Context, conn net.Conn, p peer.ID, isServer bool) (sec.SecureConn, protocol.ID, error) {
|
|
|
|
st, err := u.negotiateSecurity(ctx, conn, isServer)
|
2023-02-22 21:58:17 +00:00
|
|
|
if err != nil {
|
2024-05-15 23:15:00 +00:00
|
|
|
return nil, "", err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2023-02-22 21:58:17 +00:00
|
|
|
if isServer {
|
|
|
|
sconn, err := st.SecureInbound(ctx, conn, p)
|
2024-05-15 23:15:00 +00:00
|
|
|
return sconn, st.ID(), err
|
2023-02-22 21:58:17 +00:00
|
|
|
}
|
|
|
|
sconn, err := st.SecureOutbound(ctx, conn, p)
|
2024-05-15 23:15:00 +00:00
|
|
|
return sconn, st.ID(), err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
func (u *upgrader) negotiateMuxer(nc net.Conn, isServer bool) (*StreamMuxer, error) {
|
|
|
|
if err := nc.SetDeadline(time.Now().Add(defaultNegotiateTimeout)); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-07-04 10:51:47 +00:00
|
|
|
|
2023-02-22 21:58:17 +00:00
|
|
|
var proto protocol.ID
|
|
|
|
if isServer {
|
|
|
|
selected, _, err := u.muxerMuxer.Negotiate(nc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
proto = selected
|
|
|
|
} else {
|
|
|
|
selected, err := mss.SelectOneOf(u.muxerIDs, nc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
proto = selected
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := nc.SetDeadline(time.Time{}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if m := u.getMuxerByID(proto); m != nil {
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("selected protocol we don't have a transport for")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *upgrader) getMuxerByID(id protocol.ID) *StreamMuxer {
|
|
|
|
for _, m := range u.muxers {
|
|
|
|
if m.ID == id {
|
|
|
|
return &m
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *upgrader) setupMuxer(ctx context.Context, conn sec.SecureConn, server bool, scope network.PeerScope) (protocol.ID, network.MuxedConn, error) {
|
|
|
|
muxerSelected := conn.ConnState().StreamMultiplexer
|
|
|
|
// Use muxer selected from security handshake if available. Otherwise fall back to multistream-selection.
|
|
|
|
if len(muxerSelected) > 0 {
|
|
|
|
m := u.getMuxerByID(muxerSelected)
|
|
|
|
if m == nil {
|
|
|
|
return "", nil, fmt.Errorf("selected a muxer we don't know: %s", muxerSelected)
|
|
|
|
}
|
|
|
|
c, err := m.Muxer.NewConn(conn, server, scope)
|
|
|
|
if err != nil {
|
|
|
|
return "", nil, err
|
|
|
|
}
|
|
|
|
return muxerSelected, c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type result struct {
|
|
|
|
smconn network.MuxedConn
|
|
|
|
muxerID protocol.ID
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan result, 1)
|
|
|
|
// TODO: The muxer should take a context.
|
2018-07-04 10:51:47 +00:00
|
|
|
go func() {
|
2023-02-22 21:58:17 +00:00
|
|
|
m, err := u.negotiateMuxer(conn, server)
|
|
|
|
if err != nil {
|
|
|
|
done <- result{err: err}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
smconn, err := m.Muxer.NewConn(conn, server, scope)
|
|
|
|
done <- result{smconn: smconn, muxerID: m.ID, err: err}
|
2018-07-04 10:51:47 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
2023-02-22 21:58:17 +00:00
|
|
|
case r := <-done:
|
|
|
|
return r.muxerID, r.smconn, r.err
|
2018-07-04 10:51:47 +00:00
|
|
|
case <-ctx.Done():
|
2019-06-09 07:24:20 +00:00
|
|
|
// interrupt this process
|
|
|
|
conn.Close()
|
|
|
|
// wait to finish
|
|
|
|
<-done
|
2023-02-22 21:58:17 +00:00
|
|
|
return "", nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *upgrader) getSecurityByID(id protocol.ID) sec.SecureTransport {
|
|
|
|
for _, s := range u.security {
|
|
|
|
if s.ID() == id {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-15 23:15:00 +00:00
|
|
|
func (u *upgrader) negotiateSecurity(ctx context.Context, insecure net.Conn, server bool) (sec.SecureTransport, error) {
|
2023-02-22 21:58:17 +00:00
|
|
|
type result struct {
|
2024-05-15 23:15:00 +00:00
|
|
|
proto protocol.ID
|
|
|
|
err error
|
2023-02-22 21:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan result, 1)
|
|
|
|
go func() {
|
|
|
|
if server {
|
|
|
|
var r result
|
|
|
|
r.proto, _, r.err = u.securityMuxer.Negotiate(insecure)
|
|
|
|
done <- r
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var r result
|
2024-05-15 23:15:00 +00:00
|
|
|
r.proto, r.err = mss.SelectOneOf(u.securityIDs, insecure)
|
2023-02-22 21:58:17 +00:00
|
|
|
done <- r
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case r := <-done:
|
|
|
|
if r.err != nil {
|
2024-05-15 23:15:00 +00:00
|
|
|
return nil, r.err
|
2023-02-22 21:58:17 +00:00
|
|
|
}
|
|
|
|
if s := u.getSecurityByID(r.proto); s != nil {
|
2024-05-15 23:15:00 +00:00
|
|
|
return s, nil
|
2023-02-22 21:58:17 +00:00
|
|
|
}
|
2024-05-15 23:15:00 +00:00
|
|
|
return nil, fmt.Errorf("selected unknown security transport: %s", r.proto)
|
2023-02-22 21:58:17 +00:00
|
|
|
case <-ctx.Done():
|
2024-05-15 23:15:00 +00:00
|
|
|
// We *must* do this. We have outstanding work on the connection, and it's no longer safe to use.
|
2023-02-22 21:58:17 +00:00
|
|
|
insecure.Close()
|
|
|
|
<-done // wait to stop using the connection.
|
2024-05-15 23:15:00 +00:00
|
|
|
return nil, ctx.Err()
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|