mirror of https://github.com/rairyx/raven
remove directory
parent
4ec0b19c07
commit
0c789699e8
@ -1,11 +0,0 @@
|
|||||||
SRC = dht-interop.go
|
|
||||||
BIN = $(SRC:.go=)
|
|
||||||
DHT_SERVER = libp2p-bootstrap.goelzer.io
|
|
||||||
|
|
||||||
all: $(SRC)
|
|
||||||
go build -o $(BIN) $(SRC)
|
|
||||||
|
|
||||||
install:
|
|
||||||
-ssh $(DHT_SERVER) killall -9 dht-interop
|
|
||||||
scp $(SRC:.go=) $(DHT_SERVER):~/
|
|
||||||
ssh $(DHT_SERVER) ./$(BIN)
|
|
@ -1,139 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/ipfs/go-cid"
|
|
||||||
"github.com/libp2p/go-libp2p"
|
|
||||||
h "github.com/libp2p/go-libp2p-host"
|
|
||||||
"github.com/libp2p/go-libp2p-kad-dht"
|
|
||||||
"github.com/libp2p/go-libp2p-kad-dht/opts"
|
|
||||||
"github.com/libp2p/go-libp2p-net"
|
|
||||||
|
|
||||||
"github.com/libp2p/go-libp2p-crypto"
|
|
||||||
)
|
|
||||||
|
|
||||||
var ho h.Host
|
|
||||||
var dhtPtr *dht.IpfsDHT
|
|
||||||
|
|
||||||
func handleConn(conn net.Conn) {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
d := *dhtPtr
|
|
||||||
|
|
||||||
provideCid, err := cid.Decode("zb2rhXqLbdjpXnJG99QsjM6Nc6xaDKgEr2FfugDJynE7H2NR6")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
findCid, err := cid.Decode("QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL")
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
time.Sleep(5 * time.Second)
|
|
||||||
|
|
||||||
// First, announce ourselves as participating in this topic
|
|
||||||
fmt.Println("announcing ourselves...")
|
|
||||||
tctx, _ := context.WithTimeout(ctx, time.Second*10)
|
|
||||||
if err := d.Provide(tctx, provideCid, true); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Local node %s is providing %s\n", ho.ID().Pretty(), provideCid)
|
|
||||||
|
|
||||||
// Now, look for others who have announced
|
|
||||||
fmt.Println("searching for other peers...")
|
|
||||||
tctx, _ = context.WithTimeout(ctx, time.Second*10)
|
|
||||||
providers, err := d.FindProviders(tctx, findCid)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(providers) != 0 {
|
|
||||||
provider := providers[0]
|
|
||||||
fmt.Printf("Remote node %s is providing %s\n", provider.ID.Pretty(), findCid)
|
|
||||||
time.Sleep(5 * time.Second)
|
|
||||||
os.Exit(0)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("no remote providers!\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseArgs() (bool, string) {
|
|
||||||
usage := fmt.Sprintf("Usage: %s [-b] [PRIVATE_KEY]\n\n-b is bootstrap mode (creates DHT)\nPRIVATE_KEY is the path to a private key like '../util/private_key.bin'\n", os.Args[0])
|
|
||||||
var bBootstrap bool = false
|
|
||||||
var privKeyFilePath string
|
|
||||||
var args []string = os.Args[1:]
|
|
||||||
if (len(args) == 0) || (len(args) > 2) {
|
|
||||||
fmt.Printf("Error: wrong number of arguments\n\n%s", usage)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
if args[0] == "-b" {
|
|
||||||
bBootstrap = true
|
|
||||||
args = args[1:]
|
|
||||||
}
|
|
||||||
privKeyFilePath = args[0]
|
|
||||||
return bBootstrap, privKeyFilePath
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
ctx := context.Background()
|
|
||||||
|
|
||||||
bBootstrap, privKeyFilePath := parseArgs()
|
|
||||||
if !bBootstrap {
|
|
||||||
fmt.Printf("Error: bootstrap mode required in this example")
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Read the private key
|
|
||||||
//
|
|
||||||
var privBytes []byte
|
|
||||||
privBytes, err := ioutil.ReadFile(privKeyFilePath)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("ioutil.ReadFile: failed: %v", err)
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
var priv crypto.PrivKey
|
|
||||||
priv, err = crypto.UnmarshalPrivateKey(privBytes)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("crypto.UnmarshalPrivateKey: failed: %v", err)
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
//
|
|
||||||
// Set up a libp2p host.
|
|
||||||
//
|
|
||||||
host, err := libp2p.New(ctx,
|
|
||||||
libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/9876"),
|
|
||||||
libp2p.Identity(priv),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Println("libp2p.New: failed: %v", err)
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
host.Network().SetConnHandler(handleConn)
|
|
||||||
|
|
||||||
ho = host
|
|
||||||
|
|
||||||
fmt.Printf("To connect, run:\n")
|
|
||||||
fmt.Printf("node js-dht-test/index.js %s/ipfs/%s\n", host.Addrs()[0], host.ID().Pretty())
|
|
||||||
|
|
||||||
//
|
|
||||||
// Construct a DHT for discovery.
|
|
||||||
//
|
|
||||||
d, err := dht.New(ctx, host, dhtopts.Client(false))
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
|
|
||||||
dhtPtr = d
|
|
||||||
|
|
||||||
select {}
|
|
||||||
}
|
|
@ -1,96 +0,0 @@
|
|||||||
'use strict'
|
|
||||||
|
|
||||||
const libp2p = require('libp2p')
|
|
||||||
const TCP = require('libp2p-tcp')
|
|
||||||
const Mplex = require('libp2p-mplex')
|
|
||||||
const SECIO = require('libp2p-secio')
|
|
||||||
const PeerInfo = require('peer-info')
|
|
||||||
const CID = require('cids')
|
|
||||||
const KadDHT = require('libp2p-kad-dht')
|
|
||||||
const defaultsDeep = require('@nodeutils/defaults-deep')
|
|
||||||
const waterfall = require('async/waterfall')
|
|
||||||
const parallel = require('async/parallel')
|
|
||||||
|
|
||||||
class MyBundle extends libp2p {
|
|
||||||
constructor (_options) {
|
|
||||||
const defaults = {
|
|
||||||
modules: {
|
|
||||||
transport: [ TCP ],
|
|
||||||
streamMuxer: [ Mplex ],
|
|
||||||
connEncryption: [ SECIO ],
|
|
||||||
// we add the DHT module that will enable Peer and Content Routing
|
|
||||||
dht: KadDHT
|
|
||||||
},
|
|
||||||
config: {
|
|
||||||
dht: {
|
|
||||||
kBucketSize: 20
|
|
||||||
},
|
|
||||||
EXPERIMENTAL: {
|
|
||||||
dht: true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
super(defaultsDeep(_options, defaults))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function createNode (callback) {
|
|
||||||
let node
|
|
||||||
|
|
||||||
waterfall([
|
|
||||||
(cb) => PeerInfo.create(cb),
|
|
||||||
(peerInfo, cb) => {
|
|
||||||
peerInfo.multiaddrs.add('/ip4/0.0.0.0/tcp/0')
|
|
||||||
node = new MyBundle({
|
|
||||||
peerInfo
|
|
||||||
})
|
|
||||||
node.start(cb)
|
|
||||||
}
|
|
||||||
], (err) => callback(err, node))
|
|
||||||
}
|
|
||||||
|
|
||||||
parallel([
|
|
||||||
(cb) => createNode(cb),
|
|
||||||
], (err, nodes) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
const node1 = nodes[0]
|
|
||||||
|
|
||||||
const bootstrapAddr = process.argv[2]
|
|
||||||
console.log('Connecting to:', bootstrapAddr)
|
|
||||||
|
|
||||||
parallel([
|
|
||||||
(cb) => node1.dial(bootstrapAddr, cb),
|
|
||||||
// Set up of the cons might take time
|
|
||||||
(cb) => setTimeout(cb, 300)
|
|
||||||
], (err) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
const provideCid = new CID('QmTp9VkYvnHyrqKQuFPiuZkiX9gPcqj6x5LJ1rmWuSySnL')
|
|
||||||
const findCid = new CID('zb2rhXqLbdjpXnJG99QsjM6Nc6xaDKgEr2FfugDJynE7H2NR6')
|
|
||||||
|
|
||||||
node1.contentRouting.provide(provideCid, (err) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
console.log('Local node %s is providing %s', node1.peerInfo.id.toB58String(), provideCid.toBaseEncodedString())
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
node1.contentRouting.findProviders(findCid, 10000, (err, providers) => {
|
|
||||||
if (err) { throw err }
|
|
||||||
|
|
||||||
if (providers.length !== 0) {
|
|
||||||
const provider = providers[0]
|
|
||||||
// console.log(provider)
|
|
||||||
console.log('Remote node %s is providing %s', provider.id.toB58String(), findCid.toBaseEncodedString())
|
|
||||||
setTimeout(() => {
|
|
||||||
process.exit(0)
|
|
||||||
}, 5000)
|
|
||||||
} else {
|
|
||||||
console.log('No remote providers found!')
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}, 5000)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
})
|
|
@ -1,22 +0,0 @@
|
|||||||
{
|
|
||||||
"name": "js-dht-test",
|
|
||||||
"version": "1.0.0",
|
|
||||||
"description": "",
|
|
||||||
"main": "index.js",
|
|
||||||
"scripts": {
|
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
|
||||||
},
|
|
||||||
"author": "",
|
|
||||||
"license": "MIT",
|
|
||||||
"dependencies": {
|
|
||||||
"@nodeutils/defaults-deep": "^1.1.0",
|
|
||||||
"async": "^2.6.1",
|
|
||||||
"cids": "^0.5.3",
|
|
||||||
"libp2p": "^0.23.1",
|
|
||||||
"libp2p-kad-dht": "^0.10.2",
|
|
||||||
"libp2p-mplex": "^0.8.0",
|
|
||||||
"libp2p-secio": "^0.10.0",
|
|
||||||
"libp2p-tcp": "^0.12.1",
|
|
||||||
"peer-info": "^0.14.1"
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue