diff --git a/p2p/host/basic/basic_host.go b/p2p/host/basic/basic_host.go index 88e8c609c733e63f80f540837facd93557df21f4..ca0ce93da4a57da825d73ac53763d4705427062c 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,6 +98,12 @@ type HostOpts struct { // ConnManager is a libp2p connection manager ConnManager connmgr.ConnManager + + // Relay indicates whether the host should use circuit relay transport + Relay 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. @@ -143,10 +150,25 @@ func NewHost(net inet.Network, opts *HostOpts) *BasicHost { h.cmgr = opts.ConnManager } + var relayCtx context.Context + var relayCancel func() + if opts.Relay { + relayCtx, relayCancel = context.WithCancel(context.Background()) + err := circuit.AddRelayTransport(relayCtx, h, opts.RelayOpts...) + if err != nil { + // perhaps inappropriate, but otherwise we have to change the interface + // to return an error, which will nost likely lead to a fatality anyway + panic(err) + } + } + h.proc = goprocess.WithTeardown(func() error { if h.natmgr != nil { h.natmgr.Close() } + if relayCancel != nil { + relayCancel() + } return h.Network().Close() }) diff --git a/package.json b/package.json index ebc6c7ba5d6d04ee89be18af7b59b84b92b4364e..96b9c059b8e228d292c72dca17d23dda52e88e57 100644 --- a/package.json +++ b/package.json @@ -268,6 +268,12 @@ "hash": "QmXYjuNuxVzXKJCfWasQk1RqkhVLDM9jtUKhqc2WPQmFSB", "name": "go-libp2p-peer", "version": "2.2.0" + }, + { + "author": "vyzo", + "hash": "QmP8i7FhM3jjciE34U9MZTnyD21BpxTinTzTcmVkvfM6tv", + "name": "go-libp2p-circuit", + "version": "1.0.0" } ], "gxVersion": "0.4.0",