func Scrape(path string, ih [][20]byte) (*Files, error) { scraper, err := url.Parse(path) if err != nil { return nil, err } v, err := url.ParseQuery(scraper.RawQuery) if err != nil { return nil, err } for _, infoHash := range ih { v.Add("info_hash", string(infoHash[:])) } scraper.RawQuery = v.Encode() res, err := http.Get(scraper.String()) if err != nil { return nil, nil } fmt.Println(scraper.String()) files := &Files{} err = bencode.Unmarshal(res.Body, files) if err != nil { return nil, err } return files, nil }
func TestTracker(t *testing.T) { mi, err := ReadFile("./testdata/single_file.torrent") if err != nil { t.Fatal(err) } treq := &TrackerReq{ InfoHash: mi.InfoHash(), PeerId: BTPeerId(), Port: 6885, Uploaded: 0, Downloaded: 0, Left: 0, Compact: 1, } url := fmt.Sprintf("%s?%s", mi.Announce, treq.UrlParams()) fmt.Println(url) res, err := http.Get(url) if err != nil { t.Fatal(err) } defer res.Body.Close() tres := &TrackerRes{} err = bencode.Unmarshal(res.Body, tres) if err != nil { t.Fatal(err) } for _, peer := range tres.ListPeers() { fmt.Printf("%s:%s:%d\n", peer.PeerId, peer.IP, peer.Port) } files, err := Scrape(mi.ScraperUrl()[0], [][20]byte{mi.InfoHash()}) if err != nil { t.Fatal(err) } fmt.Println(files) }
func TestWire(t *testing.T) { mi, err := ReadFile("./testdata/single_file.torrent") if err != nil { t.Fatal(err) } fmt.Println(mi) treq := &TrackerReq{ InfoHash: mi.InfoHash(), PeerId: BTPeerId(), Port: 6885, Uploaded: 0, Downloaded: 0, Left: 0, Compact: 1, } url := fmt.Sprintf("%s?%s", mi.Announce, treq.UrlParams()) fmt.Println(url) res, err := http.Get(url) if err != nil { t.Fatal(err) } defer res.Body.Close() tres := &TrackerRes{} err = bencode.Unmarshal(res.Body, tres) if err != nil { t.Fatal(err) } stop, err := Handshake(tres.ListPeers()[0], mi.InfoHash()) if err != nil { t.Fatal(err) } fmt.Printf("Handler: %s\n", <-stop) }