options to configure known relays for autorelay
This commit is contained in:
parent
071f7de073
commit
235848850e
|
@ -70,6 +70,7 @@ type Config struct {
|
||||||
Routing RoutingC
|
Routing RoutingC
|
||||||
|
|
||||||
EnableAutoRelay bool
|
EnableAutoRelay bool
|
||||||
|
StaticRelays []peer.AddrInfo
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewNode constructs a new libp2p Host from the Config.
|
// 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
|
// advertise ourselves
|
||||||
relay.Advertise(ctx, discovery)
|
relay.Advertise(ctx, discovery)
|
||||||
} else {
|
} else {
|
||||||
_ = relay.NewAutoRelay(swrm.Context(), h, discovery, router)
|
_ = relay.NewAutoRelay(swrm.Context(), h, discovery, router, cfg.StaticRelays)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
34
options.go
34
options.go
|
@ -10,6 +10,7 @@ import (
|
||||||
"github.com/libp2p/go-libp2p-core/connmgr"
|
"github.com/libp2p/go-libp2p-core/connmgr"
|
||||||
"github.com/libp2p/go-libp2p-core/crypto"
|
"github.com/libp2p/go-libp2p-core/crypto"
|
||||||
"github.com/libp2p/go-libp2p-core/metrics"
|
"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/peerstore"
|
||||||
"github.com/libp2p/go-libp2p-core/pnet"
|
"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
|
// FilterAddresses configures libp2p to never dial nor accept connections from
|
||||||
// the given addresses.
|
// the given addresses.
|
||||||
func FilterAddresses(addrs ...*net.IPNet) Option {
|
func FilterAddresses(addrs ...*net.IPNet) Option {
|
||||||
|
|
|
@ -25,7 +25,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
DesiredRelays = 3
|
DesiredRelays = 1
|
||||||
|
|
||||||
BootDelay = 20 * time.Second
|
BootDelay = 20 * time.Second
|
||||||
)
|
)
|
||||||
|
@ -38,6 +38,8 @@ type AutoRelay struct {
|
||||||
autonat autonat.AutoNAT
|
autonat autonat.AutoNAT
|
||||||
addrsF basic.AddrsFactory
|
addrsF basic.AddrsFactory
|
||||||
|
|
||||||
|
static []peer.AddrInfo
|
||||||
|
|
||||||
disconnect chan struct{}
|
disconnect chan struct{}
|
||||||
|
|
||||||
mx sync.Mutex
|
mx sync.Mutex
|
||||||
|
@ -48,12 +50,13 @@ type AutoRelay struct {
|
||||||
cachedAddrsExpiry time.Time
|
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{
|
ar := &AutoRelay{
|
||||||
host: bhost,
|
host: bhost,
|
||||||
discover: discover,
|
discover: discover,
|
||||||
router: router,
|
router: router,
|
||||||
addrsF: bhost.AddrsFactory,
|
addrsF: bhost.AddrsFactory,
|
||||||
|
static: static,
|
||||||
relays: make(map[peer.ID]struct{}),
|
relays: make(map[peer.ID]struct{}),
|
||||||
disconnect: make(chan struct{}, 1),
|
disconnect: make(chan struct{}, 1),
|
||||||
status: autonat.NATStatusUnknown,
|
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) {
|
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)
|
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
return discovery.FindPeers(ctx, ar.discover, RelayRendezvous, discovery.Limit(1000))
|
return discovery.FindPeers(ctx, ar.discover, RelayRendezvous, discovery.Limit(1000))
|
||||||
|
|
Loading…
Reference in New Issue