2018-07-04 10:51:47 +00:00
|
|
|
package csms
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p-core/sec"
|
2018-07-04 10:51:47 +00:00
|
|
|
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
|
2019-06-09 07:24:20 +00:00
|
|
|
tpts map[string]sec.SecureTransport
|
2018-07-04 10:51:47 +00:00
|
|
|
OrderPreference []string
|
|
|
|
}
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
var _ sec.SecureMuxer = (*SSMuxer)(nil)
|
2018-07-04 10:51:47 +00:00
|
|
|
|
|
|
|
// 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.
|
2019-06-09 07:24:20 +00:00
|
|
|
func (sm *SSMuxer) AddTransport(path string, transport sec.SecureTransport) {
|
2018-07-04 10:51:47 +00:00
|
|
|
if sm.tpts == nil {
|
2019-06-09 07:24:20 +00:00
|
|
|
sm.tpts = make(map[string]sec.SecureTransport, 1)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2021-10-19 13:43:41 +00:00
|
|
|
func (sm *SSMuxer) SecureInbound(ctx context.Context, insecure net.Conn) (sec.SecureConn, bool, error) {
|
|
|
|
tpt, _, err := sm.selectProto(ctx, insecure, true)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
2021-10-19 13:43:41 +00:00
|
|
|
return nil, false, err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2021-10-19 13:43:41 +00:00
|
|
|
sconn, err := tpt.SecureInbound(ctx, insecure)
|
|
|
|
return sconn, true, err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SecureOutbound secures an outbound connection using this multistream
|
|
|
|
// multiplexed stream security transport.
|
2021-10-19 13:43:41 +00:00
|
|
|
func (sm *SSMuxer) SecureOutbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, bool, error) {
|
|
|
|
tpt, server, err := sm.selectProto(ctx, insecure, false)
|
2018-07-04 10:51:47 +00:00
|
|
|
if err != nil {
|
2021-10-19 13:43:41 +00:00
|
|
|
return nil, false, err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2021-10-19 13:43:41 +00:00
|
|
|
|
|
|
|
var sconn sec.SecureConn
|
|
|
|
if server {
|
|
|
|
sconn, err = tpt.SecureInbound(ctx, insecure)
|
|
|
|
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()
|
|
|
|
return nil, false, fmt.Errorf("Unexpected peer")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
sconn, err = tpt.SecureOutbound(ctx, insecure, p)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sconn, server, err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
|
2021-10-19 13:43:41 +00:00
|
|
|
func (sm *SSMuxer) selectProto(ctx context.Context, insecure net.Conn, server bool) (sec.SecureTransport, bool, error) {
|
2018-07-04 10:51:47 +00:00
|
|
|
var proto string
|
|
|
|
var err error
|
2021-10-19 13:43:41 +00:00
|
|
|
var iamserver bool
|
2018-07-04 10:51:47 +00:00
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(done)
|
|
|
|
if server {
|
2021-10-19 13:43:41 +00:00
|
|
|
iamserver = true
|
2018-07-04 10:51:47 +00:00
|
|
|
proto, _, err = sm.mux.Negotiate(insecure)
|
|
|
|
} else {
|
2021-10-19 13:43:41 +00:00
|
|
|
proto, iamserver, err = mss.SelectWithSimopenOrFail(sm.OrderPreference, insecure)
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
if err != nil {
|
2021-10-19 13:43:41 +00:00
|
|
|
return nil, false, err
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
if tpt, ok := sm.tpts[proto]; ok {
|
2021-10-19 13:43:41 +00:00
|
|
|
return tpt, iamserver, nil
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
2021-10-19 13:43:41 +00:00
|
|
|
return nil, false, fmt.Errorf("selected unknown security transport")
|
2018-07-04 10:51:47 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
// We *must* do this. We have outstanding work on the connection
|
|
|
|
// and it's no longer safe to use.
|
|
|
|
insecure.Close()
|
2019-06-09 07:24:20 +00:00
|
|
|
<-done // wait to stop using the connection.
|
2021-10-19 13:43:41 +00:00
|
|
|
return nil, false, ctx.Err()
|
2018-07-04 10:51:47 +00:00
|
|
|
}
|
|
|
|
}
|