seed rand, add dht command
This commit is contained in:
parent
ef5a03747d
commit
058f350ad0
2 changed files with 67 additions and 0 deletions
63
cmd/dht.go
Normal file
63
cmd/dht.go
Normal file
|
@ -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 <action>",
|
||||||
|
Args: cobra.ExactArgs(1),
|
||||||
|
Short: "Do DHT things",
|
||||||
|
Run: dhtCmd,
|
||||||
|
}
|
||||||
|
RootCmd.AddCommand(d)
|
||||||
|
|
||||||
|
ping := &cobra.Command{
|
||||||
|
Use: "ping <ip>",
|
||||||
|
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")
|
||||||
|
}
|
4
main.go
4
main.go
|
@ -1,6 +1,9 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/lbryio/lbry.go/cmd"
|
"github.com/lbryio/lbry.go/cmd"
|
||||||
|
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -9,6 +12,7 @@ import (
|
||||||
var Version string
|
var Version string
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
rand.Seed(time.Now().UnixNano())
|
||||||
log.SetLevel(log.DebugLevel)
|
log.SetLevel(log.DebugLevel)
|
||||||
cmd.Execute()
|
cmd.Execute()
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue