Browse Source

noise: add documentation for the Prologue option

pull/1728/head
Marten Seemann 2 years ago
parent
commit
40a0d93e29
  1. 27
      p2p/security/noise/session_transport.go
  2. 10
      p2p/security/noise/transport.go

27
p2p/security/noise/session_transport.go

@ -12,6 +12,16 @@ import (
type SessionOption = func(*SessionTransport) error type SessionOption = func(*SessionTransport) error
// Prologue sets a prologue for the Noise session.
// The handshake will only complete successfully if both parties set the same prologue.
// See https://noiseprotocol.org/noise.html#prologue for details.
func Prologue(prologue []byte) SessionOption {
return func(s *SessionTransport) error {
s.prologue = prologue
return nil
}
}
var _ sec.SecureTransport = &SessionTransport{} var _ sec.SecureTransport = &SessionTransport{}
// SessionTransport can be used // SessionTransport can be used
@ -39,20 +49,3 @@ func (i *SessionTransport) SecureInbound(ctx context.Context, insecure net.Conn,
func (i *SessionTransport) SecureOutbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, error) { 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, true) return newSecureSession(i.t, ctx, insecure, p, i.prologue, true)
} }
func (t *Transport) WithSessionOptions(opts ...SessionOption) (sec.SecureTransport, error) {
st := &SessionTransport{t: t}
for _, opt := range opts {
if err := opt(st); err != nil {
return nil, err
}
}
return st, nil
}
func Prologue(prologue []byte) SessionOption {
return func(s *SessionTransport) error {
s.prologue = prologue
return nil
}
}

10
p2p/security/noise/transport.go

@ -55,3 +55,13 @@ func (t *Transport) SecureInbound(ctx context.Context, insecure net.Conn, p peer
func (t *Transport) SecureOutbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, error) { func (t *Transport) SecureOutbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, error) {
return newSecureSession(t, ctx, insecure, p, nil, true) return newSecureSession(t, ctx, insecure, p, nil, true)
} }
func (t *Transport) WithSessionOptions(opts ...SessionOption) (sec.SecureTransport, error) {
st := &SessionTransport{t: t}
for _, opt := range opts {
if err := opt(st); err != nil {
return nil, err
}
}
return st, nil
}

Loading…
Cancel
Save