2022-09-25 22:04:44 +03:00
|
|
|
package p2p
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
2022-09-26 23:10:29 +03:00
|
|
|
"os/signal"
|
2022-09-25 22:04:44 +03:00
|
|
|
"sync"
|
2022-09-26 23:10:29 +03:00
|
|
|
"syscall"
|
2022-09-25 22:04:44 +03:00
|
|
|
|
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
"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"
|
|
|
|
dht "github.com/libp2p/go-libp2p-kad-dht"
|
|
|
|
"github.com/libp2p/go-tcp-transport"
|
|
|
|
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)
|
|
|
|
|
2022-09-25 22:04:44 +03: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/swarm/addrs", url.Values{})
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
var obj = map[string]map[string][]string{}
|
|
|
|
json.Unmarshal([]byte(apiResponse), &obj)
|
|
|
|
for k, v := range obj["Addrs"] {
|
|
|
|
for _, addr := range v {
|
|
|
|
nodesList = append(nodesList, (addr + "/p2p/" + k))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateNode creates an internal Libp2p nodes and returns it and it's DHT Discovery service.
|
2022-09-26 01:00:05 +03:00
|
|
|
func CreateNode(ctx context.Context, inputKey []byte, port int, handler network.StreamHandler) (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-09-26 00:37:25 +03:00
|
|
|
var swarmKey *os.File
|
|
|
|
swarmKeyFile, ok := os.LookupEnv("HYPRSPACE_SWARM_KEY")
|
|
|
|
if ok {
|
|
|
|
fmt.Println("[+] Using swarm key " + swarmKeyFile)
|
|
|
|
swarmKey, err = os.Open(swarmKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer swarmKey.Close()
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
|
|
|
extraBootstrapNodes := []string{}
|
|
|
|
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")
|
|
|
|
extraBootstrapNodes = getExtraBootstrapNodes(ipfsApiAddr)
|
2022-09-26 20:32:22 +03:00
|
|
|
fmt.Printf("[+] %d additional addresses\n", len(extraBootstrapNodes))
|
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)
|
|
|
|
|
|
|
|
key, _ := pnet.DecodeV1PSK(swarmKey)
|
|
|
|
|
|
|
|
// Create libp2p node
|
|
|
|
node, err = libp2p.New(
|
|
|
|
libp2p.PrivateNetwork(key),
|
|
|
|
libp2p.ListenAddrStrings(ip6tcp, ip4tcp),
|
|
|
|
libp2p.Identity(privateKey),
|
|
|
|
libp2p.DefaultSecurity,
|
|
|
|
libp2p.NATPortMap(),
|
|
|
|
libp2p.DefaultMuxers,
|
|
|
|
libp2p.Transport(tcp.NewTCPTransport),
|
|
|
|
libp2p.EnableHolePunching(),
|
|
|
|
libp2p.EnableRelayService(),
|
|
|
|
libp2p.EnableNATService(),
|
|
|
|
libp2p.FallbackDefaults,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup Hyprspace Stream Handler
|
|
|
|
node.SetStreamHandler(Protocol, handler)
|
|
|
|
|
|
|
|
// Create DHT Subsystem
|
|
|
|
dhtOut = dht.NewDHTClient(ctx, node, datastore.NewMapDatastore())
|
|
|
|
|
|
|
|
// Define Bootstrap Nodes.
|
|
|
|
peers := []string{
|
|
|
|
"/ip4/168.235.67.108/tcp/4001/p2p/QmRMA5pWXtfuW1y5w2t9gYxrDDD6bPRLKdWAYnHTeCxZMm",
|
|
|
|
"/ip4/95.216.8.12/tcp/4001/p2p/Qmd7QHZU8UjfYdwmjmq1SBh9pvER9AwHpfwQvnvNo3HBBo",
|
|
|
|
"/ip6/2001:41d0:800:1402::3f16:3fb5/tcp/4001/p2p/12D3KooWDUgNsoLVauCDpRAo54mc4whoBudgeXQnZZK2iVYhBLCN",
|
|
|
|
"/ip6/2001:818:da65:e400:a553:fbc1:f0b1:5743/tcp/4001/p2p/12D3KooWC1RZxLvAeEFNTZWk1FWc1sZZ3yemF4FNNRYa3X854KJ8",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert Bootstap Nodes into usable addresses.
|
|
|
|
BootstrapPeers := make(map[peer.ID]*peer.AddrInfo, len(peers))
|
|
|
|
for _, addrStr := range append(peers, extraBootstrapNodes...) {
|
|
|
|
addr, err := ma.NewMultiaddr(addrStr)
|
|
|
|
if err != nil {
|
|
|
|
return node, dhtOut, err
|
|
|
|
}
|
|
|
|
pii, err := peer.AddrInfoFromP2pAddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
return node, dhtOut, err
|
|
|
|
}
|
|
|
|
pi, ok := BootstrapPeers[pii.ID]
|
|
|
|
if !ok {
|
|
|
|
pi = &peer.AddrInfo{ID: pii.ID}
|
|
|
|
BootstrapPeers[pi.ID] = pi
|
|
|
|
}
|
|
|
|
pi.Addrs = append(pi.Addrs, pii.Addrs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's connect to the bootstrap nodes first. They will tell us about the
|
|
|
|
// other nodes in the network.
|
2022-09-26 23:10:29 +03:00
|
|
|
count := bootstrap(ctx, node, BootstrapPeers)
|
|
|
|
|
|
|
|
if count < 1 {
|
2022-09-27 00:05:35 +03:00
|
|
|
fmt.Println("[!] Initial bootstrap failed")
|
2022-09-26 23:10:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
go rebootstrap(ctx, node, BootstrapPeers)
|
|
|
|
return node, dhtOut, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func bootstrap(ctx context.Context, node host.Host, bootstrapPeers map[peer.ID]*peer.AddrInfo) int {
|
2022-09-25 22:04:44 +03:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
lock := sync.Mutex{}
|
|
|
|
count := 0
|
2022-09-26 23:10:29 +03:00
|
|
|
wg.Add(len(bootstrapPeers))
|
|
|
|
for _, peerInfo := range bootstrapPeers {
|
2022-09-25 22:04:44 +03:00
|
|
|
go func(peerInfo *peer.AddrInfo) {
|
|
|
|
defer wg.Done()
|
2022-09-26 23:54:40 +03:00
|
|
|
node.Network().ClosePeer(peerInfo.ID)
|
2022-09-25 22:04:44 +03:00
|
|
|
err := node.Connect(ctx, *peerInfo)
|
|
|
|
if err == nil {
|
|
|
|
lock.Lock()
|
|
|
|
count++
|
|
|
|
lock.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
}(peerInfo)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2022-09-26 23:10:29 +03:00
|
|
|
fmt.Printf("[+] Connected to %d bootstrap peers\n", count)
|
|
|
|
return count
|
|
|
|
}
|
2022-09-25 22:04:44 +03:00
|
|
|
|
2022-09-26 23:10:29 +03:00
|
|
|
func rebootstrap(ctx context.Context, node host.Host, bootstrapPeers map[peer.ID]*peer.AddrInfo) {
|
|
|
|
signalCh := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signalCh, syscall.SIGUSR1)
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-bootstrapTriggerChan:
|
|
|
|
bootstrap(ctx, node, bootstrapPeers)
|
|
|
|
case <-signalCh:
|
|
|
|
fmt.Println("[-] Rebootstrapping on SIGUSR1")
|
|
|
|
bootstrap(ctx, node, bootstrapPeers)
|
|
|
|
Rediscover()
|
|
|
|
}
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|
2022-09-26 23:10:29 +03:00
|
|
|
}
|
2022-09-25 22:04:44 +03:00
|
|
|
|
2022-09-26 23:10:29 +03:00
|
|
|
func Rebootstrap() {
|
|
|
|
bootstrapTriggerChan <- true
|
2022-09-25 22:04:44 +03:00
|
|
|
}
|