Skip to content

Commit

Permalink
add an option to start the relay v2
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Sep 27, 2021
1 parent 954de9a commit 70e17db
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 17 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 (
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
routed "github.com/libp2p/go-libp2p/p2p/host/routed"
circuitv2 "github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/client"
relayv2 "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

EnableRelayService bool // should we run a circuitv2 relay (if publicly reachable)
RelayServiceOpts []relayv2.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,
EnableRelayService: cfg.EnableRelayService,
RelayServiceOpts: cfg.RelayServiceOpts,
})
if err != nil {
swrm.Close()
Expand Down
15 changes: 13 additions & 2 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import (
"github.com/libp2p/go-libp2p-core/pnet"

"github.com/libp2p/go-libp2p/config"
autorelay "github.com/libp2p/go-libp2p/p2p/host/autorelay"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
holepunch "github.com/libp2p/go-libp2p/p2p/protocol/holepunch"
relayv2 "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,16 @@ func DisableRelay() Option {
}
}

// EnableRelayService configures libp2p to run a circuit v2 relay,
// if we dected that we're publicly reachable.
func EnableRelayService(opts ...relayv2.Option) Option {
return func(cfg *Config) error {
cfg.EnableRelayService = true
cfg.RelayServiceOpts = 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/relaysvc"
relayv2 "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 *relaysvc.RelayManager

AddrsFactory AddrsFactory

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

// EnableRelayService enables the circuit v2 relay (if we're publicly reachable).
EnableRelayService bool
// RelayServiceOpts are options for the circuit v2 relay.
RelayServiceOpts []relayv2.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.EnableRelayService {
h.relayManager = relaysvc.NewRelayManager(h, opts.RelayServiceOpts...)
}

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
91 changes: 91 additions & 0 deletions p2p/host/relaysvc/relay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package relaysvc

import (
"context"
"sync"

relayv2 "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 *relayv2.Relay
opts []relayv2.Option

refCount sync.WaitGroup
ctxCancel context.CancelFunc
}

func NewRelayManager(host host.Host, opts ...relayv2.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:
relay, err := relayv2.New(m.host, m.opts...)
if err != nil {
return err
}
m.mutex.Lock()
defer m.mutex.Unlock()
m.relay = relay
case network.ReachabilityPrivate:
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 70e17db

Please sign in to comment.