Skip to content

Commit

Permalink
Merge pull request #1368 from libp2p/discover-v2-relays
Browse files Browse the repository at this point in the history
implement relay v2 discovery
  • Loading branch information
marten-seemann authored Apr 10, 2022
2 parents 6009736 + 46fc1e5 commit 1c6bfa2
Show file tree
Hide file tree
Showing 9 changed files with 977 additions and 751 deletions.
22 changes: 3 additions & 19 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"github.com/libp2p/go-libp2p-core/transport"
"github.com/libp2p/go-libp2p-peerstore/pstoremem"

drouting "github.com/libp2p/go-libp2p/p2p/discovery/routing"
"github.com/libp2p/go-libp2p/p2p/host/autonat"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
Expand Down Expand Up @@ -101,8 +100,8 @@ type Config struct {
Routing RoutingC

EnableAutoRelay bool
AutoRelayOpts []autorelay.Option
AutoNATConfig
StaticRelayOpt autorelay.StaticRelayOption

EnableHolePunching bool
HolePunchingOptions []holepunch.Option
Expand Down Expand Up @@ -270,7 +269,7 @@ func (cfg *Config) NewNode() (host.Host, error) {
}
}

// Note: h.AddrsFactory may be changed by AutoRelay, but non-relay version is
// Note: h.AddrsFactory may be changed by relayFinder, but non-relay version is
// used by AutoNAT below.
var ar *autorelay.AutoRelay
addrF := h.AddrsFactory
Expand All @@ -280,22 +279,7 @@ func (cfg *Config) NewNode() (host.Host, error) {
return nil, fmt.Errorf("cannot enable autorelay; relay is not enabled")
}

var opts []autorelay.Option
if cfg.StaticRelayOpt != nil {
opts = append(opts, autorelay.Option(cfg.StaticRelayOpt))
} else {
if router == nil {
h.Close()
return nil, fmt.Errorf("cannot enable autorelay; no routing for discovery")
}
crouter, ok := router.(routing.ContentRouting)
if !ok {
h.Close()
return nil, fmt.Errorf("cannot enable autorelay; no suitable routing for discovery")
}
opts = append(opts, autorelay.WithDiscoverer(drouting.NewRoutingDiscovery(crouter)))
}
ar, err = autorelay.NewAutoRelay(h, router, opts...)
ar, err = autorelay.NewAutoRelay(h, cfg.AutoRelayOpts...)
if err != nil {
return nil, err
}
Expand Down
11 changes: 3 additions & 8 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,10 @@ func EnableRelayService(opts ...relayv2.Option) Option {
//
// This subsystem performs automatic address rewriting to advertise relay addresses when it
// detects that the node is publicly unreachable (e.g. behind a NAT).
func EnableAutoRelay(opts ...autorelay.StaticRelayOption) Option {
func EnableAutoRelay(opts ...autorelay.Option) Option {
return func(cfg *Config) error {
if len(opts) > 0 {
if len(opts) > 1 {
return errors.New("only expected a single static relay configuration option")
}
cfg.StaticRelayOpt = opts[0]
}
cfg.EnableAutoRelay = true
cfg.AutoRelayOpts = opts
return nil
}
}
Expand All @@ -269,7 +264,7 @@ func EnableAutoRelay(opts ...autorelay.StaticRelayOption) Option {
// Deprecated: pass an autorelay.WithStaticRelays option to EnableAutoRelay.
func StaticRelays(relays []peer.AddrInfo) Option {
return func(cfg *Config) error {
cfg.StaticRelayOpt = autorelay.WithStaticRelays(relays)
cfg.AutoRelayOpts = append(cfg.AutoRelayOpts, autorelay.WithStaticRelays(relays))
return nil
}
}
Expand Down
Loading

0 comments on commit 1c6bfa2

Please sign in to comment.