Add priv key gen util, use same peer id each time dht-interop.go runs

jhiesey-dht-interop-closure-instead-of-globals
Mike Goelzer 6 years ago
parent 8dad7bfba8
commit b074712530
No known key found for this signature in database
GPG Key ID: EDAC46A37751AD6D

2
.gitignore vendored

@ -1,2 +1,4 @@
dht-interop
js-dht-test/node_modules/
util/private_key.bin
util/private-key-gen

@ -1,94 +1,116 @@
package main
import (
"context"
"fmt"
"os"
"time"
"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/ipfs/go-cid"
"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")
}
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 main() {
ctx := context.Background()
//
// Set up a libp2p host.
//
host, err := libp2p.New(ctx, libp2p.ListenAddrStrings("/ip4/0.0.0.0/tcp/9876"))
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 {}
}
ctx := context.Background()
//
// Read the private key
//
var privBytes []byte
privBytes, err := ioutil.ReadFile("util/private_key.bin")
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 {}
}

@ -0,0 +1,5 @@
SRC = private-key-gen.go
BIN = $(SRC:.go=)
all: $(SRC)
go build -o $(BIN) $(SRC)

@ -0,0 +1,41 @@
package main
import (
"fmt"
"io/ioutil"
"os"
_ "github.com/ipfs/go-cid"
"github.com/libp2p/go-libp2p-crypto"
_ "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"
)
func main() {
var priv crypto.PrivKey
priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 4096)
if err != nil {
fmt.Println("crypto.GenerateKeyPair: failed: %v", err)
panic(err)
}
var privBytes []byte
privBytes, err = crypto.MarshalPrivateKey(priv)
if err != nil {
fmt.Println("crypto.MarshalPrivateKey: failed: %v", err)
panic(err)
}
// Print the marshalled bytes
//n := len(privBytes)
//s := string(privBytes[:n])
//fmt.Printf("*** <%s> (n=%v)\n", s, n)
ioutil.WriteFile("private_key.bin", privBytes, os.ModePerm)
if err != nil {
fmt.Println("ioutil.WriteFile: failed: %v", err)
panic(err)
}
}
Loading…
Cancel
Save