From 058f350ad0fdb929b54f87527366d9c01dde84da Mon Sep 17 00:00:00 2001 From: Alex Grintsvayg Date: Tue, 27 Feb 2018 13:30:54 -0500 Subject: [PATCH] seed rand, add dht command --- cmd/dht.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 4 ++++ 2 files changed, 67 insertions(+) create mode 100644 cmd/dht.go diff --git a/cmd/dht.go b/cmd/dht.go new file mode 100644 index 0000000..076ebf0 --- /dev/null +++ b/cmd/dht.go @@ -0,0 +1,63 @@ +package cmd + +import ( + "strconv" + "time" + + "github.com/lbryio/lbry.go/dht" + log "github.com/sirupsen/logrus" + "github.com/spf13/cobra" +) + +func init() { + d := &cobra.Command{ + Use: "dht ", + Args: cobra.ExactArgs(1), + Short: "Do DHT things", + Run: dhtCmd, + } + RootCmd.AddCommand(d) + + ping := &cobra.Command{ + Use: "ping ", + Args: cobra.ExactArgs(1), + Short: "Ping a node on the DHT", + Run: dhtPingCmd, + } + d.AddCommand(ping) +} + +func dhtCmd(cmd *cobra.Command, args []string) { + log.Errorln("chose a command") +} + +func dhtPingCmd(cmd *cobra.Command, args []string) { + //ip := args[0] + + port := 49449 // + (rand.Int() % 10) + + config := dht.NewStandardConfig() + config.Address = "127.0.0.1:" + strconv.Itoa(port) + config.PrimeNodes = []string{ + "127.0.0.1:10001", + } + + d := dht.New(config) + log.Println("Starting...") + go d.Run() + + time.Sleep(2 * time.Second) + + for { + peers, err := d.FindNode("012b66fc7052d9a0c8cb563b8ede7662003ba65f425c2661b5c6919d445deeb31469be8b842d6faeea3f2b3ebcaec845") + if err != nil { + time.Sleep(time.Second * 1) + continue + } + + log.Println("Found peers:", peers) + break + } + + log.Println("done") +} diff --git a/main.go b/main.go index 72dddbb..692ee69 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,9 @@ package main import ( + "math/rand" + "time" + "github.com/lbryio/lbry.go/cmd" log "github.com/sirupsen/logrus" @@ -9,6 +12,7 @@ import ( var Version string func main() { + rand.Seed(time.Now().UnixNano()) log.SetLevel(log.DebugLevel) cmd.Execute() }