func (wc *T) handleRequest(w http.ResponseWriter, r *http.Request) { logger.Debug("Got request") btih, _ := bittorrent.BTIDFromHex("0ea39049afdbaaea255ca1d0af662e2a0d503098") if r.URL.Path == "/" { wc.handleIndex(w, r) return } if len(r.URL.Path) < 21 { logger.Error("404 unexpected %v", r.URL.Path) return } btih, err := bittorrent.BTIDFromHex(r.URL.Path[1:41]) if err != nil { logger.Error("got request for invalid BTID: %v", err) return } extension := string(r.URL.Path[41:]) switch extension { case "": wc.handleTorrentPageRequest(w, r, btih) case ".torrent": wc.handleTorrentFileRequest(w, r, btih) return case ".json": wc.handleTorrentJsonInfoRequest(w, r, btih) return default: logger.Error("404 extension %v", extension) return } }
func main() { loggerconfig.Use() if len(os.Args) != 2 { logger.Fatalf("Usage: %v INFOHASH < INFOHASH.peers", os.Args[0]) return } weakrand.Seed(time.Now().UTC().UnixNano()) infoHash, err := bittorrent.BTIDFromHex(os.Args[1]) if err != nil { logger.Fatalf("Specified string was not a valid hex infohash [%v].", err) return } peers := make([]net.TCPAddr, 0) dec := json.NewDecoder(os.Stdin) dec.Decode(&peers) logger.Info("Loaded peers: %v", peers) client := bittorrent.OpenClient() swarm := client.Swarm(infoHash) for _, peer := range peers { swarm.AddPeer(peer) } logger.Info("getting info") info := swarm.Info() logger.Info("got info: %v", info) torrentFileData, err := bencoding.Encode(bencoding.Dict{ "info": info, "announce-list": bencoding.List{}, "nodes": bencoding.List{}, }) if err != nil { logger.Fatalf("error encoding torrent file: %v:", err) } os.Stdout.Write(torrentFileData) }
func main() { loggerconfig.Use() if len(os.Args) != 2 { logger.Fatalf("Usage: %v INFOHASH", os.Args[0]) return } weakrand.Seed(time.Now().UTC().UnixNano()) infoHash, err := bittorrent.BTIDFromHex(os.Args[1]) if err != nil { logger.Fatalf("Specified string was not a valid hex infohash [%v].", err) return } dhtClient, err := dht.OpenClient(".dht-peer", true) if err != nil { logger.Fatalf("Unable to open .dht-peer: %v", err) return } defer dhtClient.Close() search := dhtClient.GetPeers(infoHash) peers, err := search.AllPeers() if err != nil { logger.Fatalf("Unable to find peers: %v", err) } logger.Info("Found peers for %v:", infoHash) peerData, err := json.Marshal(peers) if err != nil { logger.Fatalf("?!?: %v", err) } os.Stdout.Write(peerData) }