diff --git a/cmd/dht-get-peers/main.go b/cmd/dht-get-peers/main.go deleted file mode 100644 index d72c34e5..00000000 --- a/cmd/dht-get-peers/main.go +++ /dev/null @@ -1,165 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "io" - "log" - "net" - "os" - "os/signal" - - "github.com/anacrolix/dht" - "github.com/anacrolix/dht/krpc" - _ "github.com/anacrolix/envpprof" -) - -var ( - tableFileName = flag.String("tableFile", "", "name of file for storing node info") - serveAddr = flag.String("serveAddr", ":0", "local UDP address") - infoHash = flag.String("infoHash", "", "torrent infohash") - once = flag.Bool("once", false, "only do one scrape iteration") - - s *dht.Server - quitting = make(chan struct{}) -) - -func loadTable() error { - if *tableFileName == "" { - return nil - } - f, err := os.Open(*tableFileName) - if os.IsNotExist(err) { - return nil - } - if err != nil { - return fmt.Errorf("error opening table file: %s", err) - } - defer f.Close() - added := 0 - for { - b := make([]byte, krpc.CompactIPv4NodeInfoLen) - _, err := io.ReadFull(f, b) - if err == io.EOF { - break - } - if err != nil { - return fmt.Errorf("error reading table file: %s", err) - } - var ni krpc.NodeInfo - err = ni.UnmarshalCompactIPv4(b) - if err != nil { - return fmt.Errorf("error unmarshaling compact node info: %s", err) - } - s.AddNode(ni) - added++ - } - log.Printf("loaded %d nodes from table file", added) - return nil -} - -func init() { - log.SetFlags(log.LstdFlags | log.Lshortfile) - flag.Parse() - switch len(*infoHash) { - case 20: - case 40: - _, err := fmt.Sscanf(*infoHash, "%x", infoHash) - if err != nil { - log.Fatal(err) - } - default: - log.Fatal("require 20 byte infohash") - } - var err error - s, err = dht.NewServer(&dht.ServerConfig{ - Addr: *serveAddr, - }) - if err != nil { - log.Fatal(err) - } - err = loadTable() - if err != nil { - log.Fatalf("error loading table: %s", err) - } - log.Printf("dht server on %s, ID is %x", s.Addr(), s.ID()) - setupSignals() -} - -func saveTable() error { - goodNodes := s.Nodes() - if *tableFileName == "" { - if len(goodNodes) != 0 { - log.Print("good nodes were discarded because you didn't specify a table file") - } - return nil - } - f, err := os.OpenFile(*tableFileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) - if err != nil { - return fmt.Errorf("error opening table file: %s", err) - } - defer f.Close() - for _, nodeInfo := range goodNodes { - var b [krpc.CompactIPv4NodeInfoLen]byte - err := nodeInfo.PutCompact(b[:]) - if err != nil { - return fmt.Errorf("error compacting node info: %s", err) - } - _, err = f.Write(b[:]) - if err != nil { - return fmt.Errorf("error writing compact node info: %s", err) - } - } - log.Printf("saved %d nodes to table file", len(goodNodes)) - return nil -} - -func setupSignals() { - ch := make(chan os.Signal) - signal.Notify(ch, os.Interrupt) - go func() { - <-ch - close(quitting) - }() -} - -func main() { - seen := make(map[string]struct{}) -getPeers: - for { - var ih [20]byte - copy(ih[:], *infoHash) - ps, err := s.Announce(ih, 0, false) - if err != nil { - log.Fatal(err) - } - values: - for { - select { - case v, ok := <-ps.Peers: - if !ok { - break values - } - log.Printf("received %d peers from %x", len(v.Peers), v.NodeInfo.ID) - for _, p := range v.Peers { - if _, ok := seen[p.String()]; ok { - continue - } - seen[p.String()] = struct{}{} - fmt.Println((&net.UDPAddr{ - IP: p.IP[:], - Port: int(p.Port), - }).String()) - } - case <-quitting: - break getPeers - } - } - if *once { - break - } - } - if err := saveTable(); err != nil { - log.Printf("error saving node table: %s", err) - } -} diff --git a/cmd/dht-ping/main.go b/cmd/dht-ping/main.go deleted file mode 100644 index fb12f844..00000000 --- a/cmd/dht-ping/main.go +++ /dev/null @@ -1,93 +0,0 @@ -// Pings DHT nodes with the given network addresses. -package main - -import ( - "fmt" - "log" - "math" - "net" - "os" - "time" - - "github.com/anacrolix/dht" - "github.com/anacrolix/dht/krpc" - "github.com/anacrolix/tagflag" - "github.com/bradfitz/iter" -) - -func main() { - log.SetFlags(log.LstdFlags | log.Lshortfile) - var args = struct { - Timeout time.Duration - Nodes []string `type:"pos" arity:"+" help:"nodes to ping e.g. router.bittorrent.com:6881"` - }{ - Timeout: math.MaxInt64, - } - tagflag.Parse(&args) - s, err := dht.NewServer(nil) - if err != nil { - log.Fatal(err) - } - log.Printf("dht server on %s", s.Addr()) - timeout := time.After(args.Timeout) - pongChan := make(chan pong) - startPings(s, pongChan, args.Nodes) - numResp := receivePongs(pongChan, timeout, len(args.Nodes)) - fmt.Printf("%d/%d responses (%f%%)\n", numResp, len(args.Nodes), 100*float64(numResp)/float64(len(args.Nodes))) -} - -func receivePongs(pongChan chan pong, timeout <-chan time.Time, maxPongs int) (numResp int) { - for range iter.N(maxPongs) { - select { - case pong := <-pongChan: - if !pong.msgOk { - break - } - numResp++ - fmt.Printf("%-65s %s\n", fmt.Sprintf("%x (%s):", pong.krpc.SenderID(), pong.addr), pong.rtt) - case <-timeout: - fmt.Fprintf(os.Stderr, "timed out\n") - return - } - } - return -} - -func startPings(s *dht.Server, pongChan chan pong, nodes []string) { - for i, addr := range nodes { - if i != 0 { - // Put a small sleep between pings to avoid network issues. - time.Sleep(1 * time.Millisecond) - } - ping(addr, pongChan, s) - } -} - -type pong struct { - addr string - krpc krpc.Msg - msgOk bool - rtt time.Duration -} - -func ping(netloc string, pongChan chan pong, s *dht.Server) { - addr, err := net.ResolveUDPAddr("udp4", netloc) - if err != nil { - log.Fatal(err) - } - t, err := s.Ping(addr) - if err != nil { - log.Fatal(err) - } - start := time.Now() - t.SetResponseHandler(func(addr string) func(krpc.Msg, bool) { - return func(resp krpc.Msg, ok bool) { - pongChan <- pong{ - addr: addr, - krpc: resp, - rtt: time.Now().Sub(start), - msgOk: ok, - } - } - }(netloc)) -} diff --git a/cmd/dht-secure-id/main.go b/cmd/dht-secure-id/main.go deleted file mode 100644 index 7add97e2..00000000 --- a/cmd/dht-secure-id/main.go +++ /dev/null @@ -1,33 +0,0 @@ -package main - -import ( - "encoding/hex" - "fmt" - "net" - "os" - - "github.com/anacrolix/dht" - "github.com/docopt/docopt-go" -) - -func main() { - args, _ := docopt.Parse(`dht-secure-id outputs the node ID secured with the IP. - -Usage: dht-secure-id `, nil, true, "", false) - id, err := hex.DecodeString(args[""].(string)) - if err != nil { - fmt.Fprintf(os.Stderr, "bad id: %s\n", err) - os.Exit(2) - } - if len(id) != 20 { - fmt.Fprintf(os.Stderr, "bad id: wrong length\n") - os.Exit(2) - } - ip := net.ParseIP(args[""].(string)) - if ip == nil { - fmt.Fprintf(os.Stderr, "bad ip\n") - os.Exit(2) - } - dht.SecureNodeId(id, ip) - fmt.Printf("%x\n", id) -} diff --git a/cmd/dht-server/main.go b/cmd/dht-server/main.go deleted file mode 100644 index c31bcd8e..00000000 --- a/cmd/dht-server/main.go +++ /dev/null @@ -1,116 +0,0 @@ -package main - -import ( - "flag" - "fmt" - "io" - "log" - "os" - "os/signal" - - "github.com/anacrolix/dht" - "github.com/anacrolix/dht/krpc" -) - -var ( - tableFileName = flag.String("tableFile", "", "name of file for storing node info") - serveAddr = flag.String("serveAddr", ":0", "local UDP address") - - s *dht.Server -) - -func loadTable() error { - if *tableFileName == "" { - return nil - } - f, err := os.Open(*tableFileName) - if os.IsNotExist(err) { - return nil - } - if err != nil { - return fmt.Errorf("error opening table file: %s", err) - } - defer f.Close() - added := 0 - for { - b := make([]byte, krpc.CompactIPv4NodeInfoLen) - _, err := io.ReadFull(f, b) - if err == io.EOF { - break - } - if err != nil { - return fmt.Errorf("error reading table file: %s", err) - } - var ni krpc.NodeInfo - err = ni.UnmarshalCompactIPv4(b) - if err != nil { - return fmt.Errorf("error unmarshaling compact node info: %s", err) - } - s.AddNode(ni) - added++ - } - log.Printf("loaded %d nodes from table file", added) - return nil -} - -func init() { - log.SetFlags(log.LstdFlags | log.Lshortfile) - flag.Parse() - var err error - s, err = dht.NewServer(&dht.ServerConfig{ - Addr: *serveAddr, - }) - if err != nil { - log.Fatal(err) - } - err = loadTable() - if err != nil { - log.Fatalf("error loading table: %s", err) - } - log.Printf("dht server on %s, ID is %x", s.Addr(), s.ID()) -} - -func saveTable() error { - goodNodes := s.Nodes() - if *tableFileName == "" { - if len(goodNodes) != 0 { - log.Printf("discarding %d good nodes!", len(goodNodes)) - } - return nil - } - f, err := os.OpenFile(*tableFileName, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666) - if err != nil { - return fmt.Errorf("error opening table file: %s", err) - } - defer f.Close() - for _, nodeInfo := range goodNodes { - var b [krpc.CompactIPv4NodeInfoLen]byte - err := nodeInfo.PutCompact(b[:]) - if err != nil { - return fmt.Errorf("error compacting node info: %s", err) - } - _, err = f.Write(b[:]) - if err != nil { - return fmt.Errorf("error writing compact node info: %s", err) - } - } - log.Printf("saved %d nodes to table file", len(goodNodes)) - return nil -} - -func setupSignals() { - ch := make(chan os.Signal) - signal.Notify(ch) - <-ch - s.Close() - - if *tableFileName != "" { - if err := saveTable(); err != nil { - log.Printf("error saving node table: %s", err) - } - } -} - -func main() { - setupSignals() -}