packages/hyprspace: use native libp2p relay

This commit is contained in:
Max Headroom 2023-01-23 22:30:31 +01:00
parent fa79991e93
commit eb9daede1f
3 changed files with 37 additions and 3 deletions

View file

@ -106,6 +106,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) {
privateKey, privateKey,
port, port,
streamHandler, streamHandler,
p2p.NewClosedCircuitRelayFilter(cfg.Peers),
) )
checkErr(err) checkErr(err)
host.SetStreamHandler(p2p.PeXProtocol, p2p.NewPeXStreamHandler(host, cfg)) host.SetStreamHandler(p2p.PeXProtocol, p2p.NewPeXStreamHandler(host, cfg))

View file

@ -20,6 +20,7 @@ import (
"github.com/libp2p/go-libp2p/core/pnet" "github.com/libp2p/go-libp2p/core/pnet"
"github.com/libp2p/go-libp2p/p2p/discovery/backoff" "github.com/libp2p/go-libp2p/p2p/discovery/backoff"
"github.com/libp2p/go-libp2p/p2p/host/autorelay" "github.com/libp2p/go-libp2p/p2p/host/autorelay"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic" libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
"github.com/libp2p/go-libp2p/p2p/transport/tcp" "github.com/libp2p/go-libp2p/p2p/transport/tcp"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
@ -88,7 +89,7 @@ func getExtraBootstrapNodes(addr ma.Multiaddr) (nodesList []string) {
} }
// CreateNode creates an internal Libp2p nodes and returns it and it's DHT Discovery service. // CreateNode creates an internal Libp2p nodes and returns it and it's DHT Discovery service.
func CreateNode(ctx context.Context, inputKey []byte, port int, handler network.StreamHandler) (node host.Host, dhtOut *dht.IpfsDHT, err error) { func CreateNode(ctx context.Context, inputKey []byte, port int, handler network.StreamHandler, acl relay.ACLFilter) (node host.Host, dhtOut *dht.IpfsDHT, err error) {
// Unmarshal Private Key // Unmarshal Private Key
privateKey, err := crypto.UnmarshalPrivateKey(inputKey) privateKey, err := crypto.UnmarshalPrivateKey(inputKey)
if err != nil { if err != nil {
@ -128,10 +129,11 @@ func CreateNode(ctx context.Context, inputKey []byte, port int, handler network.
libp2p.Transport(libp2pquic.NewTransport), libp2p.Transport(libp2pquic.NewTransport),
libp2p.Transport(tcp.NewTCPTransport), libp2p.Transport(tcp.NewTCPTransport),
libp2p.EnableHolePunching(), libp2p.EnableHolePunching(),
libp2p.EnableRelayService(), libp2p.EnableRelayService(relay.WithLimit(nil), relay.WithACL(acl)),
libp2p.EnableNATService(), libp2p.EnableNATService(),
libp2p.EnableAutoRelay( libp2p.EnableAutoRelay(
autorelay.WithNumRelays(2), autorelay.WithNumRelays(2),
autorelay.WithBootDelay(10*time.Second),
autorelay.WithPeerSource(func(ctx context.Context, numPeers int) <-chan peer.AddrInfo { autorelay.WithPeerSource(func(ctx context.Context, numPeers int) <-chan peer.AddrInfo {
r := make(chan peer.AddrInfo) r := make(chan peer.AddrInfo)
go func() { go func() {
@ -230,7 +232,9 @@ func CreateNode(ctx context.Context, inputKey []byte, port int, handler network.
for { for {
for _, p := range node.Network().Peers() { for _, p := range node.Network().Peers() {
pi := node.Network().Peerstore().PeerInfo(p) pi := node.Network().Peerstore().PeerInfo(p)
peerChan <- pi if acl.AllowReserve(p, node.Addrs()[0]) {
peerChan <- pi
}
} }
time.Sleep(delay.Delay()) time.Sleep(delay.Delay())
} }

View file

@ -0,0 +1,29 @@
package p2p
import (
"github.com/hyprspace/hyprspace/config"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
"github.com/multiformats/go-multiaddr"
)
type ClosedCircuitRelayFilter struct {
allowedPeers []config.Peer
}
func (ccr ClosedCircuitRelayFilter) AllowReserve(p peer.ID, a multiaddr.Multiaddr) bool {
_, found := config.FindPeer(ccr.allowedPeers, p)
return found
}
func (ccr ClosedCircuitRelayFilter) AllowConnect(src peer.ID, srcAddr multiaddr.Multiaddr, dest peer.ID) bool {
_, foundSrc := config.FindPeer(ccr.allowedPeers, src)
_, foundDst := config.FindPeer(ccr.allowedPeers, dest)
return foundSrc && foundDst
}
func NewClosedCircuitRelayFilter(allowedPeers []config.Peer) relay.ACLFilter {
return ClosedCircuitRelayFilter{
allowedPeers: allowedPeers,
}
}