2022-09-25 22:04:44 +03:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
2022-11-11 20:37:15 +02:00
|
|
|
"math/rand"
|
2022-09-25 22:04:44 +03:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2022-11-11 20:37:15 +02:00
|
|
|
"time"
|
2022-09-25 22:04:44 +03:00
|
|
|
|
2023-01-28 02:23:31 +02:00
|
|
|
"github.com/hyprspace/hyprspace/config"
|
2022-09-25 22:04:44 +03:00
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
dht "github.com/libp2p/go-libp2p-kad-dht"
|
2022-11-11 21:54:57 +02:00
|
|
|
"github.com/libp2p/go-libp2p/core/crypto"
|
|
|
|
"github.com/libp2p/go-libp2p/core/host"
|
|
|
|
"github.com/libp2p/go-libp2p/core/network"
|
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
"github.com/libp2p/go-libp2p/core/pnet"
|
2022-11-11 20:37:15 +02:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/discovery/backoff"
|
|
|
|
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
|
2023-01-28 02:23:31 +02:00
|
|
|
routedhost "github.com/libp2p/go-libp2p/p2p/host/routed"
|
2023-01-23 23:30:31 +02:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/protocol/circuitv2/relay"
|
2022-11-13 16:26:04 +02:00
|
|
|
libp2pquic "github.com/libp2p/go-libp2p/p2p/transport/quic"
|
2022-11-11 21:54:57 +02:00
|
|
|
"github.com/libp2p/go-libp2p/p2p/transport/tcp"
|
2022-09-25 22:04:44 +03:00
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Protocol is a descriptor for the Hyprspace P2P Protocol.
|
|
|
|
const Protocol = "/hyprspace/0.0.1"
|
|
|
|
|
2022-09-26 23:10:29 +03:00
|
|
|
var bootstrapTriggerChan = make(chan bool)
|
|
|
|
|
2023-01-21 13:50:37 +02:00
|
|
|
func getExtraPeers(addr ma.Multiaddr) (nodesList []string) {
|
2022-09-25 22:04:44 +03:00
|
|
|
nodesList = []string{}
|
|
|
|
ip4, err := addr.ValueForProtocol(ma.P_IP4)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
port, err := addr.ValueForProtocol(ma.P_TCP)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-01-21 13:50:37 +02:00
|
|
|
resp, err := http.PostForm("http://"+ip4+":"+port+"/api/v0/swarm/peers", url.Values{})
|
2022-09-25 22:04:44 +03:00
|
|
|
|
2022-09-26 20:32:22 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-09-25 22:04:44 +03:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
apiResponse, err := ioutil.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2023-01-21 13:50:37 +02:00
|
|
|
var obj = map[string][]map[string]interface{}{}
|
2022-09-25 22:04:44 +03:00
|
|
|
json.Unmarshal([]byte(apiResponse), &obj)
|
2023-01-21 13:50:37 +02:00
|
|
|
for _, v := range obj["Peers"] {
|
|
|
|
nodesList = append(nodesList, (v["Addr"].(string) + "/p2p/" + v["Peer"].(string)))
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-21 13:50:37 +02:00
|
|
|
func getExtraBootstrapNodes(addr ma.Multiaddr) (nodesList []string) {
|
|
|
|
nodesList = []string{}
|
|
|
|
ip4, err := addr.ValueForProtocol(ma.P_IP4)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
port, err := addr.ValueForProtocol(ma.P_TCP)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
resp, err := http.PostForm("http://"+ip4+":"+port+"/api/v0/bootstrap", url.Values{})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
apiResponse, err := ioutil.ReadAll(resp.Body)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var obj = map[string][]string{}
|
|
|
|
json.Unmarshal([]byte(apiResponse), &obj)
|
|
|
|
return obj["Peers"]
|
|
|
|
}
|
|
|
|
|
2022-09-25 22:04:44 +03:00
|
|
|
// CreateNode creates an internal Libp2p nodes and returns it and it's DHT Discovery service.
|
2023-01-28 02:23:31 +02:00
|
|
|
func CreateNode(ctx context.Context, inputKey []byte, port int, handler network.StreamHandler, acl relay.ACLFilter, vpnPeers []config.Peer) (node host.Host, dhtOut *dht.IpfsDHT, err error) {
|
2022-09-25 22:04:44 +03:00
|
|
|
// Unmarshal Private Key
|
2022-09-26 01:00:05 +03:00
|
|
|
privateKey, err := crypto.UnmarshalPrivateKey(inputKey)
|
2022-09-25 22:04:44 +03:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-12 22:51:21 +02:00
|
|
|
maybePrivateNet := libp2p.ChainOptions()
|
2022-09-26 00:37:25 +03:00
|
|
|
swarmKeyFile, ok := os.LookupEnv("HYPRSPACE_SWARM_KEY")
|
|
|
|
if ok {
|
|
|
|
fmt.Println("[+] Using swarm key " + swarmKeyFile)
|
2022-11-12 22:51:21 +02:00
|
|
|
var swarmKey *os.File
|
2022-09-26 00:37:25 +03:00
|
|
|
swarmKey, err = os.Open(swarmKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer swarmKey.Close()
|
2022-11-12 22:51:21 +02:00
|
|
|
key, _ := pnet.DecodeV1PSK(swarmKey)
|
|
|
|
maybePrivateNet = libp2p.PrivateNetwork(key)
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
|
2022-11-13 16:26:04 +02:00
|
|
|
ip6quic := fmt.Sprintf("/ip6/::/udp/%d/quic", port)
|
|
|
|
ip4quic := fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic", port)
|
|
|
|
|
2022-09-25 22:04:44 +03:00
|
|
|
ip6tcp := fmt.Sprintf("/ip6/::/tcp/%d", port)
|
|
|
|
ip4tcp := fmt.Sprintf("/ip4/0.0.0.0/tcp/%d", port)
|
|
|
|
|
2022-11-11 20:37:15 +02:00
|
|
|
peerChan := make(chan peer.AddrInfo)
|
|
|
|
|
2022-09-25 22:04:44 +03:00
|
|
|
// Create libp2p node
|
2023-01-28 02:23:31 +02:00
|
|
|
basicHost, err := libp2p.New(
|
2022-11-12 22:51:21 +02:00
|
|
|
maybePrivateNet,
|
2022-11-13 16:26:04 +02:00
|
|
|
libp2p.ListenAddrStrings(ip6tcp, ip4tcp, ip4quic, ip6quic),
|
2022-09-25 22:04:44 +03:00
|
|
|
libp2p.Identity(privateKey),
|
|
|
|
libp2p.DefaultSecurity,
|
|
|
|
libp2p.NATPortMap(),
|
|
|
|
libp2p.DefaultMuxers,
|
2022-11-13 16:26:04 +02:00
|
|
|
libp2p.Transport(libp2pquic.NewTransport),
|
2022-09-25 22:04:44 +03:00
|
|
|
libp2p.Transport(tcp.NewTCPTransport),
|
|
|
|
libp2p.EnableHolePunching(),
|
2023-01-23 23:30:31 +02:00
|
|
|
libp2p.EnableRelayService(relay.WithLimit(nil), relay.WithACL(acl)),
|
2022-09-25 22:04:44 +03:00
|
|
|
libp2p.EnableNATService(),
|
2022-11-11 20:37:15 +02:00
|
|
|
libp2p.EnableAutoRelay(
|
|
|
|
autorelay.WithNumRelays(2),
|
2023-01-23 23:30:31 +02:00
|
|
|
autorelay.WithBootDelay(10*time.Second),
|
2022-11-11 20:37:15 +02:00
|
|
|
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
|
2023-03-23 22:23:46 +02:00
|
|
|
}),
|
2022-11-11 20:37:15 +02:00
|
|
|
),
|
|
|
|
libp2p.WithDialTimeout(time.Second*5),
|
2022-09-25 22:04:44 +03:00
|
|
|
libp2p.FallbackDefaults,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Define Bootstrap Nodes.
|
|
|
|
peers := []string{
|
2023-01-28 14:09:16 +02:00
|
|
|
"/ip4/152.67.79.222/tcp/110/p2p/12D3KooWQWsHPUUeFhe4b6pyCaD1hBoj8j6Z7S7kTznRTh1p1eVt",
|
|
|
|
"/ip4/152.67.79.222/udp/110/quic/p2p/12D3KooWQWsHPUUeFhe4b6pyCaD1hBoj8j6Z7S7kTznRTh1p1eVt",
|
|
|
|
"/ip4/152.67.79.222/tcp/995/p2p/QmbrAHuh4RYcyN9fWePCZMVmQjbaNXtyvrDCWz4VrchbXh",
|
|
|
|
"/ip4/152.67.79.222/udp/995/quic/p2p/QmbrAHuh4RYcyN9fWePCZMVmQjbaNXtyvrDCWz4VrchbXh",
|
|
|
|
"/ip4/95.216.8.12/tcp/110/p2p/Qmd7QHZU8UjfYdwmjmq1SBh9pvER9AwHpfwQvnvNo3HBBo",
|
|
|
|
"/ip4/95.216.8.12/udp/110/quic/p2p/Qmd7QHZU8UjfYdwmjmq1SBh9pvER9AwHpfwQvnvNo3HBBo",
|
|
|
|
"/ip4/95.216.8.12/tcp/995/p2p/QmYs4xNBby2fTs8RnzfXEk161KD4mftBfCiR8yXtgGPj4J",
|
|
|
|
"/ip4/95.216.8.12/udp/995/quic/p2p/QmYs4xNBby2fTs8RnzfXEk161KD4mftBfCiR8yXtgGPj4J",
|
|
|
|
"/dnsaddr/bootstrap.libp2p.io/p2p/12D3KooWEZXjE41uU4EL2gpkAQeDXYok6wghN7wwNVPF5bwkaNfS",
|
|
|
|
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
|
|
|
|
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
|
|
|
|
"/dnsaddr/bootstrap.libp2p.io/p2p/QmZa1sAxajnQjVM8WjWXoMbmPd7NsWhfKsPkErzpm9wGkp",
|
|
|
|
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
|
|
|
|
"/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Convert Bootstap Nodes into usable addresses.
|
2022-11-10 00:52:56 +02:00
|
|
|
staticBootstrapPeers, err := parsePeerAddrs(peers)
|
|
|
|
if err != nil {
|
|
|
|
return node, nil, err
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
|
2023-01-21 13:50:37 +02:00
|
|
|
ipfsApiStr, ok := os.LookupEnv("HYPRSPACE_IPFS_API")
|
|
|
|
if ok {
|
|
|
|
ipfsApiAddr, err := ma.NewMultiaddr(ipfsApiStr)
|
|
|
|
if err == nil {
|
|
|
|
fmt.Println("[+] Getting additional peers from IPFS API")
|
|
|
|
extraPeers, err := parsePeerAddrs(getExtraPeers(ipfsApiAddr))
|
|
|
|
if err == nil {
|
|
|
|
fmt.Printf("[+] %d additional addresses\n", len(extraPeers))
|
|
|
|
for _, p := range extraPeers {
|
2023-01-28 02:23:31 +02:00
|
|
|
basicHost.Peerstore().AddAddrs(p.ID, p.Addrs, 5*time.Minute)
|
2023-01-21 13:50:37 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-10 00:52:56 +02:00
|
|
|
// Create DHT Subsystem
|
|
|
|
dhtOut, err = dht.New(
|
|
|
|
ctx,
|
2023-01-28 02:23:31 +02:00
|
|
|
basicHost,
|
2022-12-31 18:05:45 +02:00
|
|
|
dht.Mode(dht.ModeClient),
|
2022-11-10 00:52:56 +02:00
|
|
|
dht.BootstrapPeers(staticBootstrapPeers...),
|
|
|
|
dht.BootstrapPeersFunc(func() []peer.AddrInfo {
|
|
|
|
extraBootstrapNodes := []string{}
|
|
|
|
ipfsApiStr, ok := os.LookupEnv("HYPRSPACE_IPFS_API")
|
|
|
|
if ok {
|
|
|
|
ipfsApiAddr, err := ma.NewMultiaddr(ipfsApiStr)
|
|
|
|
if err == nil {
|
2023-01-21 13:50:37 +02:00
|
|
|
fmt.Println("[+] Getting additional bootstrap nodes from IPFS API")
|
2022-11-10 00:52:56 +02:00
|
|
|
extraBootstrapNodes = getExtraBootstrapNodes(ipfsApiAddr)
|
2023-01-21 13:50:37 +02:00
|
|
|
fmt.Printf("[+] %d additional bootstrap nodes\n", len(extraBootstrapNodes))
|
2022-11-10 00:52:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
dynamicBootstrapPeers, err := parsePeerAddrs(extraBootstrapNodes)
|
|
|
|
if err != nil {
|
|
|
|
return staticBootstrapPeers
|
|
|
|
} else {
|
|
|
|
return append(staticBootstrapPeers, dynamicBootstrapPeers...)
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
)
|
2022-09-26 23:10:29 +03:00
|
|
|
|
2023-01-28 02:23:31 +02:00
|
|
|
pexr := PeXRouting{basicHost, vpnPeers}
|
|
|
|
pr := ParallelRouting{[]routedhost.Routing{pexr, dhtOut}}
|
|
|
|
node = routedhost.Wrap(basicHost, pr)
|
|
|
|
|
|
|
|
// Setup Hyprspace Stream Handler
|
|
|
|
node.SetStreamHandler(Protocol, handler)
|
|
|
|
|
2022-11-10 00:52:56 +02:00
|
|
|
if err != nil {
|
|
|
|
return node, nil, err
|
2022-09-26 23:10:29 +03:00
|
|
|
}
|
|
|
|
|
2022-11-11 20:37:15 +02:00
|
|
|
// 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)
|
2023-01-23 23:30:31 +02:00
|
|
|
if acl.AllowReserve(p, node.Addrs()[0]) {
|
|
|
|
peerChan <- pi
|
|
|
|
}
|
2022-11-11 20:37:15 +02:00
|
|
|
}
|
|
|
|
time.Sleep(delay.Delay())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-09-26 23:10:29 +03:00
|
|
|
return node, dhtOut, nil
|
|
|
|
}
|
|
|
|
|
2022-11-10 00:52:56 +02:00
|
|
|
func parsePeerAddrs(peers []string) (addrs []peer.AddrInfo, err error) {
|
|
|
|
for _, addrStr := range peers {
|
|
|
|
addr, err := ma.NewMultiaddr(addrStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
pii, err := peer.AddrInfoFromP2pAddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-09-26 23:10:29 +03:00
|
|
|
}
|
2022-11-10 00:52:56 +02:00
|
|
|
addrs = append(addrs, *pii)
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
2022-11-10 00:52:56 +02:00
|
|
|
return addrs, nil
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|