Skip to content

Commit

Permalink
use the new SecureTransport and SecureMuxer interfaces (#36)
Browse files Browse the repository at this point in the history
* use the new SecureTransport and SecureMuxer interfaces

* restore sanity check for the remote peer's ID, add log statement
  • Loading branch information
marten-seemann authored Sep 8, 2021
1 parent d0442a2 commit b63c8ff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
8 changes: 5 additions & 3 deletions p2p/net/conn-security-multistream/ssms.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package csms
import (
"context"
"fmt"
"log"
"net"

"github.com/libp2p/go-libp2p-core/peer"
Expand Down Expand Up @@ -38,12 +39,12 @@ func (sm *SSMuxer) AddTransport(path string, transport sec.SecureTransport) {

// SecureInbound secures an inbound connection using this multistream
// multiplexed stream security transport.
func (sm *SSMuxer) SecureInbound(ctx context.Context, insecure net.Conn) (sec.SecureConn, bool, error) {
func (sm *SSMuxer) SecureInbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, bool, error) {
tpt, _, err := sm.selectProto(ctx, insecure, true)
if err != nil {
return nil, false, err
}
sconn, err := tpt.SecureInbound(ctx, insecure)
sconn, err := tpt.SecureInbound(ctx, insecure, p)
return sconn, true, err
}

Expand All @@ -57,13 +58,14 @@ func (sm *SSMuxer) SecureOutbound(ctx context.Context, insecure net.Conn, p peer

var sconn sec.SecureConn
if server {
sconn, err = tpt.SecureInbound(ctx, insecure)
sconn, err = tpt.SecureInbound(ctx, insecure, p)
if err != nil {
return nil, false, fmt.Errorf("failed to secure inbound connection: %s", err)
}
// ensure the correct peer connected to us
if sconn.RemotePeer() != p {
sconn.Close()
log.Printf("Handshake failed to properly authenticate peer. Authenticated %s, expected %s.", sconn.RemotePeer(), p)
return nil, false, fmt.Errorf("unexpected peer")
}
} else {
Expand Down
8 changes: 4 additions & 4 deletions p2p/net/conn-security-multistream/ssms_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ type TransportAdapter struct {
mux *SSMuxer
}

func (sm *TransportAdapter) SecureInbound(ctx context.Context, insecure net.Conn) (sec.SecureConn, error) {
sconn, _, err := sm.mux.SecureInbound(ctx, insecure)
func (sm *TransportAdapter) SecureInbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, error) {
sconn, _, err := sm.mux.SecureInbound(ctx, insecure, p)
return sconn, err
}

Expand Down Expand Up @@ -61,9 +61,9 @@ func TestNoCommonProto(t *testing.T) {
go func() {
defer wg.Done()
defer a.Close()
_, _, err := at.SecureInbound(ctx, a)
_, _, err := at.SecureInbound(ctx, a, "")
if err == nil {
t.Error("conection should have failed")
t.Error("connection should have failed")
}
}()

Expand Down

0 comments on commit b63c8ff

Please sign in to comment.