diff --git a/config/config.go b/config/config.go index 24fc1fa5e3..2ace430a49 100644 --- a/config/config.go +++ b/config/config.go @@ -76,6 +76,7 @@ type Config struct { Routing RoutingC EnableAutoRelay bool + StaticRelays []peer.AddrInfo } // NewNode constructs a new libp2p Host from the Config. @@ -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) } } diff --git a/options.go b/options.go index 6463ea712e..b46c466d4c 100644 --- a/options.go +++ b/options.go @@ -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" @@ -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. diff --git a/p2p/host/relay/autorelay.go b/p2p/host/relay/autorelay.go index abab272e36..0c3f40b53f 100644 --- a/p2p/host/relay/autorelay.go +++ b/p2p/host/relay/autorelay.go @@ -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 @@ -38,6 +45,8 @@ type AutoRelay struct { autonat autonat.AutoNAT addrsF basic.AddrsFactory + static []peer.AddrInfo + disconnect chan struct{} mx sync.Mutex @@ -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, @@ -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))