2018-03-24 00:18:00 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2018-07-13 19:31:54 +02:00
|
|
|
"log"
|
|
|
|
"math/big"
|
2018-06-15 04:30:37 +02:00
|
|
|
"net"
|
2018-07-13 19:31:54 +02:00
|
|
|
"net/http"
|
2018-06-15 04:30:37 +02:00
|
|
|
"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-03-24 00:18:00 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
2018-07-10 23:41:36 +02:00
|
|
|
type NodeRPC string
|
|
|
|
|
|
|
|
type PingArgs struct {
|
2018-07-13 19:31:54 +02:00
|
|
|
nodeID string
|
2018-07-10 23:41:36 +02:00
|
|
|
address string
|
2018-07-13 19:31:54 +02:00
|
|
|
port int
|
2018-07-10 23:41:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type PingResult string
|
|
|
|
|
|
|
|
func (n *NodeRPC) Ping(r *http.Request, args *PingArgs, result *PingResult) error {
|
|
|
|
*result = PingResult("pong")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-06-15 04:30:37 +02:00
|
|
|
var dhtPort int
|
2018-07-17 23:19:03 +02:00
|
|
|
var rpcPort int
|
2018-06-15 04:30:37 +02:00
|
|
|
|
2018-03-24 00:18:00 +01:00
|
|
|
func init() {
|
|
|
|
var cmd = &cobra.Command{
|
2018-07-10 23:30:47 +02:00
|
|
|
Use: "dht [bootstrap|connect]",
|
2018-06-15 04:30:37 +02:00
|
|
|
Short: "Run dht node",
|
|
|
|
ValidArgs: []string{"start", "bootstrap"},
|
|
|
|
Args: argFuncs(cobra.ExactArgs(1), cobra.OnlyValidArgs),
|
|
|
|
Run: dhtCmd,
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|
2018-07-10 23:41:36 +02:00
|
|
|
cmd.PersistentFlags().StringP("nodeID", "n", "", "nodeID in hex")
|
2018-06-15 04:30:37 +02:00
|
|
|
cmd.PersistentFlags().IntVar(&dhtPort, "port", 4567, "Port to start DHT on")
|
2018-07-17 23:19:03 +02:00
|
|
|
cmd.PersistentFlags().IntVar(&rpcPort, "rpc_port", 1234, "Port to listen for rpc commands on")
|
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
|
|
|
|
log.Printf("shutting down bootstrap node")
|
|
|
|
node.Shutdown()
|
|
|
|
} else {
|
2018-07-10 23:41:36 +02:00
|
|
|
nodeIDStr := cmd.Flag("nodeID").Value.String()
|
|
|
|
nodeID := bits.Bitmap{}
|
|
|
|
if nodeIDStr == "" {
|
|
|
|
nodeID = bits.Rand()
|
|
|
|
} else {
|
|
|
|
nodeID = bits.FromHexP(nodeIDStr)
|
|
|
|
}
|
|
|
|
log.Println(nodeID.String())
|
|
|
|
node := dht.NewBootstrapNode(nodeID, 1*time.Millisecond, 1*time.Minute)
|
2018-07-17 23:19:03 +02:00
|
|
|
listener, err := net.ListenPacket(dht.Network, "127.0.0.1:"+strconv.Itoa(dhtPort))
|
2018-07-10 23:41:36 +02:00
|
|
|
checkErr(err)
|
|
|
|
conn := listener.(*net.UDPConn)
|
|
|
|
err = node.Connect(conn)
|
|
|
|
checkErr(err)
|
|
|
|
log.Println("started node")
|
2018-07-13 17:24:11 +02:00
|
|
|
_, _, err = dht.FindContacts(&node.Node, nodeID.Sub(bits.FromBigP(big.NewInt(1))), false, nil)
|
2018-07-17 23:19:03 +02:00
|
|
|
rpcServer := dht.RunRPCServer("127.0.0.1:"+strconv.Itoa(rpcPort), "/", node)
|
2018-07-10 23:41:36 +02:00
|
|
|
interruptChan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(interruptChan, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
|
|
|
|
<-interruptChan
|
|
|
|
rpcServer.Wg.Done()
|
|
|
|
node.Shutdown()
|
2018-04-25 00:12:17 +02:00
|
|
|
}
|
2018-03-24 00:18:00 +01:00
|
|
|
}
|