mirror of
https://github.com/libp2p/go-libp2p.git
synced 2025-09-26 20:21:26 +08:00
upgrader: drop support for multistream simultaneous open (#2557)
* upgrader: drop support for multistream simultaneous open * upgrader: simplify client / server logic * update go-multistream to v0.5.0
This commit is contained in:
2
go.mod
2
go.mod
@@ -42,7 +42,7 @@ require (
|
||||
github.com/multiformats/go-multibase v0.2.0
|
||||
github.com/multiformats/go-multicodec v0.9.0
|
||||
github.com/multiformats/go-multihash v0.2.3
|
||||
github.com/multiformats/go-multistream v0.4.1
|
||||
github.com/multiformats/go-multistream v0.5.0
|
||||
github.com/multiformats/go-varint v0.0.7
|
||||
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58
|
||||
github.com/prometheus/client_golang v1.14.0
|
||||
|
4
go.sum
4
go.sum
@@ -366,8 +366,8 @@ github.com/multiformats/go-multicodec v0.9.0/go.mod h1:L3QTQvMIaVBkXOXXtVmYE+LI1
|
||||
github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
|
||||
github.com/multiformats/go-multihash v0.2.3 h1:7Lyc8XfX/IY2jWb/gI7JP+o7JEq9hOa7BFvVU9RSh+U=
|
||||
github.com/multiformats/go-multihash v0.2.3/go.mod h1:dXgKXCXjBzdscBLk9JkjINiEsCKRVch90MdaGiKsvSM=
|
||||
github.com/multiformats/go-multistream v0.4.1 h1:rFy0Iiyn3YT0asivDUIR05leAdwZq3de4741sbiSdfo=
|
||||
github.com/multiformats/go-multistream v0.4.1/go.mod h1:Mz5eykRVAjJWckE2U78c6xqdtyNUEhKSM0Lwar2p77Q=
|
||||
github.com/multiformats/go-multistream v0.5.0 h1:5htLSLl7lvJk3xx3qT/8Zm9J4K8vEOf/QGkvOGQAyiE=
|
||||
github.com/multiformats/go-multistream v0.5.0/go.mod h1:n6tMZiwiP2wUsR8DgfDWw1dydlEqV3l6N3/GBsX6ILA=
|
||||
github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
|
||||
github.com/multiformats/go-varint v0.0.7 h1:sWSGR+f/eu5ABZA2ZpYKBILXTTs9JWpdEM/nEGOHFS8=
|
||||
github.com/multiformats/go-varint v0.0.7/go.mod h1:r8PUYw/fD/SjBCiKOoDlGF6QawOELpZAu9eioSos/OU=
|
||||
|
@@ -152,7 +152,8 @@ func (u *upgrader) upgrade(ctx context.Context, t transport.Transport, maconn ma
|
||||
return nil, ipnet.ErrNotInPrivateNetwork
|
||||
}
|
||||
|
||||
sconn, security, server, err := u.setupSecurity(ctx, conn, p, dir)
|
||||
isServer := dir == network.DirInbound
|
||||
sconn, security, err := u.setupSecurity(ctx, conn, p, isServer)
|
||||
if err != nil {
|
||||
conn.Close()
|
||||
return nil, fmt.Errorf("failed to negotiate security protocol: %w", err)
|
||||
@@ -179,7 +180,7 @@ func (u *upgrader) upgrade(ctx context.Context, t transport.Transport, maconn ma
|
||||
}
|
||||
}
|
||||
|
||||
muxer, smconn, err := u.setupMuxer(ctx, sconn, server, connScope.PeerScope())
|
||||
muxer, smconn, err := u.setupMuxer(ctx, sconn, isServer, connScope.PeerScope())
|
||||
if err != nil {
|
||||
sconn.Close()
|
||||
return nil, fmt.Errorf("failed to negotiate stream multiplexer: %w", err)
|
||||
@@ -199,20 +200,17 @@ func (u *upgrader) upgrade(ctx context.Context, t transport.Transport, maconn ma
|
||||
return tc, nil
|
||||
}
|
||||
|
||||
func (u *upgrader) setupSecurity(ctx context.Context, conn net.Conn, p peer.ID, dir network.Direction) (sec.SecureConn, protocol.ID, bool, error) {
|
||||
isServer := dir == network.DirInbound
|
||||
var st sec.SecureTransport
|
||||
var err error
|
||||
st, isServer, err = u.negotiateSecurity(ctx, conn, isServer)
|
||||
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)
|
||||
if err != nil {
|
||||
return nil, "", false, err
|
||||
return nil, "", err
|
||||
}
|
||||
if isServer {
|
||||
sconn, err := st.SecureInbound(ctx, conn, p)
|
||||
return sconn, st.ID(), true, err
|
||||
return sconn, st.ID(), err
|
||||
}
|
||||
sconn, err := st.SecureOutbound(ctx, conn, p)
|
||||
return sconn, st.ID(), false, err
|
||||
return sconn, st.ID(), err
|
||||
}
|
||||
|
||||
func (u *upgrader) negotiateMuxer(nc net.Conn, isServer bool) (*StreamMuxer, error) {
|
||||
@@ -308,41 +306,38 @@ func (u *upgrader) getSecurityByID(id protocol.ID) sec.SecureTransport {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *upgrader) negotiateSecurity(ctx context.Context, insecure net.Conn, server bool) (sec.SecureTransport, bool, error) {
|
||||
func (u *upgrader) negotiateSecurity(ctx context.Context, insecure net.Conn, server bool) (sec.SecureTransport, error) {
|
||||
type result struct {
|
||||
proto protocol.ID
|
||||
iamserver bool
|
||||
err error
|
||||
proto protocol.ID
|
||||
err error
|
||||
}
|
||||
|
||||
done := make(chan result, 1)
|
||||
go func() {
|
||||
if server {
|
||||
var r result
|
||||
r.iamserver = true
|
||||
r.proto, _, r.err = u.securityMuxer.Negotiate(insecure)
|
||||
done <- r
|
||||
return
|
||||
}
|
||||
var r result
|
||||
r.proto, r.iamserver, r.err = mss.SelectWithSimopenOrFail(u.securityIDs, insecure)
|
||||
r.proto, r.err = mss.SelectOneOf(u.securityIDs, insecure)
|
||||
done <- r
|
||||
}()
|
||||
|
||||
select {
|
||||
case r := <-done:
|
||||
if r.err != nil {
|
||||
return nil, false, r.err
|
||||
return nil, r.err
|
||||
}
|
||||
if s := u.getSecurityByID(r.proto); s != nil {
|
||||
return s, r.iamserver, nil
|
||||
return s, nil
|
||||
}
|
||||
return nil, false, fmt.Errorf("selected unknown security transport: %s", r.proto)
|
||||
return nil, fmt.Errorf("selected unknown security transport: %s", r.proto)
|
||||
case <-ctx.Done():
|
||||
// We *must* do this. We have outstanding work on the connection
|
||||
// and it's no longer safe to use.
|
||||
// We *must* do this. We have outstanding work on the connection, and it's no longer safe to use.
|
||||
insecure.Close()
|
||||
<-done // wait to stop using the connection.
|
||||
return nil, false, ctx.Err()
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
@@ -61,7 +61,7 @@ require (
|
||||
github.com/multiformats/go-multibase v0.2.0 // indirect
|
||||
github.com/multiformats/go-multicodec v0.9.0 // indirect
|
||||
github.com/multiformats/go-multihash v0.2.3 // indirect
|
||||
github.com/multiformats/go-multistream v0.4.1 // indirect
|
||||
github.com/multiformats/go-multistream v0.5.0 // indirect
|
||||
github.com/multiformats/go-varint v0.0.7 // indirect
|
||||
github.com/onsi/ginkgo/v2 v2.11.0 // indirect
|
||||
github.com/opencontainers/runtime-spec v1.1.0 // indirect
|
||||
|
@@ -200,8 +200,8 @@ github.com/multiformats/go-multicodec v0.9.0/go.mod h1:L3QTQvMIaVBkXOXXtVmYE+LI1
|
||||
github.com/multiformats/go-multihash v0.0.8/go.mod h1:YSLudS+Pi8NHE7o6tb3D8vrpKa63epEDmG8nTduyAew=
|
||||
github.com/multiformats/go-multihash v0.2.3 h1:7Lyc8XfX/IY2jWb/gI7JP+o7JEq9hOa7BFvVU9RSh+U=
|
||||
github.com/multiformats/go-multihash v0.2.3/go.mod h1:dXgKXCXjBzdscBLk9JkjINiEsCKRVch90MdaGiKsvSM=
|
||||
github.com/multiformats/go-multistream v0.4.1 h1:rFy0Iiyn3YT0asivDUIR05leAdwZq3de4741sbiSdfo=
|
||||
github.com/multiformats/go-multistream v0.4.1/go.mod h1:Mz5eykRVAjJWckE2U78c6xqdtyNUEhKSM0Lwar2p77Q=
|
||||
github.com/multiformats/go-multistream v0.5.0 h1:5htLSLl7lvJk3xx3qT/8Zm9J4K8vEOf/QGkvOGQAyiE=
|
||||
github.com/multiformats/go-multistream v0.5.0/go.mod h1:n6tMZiwiP2wUsR8DgfDWw1dydlEqV3l6N3/GBsX6ILA=
|
||||
github.com/multiformats/go-varint v0.0.1/go.mod h1:3Ls8CIEsrijN6+B7PbrXRPxHRPuXSrVKRY101jdMZYE=
|
||||
github.com/multiformats/go-varint v0.0.7 h1:sWSGR+f/eu5ABZA2ZpYKBILXTTs9JWpdEM/nEGOHFS8=
|
||||
github.com/multiformats/go-varint v0.0.7/go.mod h1:r8PUYw/fD/SjBCiKOoDlGF6QawOELpZAu9eioSos/OU=
|
||||
|
Reference in New Issue
Block a user