Skip to content

Commit

Permalink
Merge pull request #705 from libp2p/feat/static-relays
Browse files Browse the repository at this point in the history
options to configure static relays for autorelay
  • Loading branch information
Stebalien authored Jan 17, 2020
2 parents c615fa0 + 9cd56c0 commit 76944c4
Show file tree
Hide file tree
Showing 3 changed files with 49 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 @@ -76,6 +76,7 @@ type Config struct {
Routing RoutingC

EnableAutoRelay bool
StaticRelays []peer.AddrInfo
}

// NewNode constructs a new libp2p Host from the Config.
Expand Down Expand Up @@ -231,7 +232,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
31 changes: 31 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ 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"

circuit "github.com/libp2p/go-libp2p-circuit"
config "github.com/libp2p/go-libp2p/config"
bhost "github.com/libp2p/go-libp2p/p2p/host/basic"
autorelay "github.com/libp2p/go-libp2p/p2p/host/relay"

filter "github.com/libp2p/go-maddr-filter"
ma "github.com/multiformats/go-multiaddr"
Expand Down Expand Up @@ -245,6 +247,35 @@ 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 = append(cfg.StaticRelays, relays...)
return nil
}
}

// DefaultStaticRelays configures the static relays to use the known PL-operated relays
func DefaultStaticRelays() Option {
return func(cfg *Config) error {
for _, addr := range autorelay.DefaultRelays {
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. FilterAddresses should be used for cases where the
// addresses you want to deny are known ahead of time.
Expand Down
18 changes: 16 additions & 2 deletions p2p/host/relay/autorelay.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,18 @@ const (
)

var (
DesiredRelays = 3
DesiredRelays = 1

BootDelay = 20 * time.Second
)

// These are the known PL-operated relays
var DefaultRelays = []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",
}

// AutoRelay is a Host that uses relays for connectivity when a NAT is detected.
type AutoRelay struct {
host *basic.BasicHost
Expand All @@ -38,6 +45,8 @@ type AutoRelay struct {
autonat autonat.AutoNAT
addrsF basic.AddrsFactory

static []peer.AddrInfo

disconnect chan struct{}

mx sync.Mutex
Expand All @@ -48,12 +57,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 +255,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 76944c4

Please sign in to comment.