Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an option to start the relay v2 #1197

Merged
merged 1 commit into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
1 change: 1 addition & 0 deletions examples/pubsub/chat/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ github.com/ipfs/go-datastore v0.4.1/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13X
github.com/ipfs/go-datastore v0.4.4/go.mod h1:SX/xMIKoCszPqp+z9JhPYCmoOoXTvaa13XEbGtsFUhA=
github.com/ipfs/go-datastore v0.4.5/go.mod h1:eXTcaaiN6uOlVCLS9GjJUJtlvJfM3xk23w3fyfrmmJs=
github.com/ipfs/go-datastore v0.4.6/go.mod h1:XSipLSc64rFKSFRFGo1ecQl+WhYce3K7frtpHkyPFUc=
github.com/ipfs/go-detect-race v0.0.1 h1:qX/xay2W3E4Q1U7d9lNs1sU9nvguX0a7319XbyQ6cOk=
github.com/ipfs/go-detect-race v0.0.1/go.mod h1:8BNT7shDZPo99Q74BpGMK+4D8Mn4j46UU0LZ723meps=
github.com/ipfs/go-ds-badger v0.2.3/go.mod h1:pEYw0rgg3FIrywKKnL+Snr+w/LjJZVMTBRn4FS6UHUk=
github.com/ipfs/go-ds-badger v0.2.7/go.mod h1:02rnztVKA4aZwDuaRPTf8mpqcKmXP7mLl6JPxd14JHA=
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
}