Skip to content

Commit

Permalink
options to configure known relays for autorelay
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Aug 16, 2019
1 parent 071f7de commit 2358488
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
3 changes: 2 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Config struct {
Routing RoutingC

EnableAutoRelay bool
StaticRelays []peer.AddrInfo
}

// NewNode constructs a new libp2p Host from the Config.
Expand Down Expand Up @@ -226,7 +227,7 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
// advertise ourselves
relay.Advertise(ctx, discovery)
} else {
_ = relay.NewAutoRelay(swrm.Context(), h, discovery, router)
_ = relay.NewAutoRelay(swrm.Context(), h, discovery, router, cfg.StaticRelays)
}
}

Expand Down
34 changes: 34 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/libp2p/go-libp2p-core/connmgr"
"github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/peerstore"
"github.com/libp2p/go-libp2p-core/pnet"

Expand Down Expand Up @@ -245,6 +246,39 @@ func EnableAutoRelay() Option {
}
}

// StaticRelays configures known relays for autorelay; when this option is enabled
// then the system will use the configured relays instead of querying the DHT to
// discover relays
func StaticRelays(relays []peer.AddrInfo) Option {
return func(cfg *Config) error {
cfg.StaticRelays = relays
return nil
}
}

// DefaultRelays configures the static relays to use the known PL-operated relays
func DefaultRelays() Option {
return func(cfg *Config) error {
for _, addr := range []string{
"/ip4/147.75.80.110/tcp/4001/p2p/QmbFgm5zan8P6eWWmeyfncR5feYEMPbht5b1FW1C37aQ7y",
"/ip4/147.75.195.153/tcp/4001/p2p/QmW9m57aiBDHAkKj9nmFSEn7ZqrcF1fZS4bipsTCHburei",
"/ip4/147.75.70.221/tcp/4001/p2p/Qme8g49gm3q4Acp7xWBKg3nAa9fxZ1YmyDJdyGgoG6LsXh",
} {
a, err := ma.NewMultiaddr(addr)
if err != nil {
return err
}
pi, err := peer.AddrInfoFromP2pAddr(a)
if err != nil {
return err
}
cfg.StaticRelays = append(cfg.StaticRelays, *pi)
}

return nil
}
}

// FilterAddresses configures libp2p to never dial nor accept connections from
// the given addresses.
func FilterAddresses(addrs ...*net.IPNet) Option {
Expand Down
11 changes: 9 additions & 2 deletions p2p/host/relay/autorelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
)

var (
DesiredRelays = 3
DesiredRelays = 1

BootDelay = 20 * time.Second
)
Expand All @@ -38,6 +38,8 @@ type AutoRelay struct {
autonat autonat.AutoNAT
addrsF basic.AddrsFactory

static []peer.AddrInfo

disconnect chan struct{}

mx sync.Mutex
Expand All @@ -48,12 +50,13 @@ type AutoRelay struct {
cachedAddrsExpiry time.Time
}

func NewAutoRelay(ctx context.Context, bhost *basic.BasicHost, discover discovery.Discoverer, router routing.PeerRouting) *AutoRelay {
func NewAutoRelay(ctx context.Context, bhost *basic.BasicHost, discover discovery.Discoverer, router routing.PeerRouting, static []peer.AddrInfo) *AutoRelay {
ar := &AutoRelay{
host: bhost,
discover: discover,
router: router,
addrsF: bhost.AddrsFactory,
static: static,
relays: make(map[peer.ID]struct{}),
disconnect: make(chan struct{}, 1),
status: autonat.NATStatusUnknown,
Expand Down Expand Up @@ -245,6 +248,10 @@ func (ar *AutoRelay) connect(ctx context.Context, pi peer.AddrInfo) bool {
}

func (ar *AutoRelay) discoverRelays(ctx context.Context) ([]peer.AddrInfo, error) {
if len(ar.static) > 0 {
return ar.static, nil
}

ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
defer cancel()
return discovery.FindPeers(ctx, ar.discover, RelayRendezvous, discovery.Limit(1000))
Expand Down

0 comments on commit 2358488

Please sign in to comment.