You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
3.6 KiB

5 years ago
package noise
import (
"bufio"
5 years ago
"context"
"net"
"sync"
"time"
"github.com/flynn/noise"
"github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
5 years ago
)
type secureSession struct {
initiator bool
checkPeerID bool
5 years ago
localID peer.ID
localKey crypto.PrivKey
remoteID peer.ID
remoteKey crypto.PubKey
5 years ago
readLock sync.Mutex
writeLock sync.Mutex
insecureConn net.Conn
insecureReader *bufio.Reader // to cushion io read syscalls
// we don't buffer writes to avoid introducing latency; optimisation possible. // TODO revisit
5 years ago
qseek int // queued bytes seek value.
qbuf []byte // queued bytes buffer.
rlen [2]byte // work buffer to read in the incoming message length.
enc *noise.CipherState
dec *noise.CipherState
// noise prologue
prologue []byte
initiatorEarlyDataHandler, responderEarlyDataHandler EarlyDataHandler
// ConnectionState holds state information releated to the secureSession entity.
connectionState network.ConnectionState
5 years ago
}
// newSecureSession creates a Noise session over the given insecureConn Conn, using
5 years ago
// the libp2p identity keypair from the given Transport.
func newSecureSession(tpt *Transport, ctx context.Context, insecure net.Conn, remote peer.ID, prologue []byte, initiatorEDH, responderEDH EarlyDataHandler, initiator, checkPeerID bool) (*secureSession, error) {
5 years ago
s := &secureSession{
insecureConn: insecure,
insecureReader: bufio.NewReader(insecure),
initiator: initiator,
localID: tpt.localID,
localKey: tpt.privateKey,
remoteID: remote,
prologue: prologue,
initiatorEarlyDataHandler: initiatorEDH,
responderEarlyDataHandler: responderEDH,
checkPeerID: checkPeerID,
5 years ago
}
// the go-routine we create to run the handshake will
// write the result of the handshake to the respCh.
respCh := make(chan error, 1)
go func() {
respCh <- s.runHandshake(ctx)
}()
select {
case err := <-respCh:
if err != nil {
_ = s.insecureConn.Close()
}
return s, err
case <-ctx.Done():
// If the context has been cancelled, we close the underlying connection.
// We then wait for the handshake to return because of the first error it encounters
// so we don't return without cleaning up the go-routine.
_ = s.insecureConn.Close()
<-respCh
return nil, ctx.Err()
}
5 years ago
}
func (s *secureSession) LocalAddr() net.Addr {
return s.insecureConn.LocalAddr()
5 years ago
}
func (s *secureSession) LocalPeer() peer.ID {
return s.localID
5 years ago
}
func (s *secureSession) LocalPublicKey() crypto.PubKey {
return s.localKey.GetPublic()
}
func (s *secureSession) RemoteAddr() net.Addr {
return s.insecureConn.RemoteAddr()
5 years ago
}
func (s *secureSession) RemotePeer() peer.ID {
return s.remoteID
5 years ago
}
func (s *secureSession) RemotePublicKey() crypto.PubKey {
return s.remoteKey
5 years ago
}
func (s *secureSession) ConnState() network.ConnectionState {
return s.connectionState
}
5 years ago
func (s *secureSession) SetDeadline(t time.Time) error {
return s.insecureConn.SetDeadline(t)
5 years ago
}
func (s *secureSession) SetReadDeadline(t time.Time) error {
return s.insecureConn.SetReadDeadline(t)
5 years ago
}
func (s *secureSession) SetWriteDeadline(t time.Time) error {
return s.insecureConn.SetWriteDeadline(t)
5 years ago
}
func (s *secureSession) Close() error {
return s.insecureConn.Close()
5 years ago
}
func SessionWithConnState(s *secureSession, muxer protocol.ID) *secureSession {
if s != nil {
s.connectionState.StreamMultiplexer = muxer
s.connectionState.UsedEarlyMuxerNegotiation = muxer != ""
}
return s
}