ssms.go 2.32 KB
Newer Older
Steven Allen's avatar
Steven Allen committed
1
package csms
Steven Allen's avatar
Steven Allen committed
2 3 4 5 6 7

import (
	"context"
	"fmt"
	"net"

Steven Allen's avatar
Steven Allen committed
8
	connsec "github.com/libp2p/go-conn-security"
Steven Allen's avatar
go fmt  
Steven Allen committed
9
	peer "github.com/libp2p/go-libp2p-peer"
Steven Allen's avatar
Steven Allen committed
10 11 12 13 14 15 16 17 18
	mss "github.com/multiformats/go-multistream"
)

// SSMuxer is a multistream stream security transport multiplexer.
//
// SSMuxer is safe to use without initialization. However, it's not safe to move
// after use.
type SSMuxer struct {
	mux             mss.MultistreamMuxer
Steven Allen's avatar
Steven Allen committed
19
	tpts            map[string]connsec.Transport
Steven Allen's avatar
Steven Allen committed
20 21 22
	OrderPreference []string
}

Steven Allen's avatar
Steven Allen committed
23
var _ connsec.Transport = (*SSMuxer)(nil)
Steven Allen's avatar
Steven Allen committed
24

Steven Allen's avatar
Steven Allen committed
25 26 27 28
// AddTransport adds a stream security transport to this multistream muxer.
//
// This method is *not* thread-safe. It should be called only when initializing
// the SSMuxer.
Steven Allen's avatar
Steven Allen committed
29
func (sm *SSMuxer) AddTransport(path string, transport connsec.Transport) {
Steven Allen's avatar
Steven Allen committed
30
	if sm.tpts == nil {
Steven Allen's avatar
Steven Allen committed
31
		sm.tpts = make(map[string]connsec.Transport, 1)
Steven Allen's avatar
Steven Allen committed
32 33 34 35 36 37 38 39 40
	}

	sm.mux.AddHandler(path, nil)
	sm.tpts[path] = transport
	sm.OrderPreference = append(sm.OrderPreference, path)
}

// SecureInbound secures an inbound connection using this multistream
// multiplexed stream security transport.
Steven Allen's avatar
Steven Allen committed
41
func (sm *SSMuxer) SecureInbound(ctx context.Context, insecure net.Conn) (connsec.Conn, error) {
Steven Allen's avatar
Steven Allen committed
42 43 44 45 46 47 48 49 50
	tpt, err := sm.selectProto(ctx, insecure, true)
	if err != nil {
		return nil, err
	}
	return tpt.SecureInbound(ctx, insecure)
}

// SecureOutbound secures an outbound connection using this multistream
// multiplexed stream security transport.
Steven Allen's avatar
Steven Allen committed
51
func (sm *SSMuxer) SecureOutbound(ctx context.Context, insecure net.Conn, p peer.ID) (connsec.Conn, error) {
Steven Allen's avatar
Steven Allen committed
52 53 54 55 56 57 58
	tpt, err := sm.selectProto(ctx, insecure, false)
	if err != nil {
		return nil, err
	}
	return tpt.SecureOutbound(ctx, insecure, p)
}

Steven Allen's avatar
Steven Allen committed
59
func (sm *SSMuxer) selectProto(ctx context.Context, insecure net.Conn, server bool) (connsec.Transport, error) {
Steven Allen's avatar
Steven Allen committed
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	var proto string
	var err error
	done := make(chan struct{})
	go func() {
		defer close(done)
		if server {
			proto, _, err = sm.mux.Negotiate(insecure)
		} else {
			proto, err = mss.SelectOneOf(sm.OrderPreference, insecure)
		}
	}()

	select {
	case <-done:
		if err != nil {
			return nil, err
		}
		if tpt, ok := sm.tpts[proto]; ok {
			return tpt, nil
		}
		return nil, fmt.Errorf("selected unknown security transport")
	case <-ctx.Done():
		// We *must* do this. We have outstanding work on the connection
		// and it's no longer safe to use.
		insecure.Close()
		return nil, ctx.Err()
	}
}