Skip to content

Commit 97696ac

Browse files
start a relay v2 by default, add config options
1 parent 8a70f70 commit 97696ac

5 files changed

Lines changed: 151 additions & 16 deletions

File tree

config/config.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/libp2p/go-libp2p/p2p/host/relay"
2222
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
2323
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
24+
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
2425
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
2526

2627
autonat "github.com/libp2p/go-libp2p-autonat"
@@ -74,7 +75,10 @@ type Config struct {
7475
PSK pnet.PSK
7576

7677
RelayCustom bool
77-
Relay bool
78+
Relay bool // should the relay transport be used
79+
80+
DisableRelayService bool // should we run a circuitv2 relay (if publicly reachable)
81+
RelayServiceOpts []relayv2.Option
7882

7983
ListenAddrs []ma.Multiaddr
8084
AddrsFactory bhost.AddrsFactory
@@ -196,6 +200,8 @@ func (cfg *Config) NewNode() (host.Host, error) {
196200
MultiaddrResolver: cfg.MultiaddrResolver,
197201
EnableHolePunching: cfg.EnableHolePunching,
198202
HolePunchingOptions: cfg.HolePunchingOptions,
203+
EnableRelayService: !cfg.DisableRelayService,
204+
RelayServiceOpts: cfg.RelayServiceOpts,
199205
})
200206
if err != nil {
201207
swrm.Close()

examples/pubsub/chat/go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,7 @@ github.com/libp2p/go-addr-util v0.1.0/go.mod h1:6I3ZYuFr2O/9D+SoyM0zEw0EF3YkldtT
404404
github.com/libp2p/go-buffer-pool v0.0.1/go.mod h1:xtyIz9PMobb13WaxR6Zo1Pd1zXJKYg0a8KiIvDp3TzQ=
405405
github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs=
406406
github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM=
407+
github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38yPW7c=
407408
github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic=
408409
github.com/libp2p/go-conn-security-multistream v0.2.0/go.mod h1:hZN4MjlNetKD3Rq5Jb/P5ohUnFLNzEAR4DLSzpn2QLU=
409410
github.com/libp2p/go-conn-security-multistream v0.2.1/go.mod h1:cR1d8gA0Hr59Fj6NhaTpFhJZrjSYuNmhpT2r25zYR70=
@@ -414,6 +415,7 @@ github.com/libp2p/go-eventbus v0.2.1/go.mod h1:jc2S4SoEVPP48H9Wpzm5aiGwUCBMfGhVh
414415
github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8=
415416
github.com/libp2p/go-flow-metrics v0.0.3 h1:8tAs/hSdNvUiLgtlSy3mxwxWP4I9y/jlkPFT7epKdeM=
416417
github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs=
418+
github.com/libp2p/go-libp2p-asn-util v0.0.0-20210818120414-1f382a4aa43a h1:6yEuCOY31elgeJ2KA2JiREZjIznvH6lOWCdHRuhgEgc=
417419
github.com/libp2p/go-libp2p-asn-util v0.0.0-20210818120414-1f382a4aa43a/go.mod h1:wu+AnM9Ii2KgO5jMmS1rz9dvzTdj8BXqsPR9HR0XB7I=
418420
github.com/libp2p/go-libp2p-autonat v0.5.0 h1:/+3+4NcQV47DQ/duvRyFDP8oxv6CQTvSKYD5iWoPcYs=
419421
github.com/libp2p/go-libp2p-autonat v0.5.0/go.mod h1:085tmmuXn0nXgFwuF7a2tt4UxgTjuapbuml27v4htKY=

options.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ import (
1919
"github.com/libp2p/go-libp2p/config"
2020
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
2121
autorelay "github.com/libp2p/go-libp2p/p2p/host/relay"
22-
holepunch "github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
22+
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
23+
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
2324

2425
ma "github.com/multiformats/go-multiaddr"
2526
madns "github.com/multiformats/go-multiaddr-dns"
@@ -231,6 +232,26 @@ func DisableRelay() Option {
231232
}
232233
}
233234

235+
// DisableRelayService configures libp2p to not run a circuit v2 relay.
236+
// Note that we only start the relay if we detect that we're publicly reachable.
237+
func DisableRelayService() Option {
238+
return func(cfg *Config) error {
239+
cfg.DisableRelayService = true
240+
cfg.RelayServiceOpts = nil
241+
return nil
242+
}
243+
}
244+
245+
// EnableRelayService configures libp2p to run a circuit v2 relay,
246+
// if we dected that we're publicly reachable.
247+
func EnableRelayService(opts ...relayv2.Option) Option {
248+
return func(cfg *Config) error {
249+
cfg.DisableRelayService = false
250+
cfg.RelayServiceOpts = opts
251+
return nil
252+
}
253+
}
254+
234255
// EnableAutoRelay configures libp2p to enable the AutoRelay subsystem.
235256
//
236257
// Dependencies:

p2p/host/basic/basic_host.go

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

p2p/host/relayv2/relay.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package relayv2
2+
3+
import (
4+
"context"
5+
"sync"
6+
7+
relayv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
8+
9+
"github.com/libp2p/go-libp2p-core/event"
10+
"github.com/libp2p/go-libp2p-core/host"
11+
"github.com/libp2p/go-libp2p-core/network"
12+
)
13+
14+
type RelayManager struct {
15+
host host.Host
16+
17+
mutex sync.Mutex
18+
relay *relayv2.Relay
19+
opts []relayv2.Option
20+
21+
refCount sync.WaitGroup
22+
ctxCancel context.CancelFunc
23+
}
24+
25+
func NewRelayManager(host host.Host, opts ...relayv2.Option) *RelayManager {
26+
ctx, cancel := context.WithCancel(context.Background())
27+
m := &RelayManager{
28+
host: host,
29+
opts: opts,
30+
ctxCancel: cancel,
31+
}
32+
m.refCount.Add(1)
33+
go m.background(ctx)
34+
return m
35+
}
36+
37+
func (m *RelayManager) background(ctx context.Context) {
38+
defer m.refCount.Done()
39+
defer func() {
40+
m.mutex.Lock()
41+
if m.relay != nil {
42+
m.relay.Close()
43+
}
44+
m.mutex.Unlock()
45+
}()
46+
47+
subReachability, _ := m.host.EventBus().Subscribe(new(event.EvtLocalReachabilityChanged))
48+
defer subReachability.Close()
49+
50+
for {
51+
select {
52+
case <-ctx.Done():
53+
return
54+
case ev, ok := <-subReachability.Out():
55+
if !ok {
56+
return
57+
}
58+
if err := m.reachabilityChanged(ev.(event.EvtLocalReachabilityChanged).Reachability); err != nil {
59+
return
60+
}
61+
}
62+
}
63+
}
64+
65+
func (m *RelayManager) reachabilityChanged(r network.Reachability) error {
66+
switch r {
67+
case network.ReachabilityPublic:
68+
relay, err := relayv2.New(m.host, m.opts...)
69+
if err != nil {
70+
return err
71+
}
72+
m.mutex.Lock()
73+
defer m.mutex.Unlock()
74+
m.relay = relay
75+
case network.ReachabilityPrivate:
76+
m.mutex.Lock()
77+
defer m.mutex.Unlock()
78+
if m.relay != nil {
79+
err := m.relay.Close()
80+
m.relay = nil
81+
return err
82+
}
83+
}
84+
return nil
85+
}
86+
87+
func (m *RelayManager) Close() error {
88+
m.ctxCancel()
89+
m.refCount.Wait()
90+
return nil
91+
}

0 commit comments

Comments
 (0)