-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow adding prologue to noise connections #1663
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
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/sec" | ||
manet "github.com/multiformats/go-multiaddr/net" | ||
) | ||
|
||
type NoiseOptions struct { | ||
Prologue []byte | ||
} | ||
|
||
var _ sec.SecureTransport = &intermediateTransport{} | ||
|
||
// intermediate transport can be used | ||
// to provide per-connection options | ||
type intermediateTransport struct { | ||
t *Transport | ||
// options | ||
prologue []byte | ||
} | ||
|
||
// SecureInbound runs the Noise handshake as the responder. | ||
// If p is empty, connections from any peer are accepted. | ||
func (i *intermediateTransport) SecureInbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, error) { | ||
c, err := newSecureSession(i.t, ctx, insecure, p, i.prologue, false) | ||
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 | ||
} | ||
|
||
// SecureOutbound runs the Noise handshake as the initiator. | ||
func (i *intermediateTransport) SecureOutbound(ctx context.Context, insecure net.Conn, p peer.ID) (sec.SecureConn, error) { | ||
return newSecureSession(i.t, ctx, insecure, p, i.prologue, true) | ||
} | ||
|
||
func (t *Transport) WithNoiseOptions(opts NoiseOptions) sec.SecureTransport { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why can't this be a normal constructor option? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is because the noise transport has both transport level and session level options. eg. The private key could be shared among multiple different connections, but the prologue can be unique to each connection. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can rename this to |
||
return &intermediateTransport{ | ||
t: t, | ||
prologue: opts.Prologue, | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid config structs and use the normal config function pattern.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I considered that but want to avoid exposing the intermediate transport.