Example #1
0
        public void CheckFileTypeTorrent()
        {
            try {

                BEncoding encoding = new BEncoding("fichierquiexistepas.torrent");
                Assert.Fail("An exception should have been thrown");

            } catch(FileNotFoundException ex) {
                Assert.AreEqual("The given file was not found.", ex.Message);
            } catch (Exception ex) {
                Assert.Fail(string.Format( "Unexpected exception of type {0} caught: {1}", ex.GetType(), ex.Message));
            }
        }
Example #2
0
        /// <summary>
        /// Décode une liste BEncode
        /// </summary>
        /// <param name="stack"></param>
        public override void Decode(Queue <byte> queue)
        {
            //on supprime la 1ère valeur qui est égale à 'l', qui permettait d'identifier le type de BEncode
            char ch = (char)queue.Dequeue();

            //on récupère chacun des caractères tant que nous ne sommes pas à la fin de la queue
            while ((ch = (char)queue.Peek()) != 'e')
            {
                //on récupère la valeur
                BEncoding     decoding = new BEncoding(queue);
                BEncodedValue value    = decoding.Decode();

                //on ajoute la valeur à la liste
                Value.Add(value);
            }

            //suppression de la lettre de fin 'e'
            queue.Dequeue();
        }
Example #3
0
        /// <summary>
        /// Décode un dictionnaire BEncoded
        /// </summary>
        /// <param name="stack"></param>
        public override void Decode(Queue <byte> queue)
        {
            //on supprime la 1ère valeur qui est égale à 'd', qui permettait d'identifier le type de BEncode
            char ch = (char)queue.Dequeue();

            //on récupère chacun des caractères tant que nous ne sommes pas à la fin de la queue
            while ((ch = (char)queue.Peek()) != 'e')
            {
                //on récupère la clé
                BEncoding      decodingKey = new BEncoding(queue);
                BEncodedString key         = (BEncodedString)decodingKey.Decode();

                //on récupère la valeur
                BEncoding     decodingValue = new BEncoding(queue);
                BEncodedValue value         = decodingValue.Decode();

                //on ajoute la clé / valeur au dico final
                Value.Add(key, value);
            }

            //suppression de la lettre de fin 'e'
            queue.Dequeue();
        }
Example #4
0
        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());
        }
Example #5
0
 public void CheckFileTypeNotTorrent()
 {
     BEncoding encoding = new BEncoding("fichierquiexistepas.extension");
 }
Example #6
0
 public void CheckFilePathEmpty()
 {
     BEncoding encoding = new BEncoding(string.Empty);
 }
Example #7
0
 public void CheckFileExists()
 {
     BEncoding encoding = new BEncoding("fichierquiexistepas.torrent");
 }
Example #8
0
 public void CheckDataEmpty()
 {
     BEncoding encoding = new BEncoding(new byte[0]);
 }
Example #9
0
        /// <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);
                            }
                        }
                    }
                }
            }
        }