diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index 88e8c609c733e63f80f540837facd93557df21f4..a7992d0aa13c88c066c1265a1cf5b0f89f11eb7d 100644 --- a/p2p/host/basic/basic_host.go +++ b/p2p/host/basic/basic_host.go @@ -9,6 +9,7 @@ import ( logging "github.com/ipfs/go-log" goprocess "github.com/jbenet/goprocess" + circuit "github.com/libp2p/go-libp2p-circuit" connmgr "github.com/libp2p/go-libp2p-connmgr" metrics "github.com/libp2p/go-libp2p-metrics" mstream "github.com/libp2p/go-libp2p-metrics/stream" @@ -97,10 +98,16 @@ type HostOpts struct { // ConnManager is a libp2p connection manager ConnManager connmgr.ConnManager + + // Relay indicates whether the host should use circuit relay transport + EnableRelay bool + + // RelayOpts are options for the relay transport; only meaningful when Relay=true + RelayOpts []circuit.RelayOpt } // NewHost constructs a new *BasicHost and activates it by attaching its stream and connection handlers to the given inet.Network. -func NewHost(net inet.Network, opts *HostOpts) *BasicHost { +func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost, error) { h := &BasicHost{ network: net, mux: msmux.NewMultistreamMuxer(), @@ -143,17 +150,32 @@ func NewHost(net inet.Network, opts *HostOpts) *BasicHost { h.cmgr = opts.ConnManager } + var relayCtx context.Context + var relayCancel func() + h.proc = goprocess.WithTeardown(func() error { if h.natmgr != nil { h.natmgr.Close() } + if relayCancel != nil { + relayCancel() + } return h.Network().Close() }) net.SetConnHandler(h.newConnHandler) net.SetStreamHandler(h.newStreamHandler) - return h + if opts.EnableRelay { + relayCtx, relayCancel = context.WithCancel(ctx) + err := circuit.AddRelayTransport(relayCtx, h, opts.RelayOpts...) + if err != nil { + h.Close() + return nil, err + } + } + + return h, nil } // New constructs and sets up a new *BasicHost with given Network and options. @@ -178,7 +200,14 @@ func New(net inet.Network, opts ...interface{}) *BasicHost { } } - return NewHost(net, hostopts) + h, err := NewHost(context.Background(), net, hostopts) + if err != nil { + // this cannot happen with legacy options + // plus we want to keep the (deprecated) legacy interface unchanged + panic(err) + } + + return h } // newConnHandler is the remote-opened conn handler for inet.Network diff --git a/p2p/net/mock/mock_net.go b/p2p/net/mock/mock_net.go index 357c81031bb58841ee2e85d71cf23eb3d53b3b6a..898292af8eed49efa6f7a10defb6ab2f7770b405 100644 --- a/p2p/net/mock/mock_net.go +++ b/p2p/net/mock/mock_net.go @@ -87,7 +87,11 @@ func (mn *mocknet) AddPeerWithPeerstore(p peer.ID, ps pstore.Peerstore) (host.Ho opts := &bhost.HostOpts{ NegotiationTimeout: -1, } - h := bhost.NewHost(n, opts) + + h, err := bhost.NewHost(mn.ctx, n, opts) + if err != nil { + return nil, err + } mn.proc.AddChild(n.proc) diff --git a/package.json b/package.json index ebc6c7ba5d6d04ee89be18af7b59b84b92b4364e..e8fab7d13bfa2b4506f982a82dfe297fe674336e 100644 --- a/package.json +++ b/package.json @@ -268,6 +268,12 @@ "hash": "QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB", "name": "go-libp2p-peer", "version": "2.2.0" + }, + { + "author": "vyzo", + "hash": "QmYv8PxtikjeL6AfsheUiJfoFRzKvx8hdwRf4odBWH7mQx", + "name": "go-libp2p-circuit", + "version": "1.1.3" } ], "gxVersion": "0.4.0",