From 235848850eb9b64e326f85f0ca04d32b21ef2738 Mon Sep 17 00:00:00 2001 From: vyzo Date: Fri, 16 Aug 2019 13:02:03 +0300 Subject: [PATCH 1/2] options to configure known relays for autorelay --- config/config.go | 3 ++- options.go | 34 ++++++++++++++++++++++++++++++++++ p2p/host/relay/autorelay.go | 11 +++++++++-- 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/config/config.go b/config/config.go index 3bb51bf1..46e99aae 100644 --- a/config/config.go +++ b/config/config.go @@ -70,6 +70,7 @@ type Config struct { Routing RoutingC EnableAutoRelay bool + StaticRelays []peer.AddrInfo } // NewNode constructs a new libp2p Host from the Config. @@ -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) } } diff --git a/options.go b/options.go index 6d310f1a..a78a3043 100644 --- a/options.go +++ b/options.go @@ -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" @@ -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 { diff --git a/p2p/host/relay/autorelay.go b/p2p/host/relay/autorelay.go index abab272e..0dfda1d5 100644 --- a/p2p/host/relay/autorelay.go +++ b/p2p/host/relay/autorelay.go @@ -25,7 +25,7 @@ const ( ) var ( - DesiredRelays = 3 + DesiredRelays = 1 BootDelay = 20 * time.Second ) @@ -38,6 +38,8 @@ type AutoRelay struct { autonat autonat.AutoNAT addrsF basic.AddrsFactory + static []peer.AddrInfo + disconnect chan struct{} mx sync.Mutex @@ -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, @@ -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)) From 9cd56c0babc918852643f146112f2e7782aa1418 Mon Sep 17 00:00:00 2001 From: vyzo Date: Tue, 8 Oct 2019 13:01:29 +0300 Subject: [PATCH 2/2] use a global variable for default relays and rename DefaultRelays option to DefaultStaticRelays. --- options.go | 13 +++++-------- p2p/host/relay/autorelay.go | 7 +++++++ 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/options.go b/options.go index a78a3043..899542ea 100644 --- a/options.go +++ b/options.go @@ -17,6 +17,7 @@ import ( 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" @@ -251,19 +252,15 @@ func EnableAutoRelay() Option { // discover relays func StaticRelays(relays []peer.AddrInfo) Option { return func(cfg *Config) error { - cfg.StaticRelays = relays + cfg.StaticRelays = append(cfg.StaticRelays, relays...) return nil } } -// DefaultRelays configures the static relays to use the known PL-operated relays -func DefaultRelays() Option { +// DefaultStaticRelays configures the static relays to use the known PL-operated relays +func DefaultStaticRelays() 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", - } { + for _, addr := range autorelay.DefaultRelays { a, err := ma.NewMultiaddr(addr) if err != nil { return err diff --git a/p2p/host/relay/autorelay.go b/p2p/host/relay/autorelay.go index 0dfda1d5..0c3f40b5 100644 --- a/p2p/host/relay/autorelay.go +++ b/p2p/host/relay/autorelay.go @@ -30,6 +30,13 @@ var ( 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