Skip to content

Commit

Permalink
start a relay v2 by default, add config options
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 23, 2021
1 parent 8a70f70 commit 71eb4bf
Show file tree
Hide file tree
Showing 5 changed files with 154 additions and 16 deletions.
8 changes: 7 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/libp2p/go-libp2p/p2p/host/relay"
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
v2relay "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"

autonat "github.com/libp2p/go-libp2p-autonat"
Expand Down Expand Up @@ -74,7 +75,10 @@ type Config struct {
PSK pnet.PSK

RelayCustom bool
Relay bool
Relay bool // should the relay transport be used

DisableRelay bool // should we run a circuitv2 relay (if publicly reachable)
RelayOpts []v2relay.Option

ListenAddrs []ma.Multiaddr
AddrsFactory bhost.AddrsFactory
Expand Down Expand Up @@ -196,6 +200,8 @@ func (cfg *Config) NewNode() (host.Host, error) {
MultiaddrResolver: cfg.MultiaddrResolver,
EnableHolePunching: cfg.EnableHolePunching,
HolePunchingOptions: cfg.HolePunchingOptions,
EnableV2Relay: !cfg.DisableRelay,
RelayOpts: cfg.RelayOpts,
})
if err != nil {
swrm.Close()
Expand Down
2 changes: 2 additions & 0 deletions examples/pubsub/chat/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ github.com/libp2p/go-addr-util v0.1.0/go.mod h1:6I3ZYuFr2O/9D+SoyM0zEw0EF3YkldtT
github.com/libp2p/go-buffer-pool v0.0.1/go.mod h1:xtyIz9PMobb13WaxR6Zo1Pd1zXJKYg0a8KiIvDp3TzQ=
github.com/libp2p/go-buffer-pool v0.0.2 h1:QNK2iAFa8gjAe1SPz6mHSMuCcjs+X1wlHzeOSqcmlfs=
github.com/libp2p/go-buffer-pool v0.0.2/go.mod h1:MvaB6xw5vOrDl8rYZGLFdKAuk/hRoRZd1Vi32+RXyFM=
github.com/libp2p/go-cidranger v1.1.0 h1:ewPN8EZ0dd1LSnrtuwd4709PXVcITVeuwbag38yPW7c=
github.com/libp2p/go-cidranger v1.1.0/go.mod h1:KWZTfSr+r9qEo9OkI9/SIEeAtw+NNoU0dXIXt15Okic=
github.com/libp2p/go-conn-security-multistream v0.2.0/go.mod h1:hZN4MjlNetKD3Rq5Jb/P5ohUnFLNzEAR4DLSzpn2QLU=
github.com/libp2p/go-conn-security-multistream v0.2.1/go.mod h1:cR1d8gA0Hr59Fj6NhaTpFhJZrjSYuNmhpT2r25zYR70=
Expand All @@ -414,6 +415,7 @@ github.com/libp2p/go-eventbus v0.2.1/go.mod h1:jc2S4SoEVPP48H9Wpzm5aiGwUCBMfGhVh
github.com/libp2p/go-flow-metrics v0.0.1/go.mod h1:Iv1GH0sG8DtYN3SVJ2eG221wMiNpZxBdp967ls1g+k8=
github.com/libp2p/go-flow-metrics v0.0.3 h1:8tAs/hSdNvUiLgtlSy3mxwxWP4I9y/jlkPFT7epKdeM=
github.com/libp2p/go-flow-metrics v0.0.3/go.mod h1:HeoSNUrOJVK1jEpDqVEiUOIXqhbnS27omG0uWU5slZs=
github.com/libp2p/go-libp2p-asn-util v0.0.0-20210818120414-1f382a4aa43a h1:6yEuCOY31elgeJ2KA2JiREZjIznvH6lOWCdHRuhgEgc=
github.com/libp2p/go-libp2p-asn-util v0.0.0-20210818120414-1f382a4aa43a/go.mod h1:wu+AnM9Ii2KgO5jMmS1rz9dvzTdj8BXqsPR9HR0XB7I=
github.com/libp2p/go-libp2p-autonat v0.5.0 h1:/+3+4NcQV47DQ/duvRyFDP8oxv6CQTvSKYD5iWoPcYs=
github.com/libp2p/go-libp2p-autonat v0.5.0/go.mod h1:085tmmuXn0nXgFwuF7a2tt4UxgTjuapbuml27v4htKY=
Expand Down
23 changes: 22 additions & 1 deletion options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ import (
"github.com/libp2p/go-libp2p/config"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
autorelay "github.com/libp2p/go-libp2p/p2p/host/relay"
holepunch "github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
v2relay "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"

ma "github.com/multiformats/go-multiaddr"
madns "github.com/multiformats/go-multiaddr-dns"
Expand Down Expand Up @@ -231,6 +232,26 @@ func DisableRelay() Option {
}
}

// DisableRelaying configures libp2p to not run a circuit v2 relay.
// Note that we only start the relay if we detect that we're publicly reachable.
func DisableRelaying() Option {
return func(cfg *Config) error {
cfg.DisableRelay = true
cfg.RelayOpts = nil
return nil
}
}

// EnableRelaying configures libp2p to run a circuit v2 relay,
// if we dected that we're publicly reachable.
func EnableRelaying(opts ...v2relay.Option) Option {
return func(cfg *Config) error {
cfg.DisableRelay = false
cfg.RelayOpts = opts
return nil
}
}

// EnableAutoRelay configures libp2p to enable the AutoRelay subsystem.
//
// Dependencies:
Expand Down
43 changes: 29 additions & 14 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ import (
"sync"
"time"

autonat "github.com/libp2p/go-libp2p-autonat"
"github.com/libp2p/go-libp2p/p2p/host/relayv2"
v2relay "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"

"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/event"
Expand All @@ -19,13 +24,11 @@ import (
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/protocol"
"github.com/libp2p/go-libp2p-core/record"
"github.com/libp2p/go-libp2p/p2p/protocol/holepunch"

addrutil "github.com/libp2p/go-addr-util"
"github.com/libp2p/go-eventbus"
autonat "github.com/libp2p/go-libp2p-autonat"
inat "github.com/libp2p/go-libp2p-nat"
"github.com/libp2p/go-libp2p/p2p/protocol/identify"
"github.com/libp2p/go-libp2p/p2p/protocol/ping"
"github.com/libp2p/go-netroute"

logging "github.com/ipfs/go-log/v2"
Expand Down Expand Up @@ -70,15 +73,16 @@ type BasicHost struct {
// keep track of resources we need to wait on before shutting down
refCount sync.WaitGroup

network network.Network
mux *msmux.MultistreamMuxer
ids *identify.IDService
hps *holepunch.Service
pings *ping.PingService
natmgr NATManager
maResolver *madns.Resolver
cmgr connmgr.ConnManager
eventbus event.Bus
network network.Network
mux *msmux.MultistreamMuxer
ids *identify.IDService
hps *holepunch.Service
pings *ping.PingService
natmgr NATManager
maResolver *madns.Resolver
cmgr connmgr.ConnManager
eventbus event.Bus
relayManager *relayv2.RelayManager

AddrsFactory AddrsFactory

Expand Down Expand Up @@ -133,6 +137,11 @@ type HostOpts struct {
// EnablePing indicates whether to instantiate the ping service
EnablePing bool

// EnableV2Relay enables the circuit v2 relay (if we're publicly reachable).
EnableV2Relay bool
// RelayOpts are options for the circuit v2 relay.
RelayOpts []v2relay.Option

// UserAgent sets the user-agent for the host.
UserAgent string

Expand Down Expand Up @@ -245,6 +254,10 @@ func NewHost(n network.Network, opts *HostOpts) (*BasicHost, error) {
n.Notify(h.cmgr.Notifee())
}

if opts.EnableV2Relay {
h.relayManager = relayv2.NewRelayManager(h, opts.RelayOpts...)
}

if opts.EnablePing {
h.pings = ping.NewPingService(h)
}
Expand Down Expand Up @@ -1007,7 +1020,9 @@ func (h *BasicHost) Close() error {
if h.autoNat != nil {
h.autoNat.Close()
}

if h.relayManager != nil {
h.relayManager.Close()
}
if h.hps != nil {
h.hps.Close()
}
Expand Down
94 changes: 94 additions & 0 deletions p2p/host/relayv2/relay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package relayv2

import (
"context"
"fmt"
"sync"

v2relay "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"

"github.com/libp2p/go-libp2p-core/event"
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/network"
)

type RelayManager struct {
host host.Host

mutex sync.Mutex
relay *v2relay.Relay
opts []v2relay.Option

refCount sync.WaitGroup
ctxCancel context.CancelFunc
}

func NewRelayManager(host host.Host, opts ...v2relay.Option) *RelayManager {
ctx, cancel := context.WithCancel(context.Background())
m := &RelayManager{
host: host,
opts: opts,
ctxCancel: cancel,
}
m.refCount.Add(1)
go m.background(ctx)
return m
}

func (m *RelayManager) background(ctx context.Context) {
defer m.refCount.Done()
defer func() {
m.mutex.Lock()
if m.relay != nil {
m.relay.Close()
}
m.mutex.Unlock()
}()

subReachability, _ := m.host.EventBus().Subscribe(new(event.EvtLocalReachabilityChanged))
defer subReachability.Close()

for {
select {
case <-ctx.Done():
return
case ev, ok := <-subReachability.Out():
if !ok {
return
}
if err := m.reachabilityChanged(ev.(event.EvtLocalReachabilityChanged).Reachability); err != nil {
return
}
}
}
}

func (m *RelayManager) reachabilityChanged(r network.Reachability) error {
switch r {
case network.ReachabilityPublic:
fmt.Println("reachability changed to public")
relay, err := v2relay.New(m.host, m.opts...)
if err != nil {
return err
}
m.mutex.Lock()
defer m.mutex.Unlock()
m.relay = relay
case network.ReachabilityPrivate:
fmt.Println("reachability changed to private")
m.mutex.Lock()
defer m.mutex.Unlock()
if m.relay != nil {
err := m.relay.Close()
m.relay = nil
return err
}
}
return nil
}

func (m *RelayManager) Close() error {
m.ctxCancel()
m.refCount.Wait()
return nil
}

0 comments on commit 71eb4bf

Please sign in to comment.