From eb9daede1fc90de2334c31967657cc7016bacd68 Mon Sep 17 00:00:00 2001 From: Max Date: Mon, 23 Jan 2023 22:30:31 +0100 Subject: [PATCH] packages/hyprspace: use native libp2p relay --- packages/networking/hyprspace/cli/up.go | 1 + packages/networking/hyprspace/p2p/node.go | 10 +++++--- packages/networking/hyprspace/p2p/relay.go | 29 ++++++++++++++++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 packages/networking/hyprspace/p2p/relay.go diff --git a/packages/networking/hyprspace/cli/up.go b/packages/networking/hyprspace/cli/up.go index b7b6812..7339623 100644 --- a/packages/networking/hyprspace/cli/up.go +++ b/packages/networking/hyprspace/cli/up.go @@ -106,6 +106,7 @@ func UpRun(r *cmd.Root, c *cmd.Sub) { privateKey, port, streamHandler, + p2p.NewClosedCircuitRelayFilter(cfg.Peers), ) checkErr(err) host.SetStreamHandler(p2p.PeXProtocol, p2p.NewPeXStreamHandler(host, cfg)) diff --git a/packages/networking/hyprspace/p2p/node.go b/packages/networking/hyprspace/p2p/node.go index 0818fd7..f5a4113 100644 --- a/packages/networking/hyprspace/p2p/node.go +++ b/packages/networking/hyprspace/p2p/node.go @@ -20,6 +20,7 @@ import ( "github.com/libp2p/go-libp2p/core/pnet" "github.com/libp2p/go-libp2p/p2p/discovery/backoff" "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" "github.com/libp2p/go-libp2p/p2p/transport/tcp" 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. -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 privateKey, err := crypto.UnmarshalPrivateKey(inputKey) if err != nil { @@ -128,10 +129,11 @@ func CreateNode(ctx context.Context, inputKey []byte, port int, handler network. libp2p.Transport(libp2pquic.NewTransport), libp2p.Transport(tcp.NewTCPTransport), libp2p.EnableHolePunching(), - libp2p.EnableRelayService(), + libp2p.EnableRelayService(relay.WithLimit(nil), relay.WithACL(acl)), libp2p.EnableNATService(), libp2p.EnableAutoRelay( autorelay.WithNumRelays(2), + autorelay.WithBootDelay(10*time.Second), autorelay.WithPeerSource(func(ctx context.Context, numPeers int) <-chan peer.AddrInfo { r := make(chan peer.AddrInfo) go func() { @@ -230,7 +232,9 @@ func CreateNode(ctx context.Context, inputKey []byte, port int, handler network. for { for _, p := range node.Network().Peers() { pi := node.Network().Peerstore().PeerInfo(p) - peerChan <- pi + if acl.AllowReserve(p, node.Addrs()[0]) { + peerChan <- pi + } } time.Sleep(delay.Delay()) } diff --git a/packages/networking/hyprspace/p2p/relay.go b/packages/networking/hyprspace/p2p/relay.go new file mode 100644 index 0000000..afbf060 --- /dev/null +++ b/packages/networking/hyprspace/p2p/relay.go @@ -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, + } +}