package noise
import (
"context"
"net"
"github.com/libp2p/go-libp2p/core/canonicallog"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/core/sec"
"github.com/libp2p/go-libp2p/p2p/security/noise/pb"
manet "github.com/multiformats/go-multiaddr/net"
)
type SessionOption = func (*SessionTransport ) error
func Prologue (prologue []byte ) SessionOption {
return func (s *SessionTransport ) error {
s .prologue = prologue
return nil
}
}
type EarlyDataHandler interface {
Send (context .Context , net .Conn , peer .ID ) *pb .NoiseExtensions
Received (context .Context , net .Conn , *pb .NoiseExtensions ) error
}
func EarlyData (initiator , responder EarlyDataHandler ) SessionOption {
return func (s *SessionTransport ) error {
s .initiatorEarlyDataHandler = initiator
s .responderEarlyDataHandler = responder
return nil
}
}
func DisablePeerIDCheck () SessionOption {
return func (s *SessionTransport ) error {
s .disablePeerIDCheck = true
return nil
}
}
var _ sec .SecureTransport = &SessionTransport {}
type SessionTransport struct {
t *Transport
prologue []byte
disablePeerIDCheck bool
protocolID protocol .ID
initiatorEarlyDataHandler, responderEarlyDataHandler EarlyDataHandler
}
func (i *SessionTransport ) SecureInbound (ctx context .Context , insecure net .Conn , p peer .ID ) (sec .SecureConn , error ) {
checkPeerID := !i .disablePeerIDCheck && p != ""
c , err := newSecureSession (i .t , ctx , insecure , p , i .prologue , i .initiatorEarlyDataHandler , i .responderEarlyDataHandler , false , checkPeerID )
if err != nil {
addr , maErr := manet .FromNetAddr (insecure .RemoteAddr ())
if maErr == nil {
canonicallog .LogPeerStatus (100 , p , addr , "handshake_failure" , "noise" , "err" , err .Error())
}
}
return c , err
}
func (i *SessionTransport ) SecureOutbound (ctx context .Context , insecure net .Conn , p peer .ID ) (sec .SecureConn , error ) {
return newSecureSession (i .t , ctx , insecure , p , i .prologue , i .initiatorEarlyDataHandler , i .responderEarlyDataHandler , true , !i .disablePeerIDCheck )
}
func (i *SessionTransport ) ID () protocol .ID {
return i .protocolID
}
The pages are generated with Golds v0.8.2 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .