2018-03-24 00:18:00 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-06-15 04:30:37 +02:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"strconv"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
2018-07-13 19:31:54 +02:00
|
|
|
|
2018-03-24 00:18:00 +01:00
|
|
|
"github.com/lbryio/reflector.go/dht"
|
2018-06-14 17:48:02 +02:00
|
|
|
"github.com/lbryio/reflector.go/dht/bits"
|
2018-07-13 19:31:54 +02:00
|
|
|
|
2018-09-20 17:24:36 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
2018-03-24 00:18:00 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-08-07 17:00:04 +02:00
|
|
|
var dhtNodeID string
|
2018-06-15 04:30:37 +02:00
|
|
|
var dhtPort int
|
2018-08-31 02:20:15 +02:00
|
|
|
var dhtRPCPort int
|
2018-08-07 17:00:04 +02:00
|
|
|
var dhtSeeds []string
|
2018-06-15 04:30:37 +02:00
|
|
|
|
2018-03-24 00:18:00 +01:00
|
|
|
func init() {
|
|
|
|
var cmd = &cobra.Command{
|
2018-08-07 17:00:04 +02:00
|
|
|
Use: "dht [connect|bootstrap]",
|
2018-06-15 04:30:37 +02:00
|
|
|
Short: "Run dht node",
|
2018-08-07 17:00:04 +02:00
|
|
|
ValidArgs: []string{"connect", "bootstrap"},
|
2018-06-15 04:30:37 +02:00
|
|
|
Args: argFuncs(cobra.ExactArgs(1), cobra.OnlyValidArgs),
|
|
|
|
Run: dhtCmd,
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
2018-08-07 17:00:04 +02:00
|
|
|
cmd.PersistentFlags().StringVar(&dhtNodeID, "nodeID", "", "nodeID in hex")
|
2018-06-15 04:30:37 +02:00
|
|
|
cmd.PersistentFlags().IntVar(&dhtPort, "port", 4567, "Port to start DHT on")
|
2018-08-31 02:20:15 +02:00
|
|
|
cmd.PersistentFlags().IntVar(&dhtRPCPort, "rpcPort", 0, "Port to listen for rpc commands on")
|
2018-08-07 17:00:04 +02:00
|
|
|
cmd.PersistentFlags().StringSliceVar(&dhtSeeds, "seeds", []string{}, "Addresses of seed nodes")
|
2018-05-30 03:38:55 +02:00
|
|
|
rootCmd.AddCommand(cmd)
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func dhtCmd(cmd *cobra.Command, args []string) {
|
2018-06-15 04:30:37 +02:00
|
|
|
if args[0] == "bootstrap" {
|
2018-06-19 19:47:13 +02:00
|
|
|
node := dht.NewBootstrapNode(bits.Rand(), 1*time.Millisecond, 1*time.Minute)
|
2018-06-15 04:30:37 +02:00
|
|
|
listener, err := net.ListenPacket(dht.Network, "127.0.0.1:"+strconv.Itoa(dhtPort))
|
|
|
|
checkErr(err)
|
|
|
|
conn := listener.(*net.UDPConn)
|
|
|
|
err = node.Connect(conn)
|
|
|
|
checkErr(err)
|
|
|
|
interruptChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interruptChan, os.Interrupt, syscall.SIGTERM)
|
|
|
|
<-interruptChan
|
|
|
|
node.Shutdown()
|
|
|
|
} else {
|
2018-08-07 17:00:04 +02:00
|
|
|
nodeID := bits.Rand()
|
|
|
|
if dhtNodeID != "" {
|
|
|
|
nodeID = bits.FromHexP(dhtNodeID)
|
2018-07-10 23:41:36 +02:00
|
|
|
}
|
|
|
|
log.Println(nodeID.String())
|
2018-08-07 17:00:04 +02:00
|
|
|
|
|
|
|
dhtConf := dht.NewStandardConfig()
|
|
|
|
dhtConf.Address = "0.0.0.0:" + strconv.Itoa(dhtPort)
|
2018-08-31 02:20:15 +02:00
|
|
|
dhtConf.RPCPort = dhtRPCPort
|
2018-08-07 17:00:04 +02:00
|
|
|
if len(dhtSeeds) > 0 {
|
|
|
|
dhtConf.SeedNodes = dhtSeeds
|
|
|
|
}
|
|
|
|
|
|
|
|
d := dht.New(dhtConf)
|
2018-08-07 17:38:55 +02:00
|
|
|
err := d.Start()
|
|
|
|
if err != nil {
|
|
|
|
log.Println("error starting dht: " + err.Error())
|
|
|
|
return
|
|
|
|
}
|
2018-08-07 17:00:04 +02:00
|
|
|
|
2018-07-10 23:41:36 +02:00
|
|
|
interruptChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interruptChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
<-interruptChan
|
2018-08-07 17:00:04 +02:00
|
|
|
d.Shutdown()
|
2018-04-25 00:12:17 +02:00
|
|
|
}
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|