packages/hyprspace: implement AutoRelay
This commit is contained in:
parent
c3435bf9b8
commit
4d708f3c33
1 changed files with 44 additions and 0 deletions
|
@ -5,9 +5,11 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
|
"math/rand"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/url"
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p"
|
"github.com/libp2p/go-libp2p"
|
||||||
"github.com/libp2p/go-libp2p-core/crypto"
|
"github.com/libp2p/go-libp2p-core/crypto"
|
||||||
|
@ -16,6 +18,8 @@ import (
|
||||||
"github.com/libp2p/go-libp2p-core/peer"
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
"github.com/libp2p/go-libp2p-core/pnet"
|
"github.com/libp2p/go-libp2p-core/pnet"
|
||||||
dht "github.com/libp2p/go-libp2p-kad-dht"
|
dht "github.com/libp2p/go-libp2p-kad-dht"
|
||||||
|
"github.com/libp2p/go-libp2p/p2p/discovery/backoff"
|
||||||
|
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
|
||||||
"github.com/libp2p/go-tcp-transport"
|
"github.com/libp2p/go-tcp-transport"
|
||||||
ma "github.com/multiformats/go-multiaddr"
|
ma "github.com/multiformats/go-multiaddr"
|
||||||
)
|
)
|
||||||
|
@ -81,6 +85,8 @@ func CreateNode(ctx context.Context, inputKey []byte, port int, handler network.
|
||||||
|
|
||||||
key, _ := pnet.DecodeV1PSK(swarmKey)
|
key, _ := pnet.DecodeV1PSK(swarmKey)
|
||||||
|
|
||||||
|
peerChan := make(chan peer.AddrInfo)
|
||||||
|
|
||||||
// Create libp2p node
|
// Create libp2p node
|
||||||
node, err = libp2p.New(
|
node, err = libp2p.New(
|
||||||
libp2p.PrivateNetwork(key),
|
libp2p.PrivateNetwork(key),
|
||||||
|
@ -93,6 +99,32 @@ func CreateNode(ctx context.Context, inputKey []byte, port int, handler network.
|
||||||
libp2p.EnableHolePunching(),
|
libp2p.EnableHolePunching(),
|
||||||
libp2p.EnableRelayService(),
|
libp2p.EnableRelayService(),
|
||||||
libp2p.EnableNATService(),
|
libp2p.EnableNATService(),
|
||||||
|
libp2p.EnableAutoRelay(
|
||||||
|
autorelay.WithNumRelays(2),
|
||||||
|
autorelay.WithPeerSource(func(ctx context.Context, numPeers int) <-chan peer.AddrInfo {
|
||||||
|
r := make(chan peer.AddrInfo)
|
||||||
|
go func() {
|
||||||
|
defer close(r)
|
||||||
|
for ; numPeers != 0; numPeers-- {
|
||||||
|
select {
|
||||||
|
case v, ok := <-peerChan:
|
||||||
|
if !ok {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
select {
|
||||||
|
case r <- v:
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
case <-ctx.Done():
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
return r
|
||||||
|
}, time.Second*1),
|
||||||
|
),
|
||||||
|
libp2p.WithDialTimeout(time.Second*5),
|
||||||
libp2p.FallbackDefaults,
|
libp2p.FallbackDefaults,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -146,6 +178,18 @@ func CreateNode(ctx context.Context, inputKey []byte, port int, handler network.
|
||||||
return node, nil, err
|
return node, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Continuously feed peers into the AutoRelay service
|
||||||
|
go func() {
|
||||||
|
delay := backoff.NewExponentialDecorrelatedJitter(time.Second, time.Second*60, 5.0, rand.NewSource(time.Now().UnixMilli()))()
|
||||||
|
for {
|
||||||
|
for _, p := range node.Network().Peers() {
|
||||||
|
pi := node.Network().Peerstore().PeerInfo(p)
|
||||||
|
peerChan <- pi
|
||||||
|
}
|
||||||
|
time.Sleep(delay.Delay())
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
|
||||||
return node, dhtOut, nil
|
return node, dhtOut, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue