@@ -9,7 +9,12 @@ import (
99 "sync"
1010 "time"
1111
12- autonat "github.com/libp2p/go-libp2p-autonat"
12+ relayservice "github.com/libp2p/go-libp2p/p2p/host/relayv2"
13+ relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
14+ "github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
15+ "github.com/libp2p/go-libp2p/p2p/protocol/identify"
16+ "github.com/libp2p/go-libp2p/p2p/protocol/ping"
17+
1318 "github.com/libp2p/go-libp2p-core/connmgr"
1419 "github.com/libp2p/go-libp2p-core/crypto"
1520 "github.com/libp2p/go-libp2p-core/event"
@@ -19,13 +24,11 @@ import (
1924 "github.com/libp2p/go-libp2p-core/peerstore"
2025 "github.com/libp2p/go-libp2p-core/protocol"
2126 "github.com/libp2p/go-libp2p-core/record"
22- "github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
2327
2428 addrutil "github.com/libp2p/go-addr-util"
2529 "github.com/libp2p/go-eventbus"
30+ autonat "github.com/libp2p/go-libp2p-autonat"
2631 inat "github.com/libp2p/go-libp2p-nat"
27- "github.com/libp2p/go-libp2p/p2p/protocol/identify"
28- "github.com/libp2p/go-libp2p/p2p/protocol/ping"
2932 "github.com/libp2p/go-netroute"
3033
3134 logging "github.com/ipfs/go-log/v2"
@@ -70,15 +73,16 @@ type BasicHost struct {
7073 // keep track of resources we need to wait on before shutting down
7174 refCount sync.WaitGroup
7275
73- network network.Network
74- mux * msmux.MultistreamMuxer
75- ids * identify.IDService
76- hps * holepunch.Service
77- pings * ping.PingService
78- natmgr NATManager
79- maResolver * madns.Resolver
80- cmgr connmgr.ConnManager
81- eventbus event.Bus
76+ network network.Network
77+ mux * msmux.MultistreamMuxer
78+ ids * identify.IDService
79+ hps * holepunch.Service
80+ pings * ping.PingService
81+ natmgr NATManager
82+ maResolver * madns.Resolver
83+ cmgr connmgr.ConnManager
84+ eventbus event.Bus
85+ relayManager * relayservice.RelayManager
8286
8387 AddrsFactory AddrsFactory
8488
@@ -133,6 +137,11 @@ type HostOpts struct {
133137 // EnablePing indicates whether to instantiate the ping service
134138 EnablePing bool
135139
140+ // EnableRelayService enables the circuit v2 relay (if we're publicly reachable).
141+ EnableRelayService bool
142+ // RelayServiceOpts are options for the circuit v2 relay.
143+ RelayServiceOpts []relayv2.Option
144+
136145 // UserAgent sets the user-agent for the host.
137146 UserAgent string
138147
@@ -245,6 +254,10 @@ func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
245254 n .Notify (h .cmgr .Notifee ())
246255 }
247256
257+ if opts .EnableRelayService {
258+ h .relayManager = relayservice .NewRelayManager (h , opts .RelayServiceOpts ... )
259+ }
260+
248261 if opts .EnablePing {
249262 h .pings = ping .NewPingService (h )
250263 }
@@ -1007,7 +1020,9 @@ func (h *BasicHost) Close() error {
10071020 if h .autoNat != nil {
10081021 h .autoNat .Close ()
10091022 }
1010-
1023+ if h .relayManager != nil {
1024+ h .relayManager .Close ()
1025+ }
10111026 if h .hps != nil {
10121027 h .hps .Close ()
10131028 }
0 commit comments