public void DecodeExistingFile() { BEncoding decoding = new BEncoding(Tools.GetTestDataFilePath("ubuntu-15.04-desktop-amd64.iso.torrent")); BEncodedDictionary values = (BEncodedDictionary)decoding.Decode(); Assert.AreEqual("http://torrent.ubuntu.com:6969/announce", values["announce"].ToString()); }
/// <summary> /// Charge un torrent depuis un fichier /// </summary> /// <param name="path"></param> public Torrent(string path) : this() { BEncoding decoding = new BEncoding(path); _data = (BEncodedDictionary)decoding.Decode(); //charge le torrent depuis les données récupérés LoadTorrent(); }
/// <summary> /// Makes an announce request to this tracker. /// </summary> /// <param name="request">The announce request object.</param> /// <returns>The announce response.</returns> public override async Task <AnnounceResponse> Announce(AnnounceRequest request) { if (request == null) { throw new ArgumentNullException("request"); } // Check if announces are allowed if (announceUri == null) { return(null); } try { Uri uri = request.GetUri(announceUri, key, trackerID); byte[] responseBytes = await client.GetByteArrayAsync(uri).ConfigureAwait(false); Stats.IncreaseDownloadedBytes(responseBytes.Length); var info = BEncoding.Decode(responseBytes) as BEncoding.Dictionary; if (info == null) { status = TrackerStatus.InvalidResponse; failureMessage = "The tracker returned an invalid announce response."; return(null); } var announceResponse = HandleAnnounceResponse(info); if (announceResponse == null) { status = TrackerStatus.InvalidResponse; failureMessage = "The tracker returned an invalid announce response."; return(null); } failureMessage = announceResponse.FailureReason; warningMessage = announceResponse.WarningMessage; status = TrackerStatus.OK; return(announceResponse); } catch (HttpRequestException ex) { status = TrackerStatus.Offline; failureMessage = string.Format("Failed to perform announce request: {0}", ex.Message); return(null); } catch (Exception ex) { status = TrackerStatus.InvalidResponse; failureMessage = string.Format("Exception performing announce request: {0}", ex.Message); return(null); } }
/// <summary> /// Makes a scrape request to this tracker. /// </summary> /// <param name="infoHashes">The optional array of info hashes. Can be null or empty.</param> /// <returns>The announce response.</returns> public override async Task <ScrapeResponse> Scrape(InfoHash[] infoHashes) { if (infoHashes == null) { throw new ArgumentNullException("infoHashes"); } // Check if scrapes are allowed if (scrapeUri == null) { return(null); } try { var queryParameters = new KeyValuePair <string, object> [infoHashes.Length]; for (int i = 0; i < infoHashes.Length; i++) { queryParameters[i] = new KeyValuePair <string, object>("info_hash", infoHashes[i].ToUrlEncodedString()); } var uri = UriHelper.AppendQueryString(scrapeUri, queryParameters); byte[] responseBytes = await client.GetByteArrayAsync(uri).ConfigureAwait(false); Stats.IncreaseDownloadedBytes(responseBytes.Length); var info = BEncoding.Decode(responseBytes) as BEncoding.Dictionary; if (info == null) { failureMessage = "The tracker returned an invalid scrape response."; return(null); } var scrapeResponse = HandleScrapeResponse(info); if (scrapeResponse == null) { failureMessage = "The tracker returned an invalid scrape response."; return(null); } return(scrapeResponse); } catch (HttpRequestException ex) { failureMessage = string.Format("Failed to perform scrape request: {0}", ex.Message); return(null); } catch (Exception ex) { failureMessage = string.Format("Exception performing scrape request: {0}", ex.Message); return(null); } }
/// <summary> /// Loads torrent information from a stream. /// </summary> /// <param name="stream">The stream to load the torrent from.</param> public void LoadFromStream(Stream stream) { if (stream == null) { throw new ArgumentNullException("stream"); } else if (!stream.CanRead) { throw new ArgumentException("The stream cannot be read from.", "stream"); } var torrentInfo = BEncoding.Decode(stream) as BEncoding.Dictionary; if (torrentInfo == null) { throw new InvalidDataException("The specified file does not appear to include torrent information."); } Load(torrentInfo); }
/// <summary> /// /// </summary> public void Request() { string encoded_hash = Torrent.GetEncodedInfoHash(); //construction de la requête vers le tracker StringBuilder builder = new StringBuilder(Tracker.Url); builder.AppendFormat("?info_hash={0}", encoded_hash); builder.Append("&peer_id=adkiepeycosozpsngtoi"); builder.Append("&uploaded=0"); builder.Append("&downloaded=0"); builder.AppendFormat("&compact={0}", Compact ? 1 : 0); builder.Append("&left=120000"); builder.Append("&event=started"); builder.Append("&port=6881"); //création de la requête GET HttpWebRequest request = (HttpWebRequest)WebRequest.Create(builder.ToString()); request.Method = "GET"; //envoi de la requête using (WebResponse response = request.GetResponse()) { //récupération de la réponse using (Stream stream = response.GetResponseStream()) { using (var reader = new StreamReader(stream, Encoding.Default)) { string responseText = reader.ReadToEnd(); byte[] data = Encoding.Default.GetBytes(responseText); BEncoding encoding = new BEncoding(data); BEncodedDictionary dictionary = (BEncodedDictionary)encoding.Decode(); Complete = (BEncodedInteger)dictionary["complete"]; Incomplete = (BEncodedInteger)dictionary["incomplete"]; Interval = (BEncodedInteger)dictionary["interval"]; // la liste des peers peut être soit une liste, soit une chaine simplifiée en big endian if (dictionary["peers"] is BEncodedList) { BEncodedList peers = (BEncodedList)dictionary["peers"]; } else if (dictionary["peers"] is BEncodedString) { byte[] peers = Encoding.Default.GetBytes((BEncodedString)dictionary["peers"]); for (int i = 0; i < peers.Length; i = i + 6) { byte[] ip = new byte[4]; byte[] port = new byte[2]; Array.Copy(peers, i, ip, 0, 4); Array.Copy(peers, i + 4, port, 0, 2); Array.Reverse(port); IPEndPoint ipEndPoint = new IPEndPoint(new IPAddress(ip), BitConverter.ToUInt16(port, 0)); Peer peer = new Peer(ipEndPoint); Peers.Add(peer); } } } } } }