/// <summary> /// Sends a request to tracker. /// </summary> /// <remarks> /// SendTrackerRequest() /// /// SYNOPSIS /// /// void SendTrackerRequest() /// /// DESCRIPTION /// /// This function will send and receive a tracker response. Once a /// response is received it will decode the response and if the /// response is valid, it will call the base class event. /// /// </remarks> private void SendTrackerRequest() { try { // Sends request and gets response. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(EncodeTrackerRequest()); HttpWebResponse response = (HttpWebResponse)request.GetResponse(); // Copy response. var responseStream = response.GetResponseStream(); var memoryStream = new MemoryStream(); responseStream.CopyTo(memoryStream); byte[] rawTrackerResponse = memoryStream.ToArray(); // Decode and parse response. var decodedResponse = Bencode.BDecode(rawTrackerResponse).ElementAt(0).Value; if (ParseResponse(decodedResponse)) { // Call the base Torrent class invocation method. base.OnPeerListUpdated(); } } catch { } }
public void TestMultiFileTorrentWithErrorDecode() { Bencode bEncode = new Bencode(); byte[] expected = System.IO.File.ReadAllBytes(Constants.MultiFileWithErrorTorrent); Assert.Throws <Exception>(() => { BNodeBase torrentBase = bEncode.Decode(expected); }); }
static void Main(string[] args) { string test = @"C:\Users\dotnet\Downloads\torr.torrent"; Console.WriteLine("Torrent file path:"); String torrentfile = test; TorrentFile torrent = Bencode.DecodeTorrentFile(torrentfile); Console.WriteLine("Info in torrentfile:"); Console.WriteLine(torrent.Announce); foreach (var St in torrent.AnnounceList) { Console.WriteLine(St.ToString()); } Console.WriteLine(torrent.Comment); Console.WriteLine(torrent.CreatedBy); Console.WriteLine(torrent.CreationDate); Console.WriteLine(torrent.Encoding); BDictionary info = (BDictionary)torrent["info"]; BList files = (BList)info["files"]; foreach (BDictionary v in files) { BList path = (BList)v["path"]; Console.WriteLine("Path: {0}, length: {1}", path[0], v["length"]); } Console.ReadLine(); }
public void TestDecodeThenEncodeTheSamString(string expected) { Bencode bEncode = new Bencode(); BNodeBase bNode = bEncode.Decode(Encoding.ASCII.GetBytes(expected)); byte[] actual = bEncode.Encode(bNode); Assert.Equal(Encoding.ASCII.GetBytes(expected), actual); }
public void TestMultiFileTorrentDecodeEncodeCheckTheSameAfter() { Bencode bEncode = new Bencode(); byte[] expected = System.IO.File.ReadAllBytes(Constants.MultiFileTorrent); BNodeBase torrentBase = bEncode.Decode(expected); byte[] actual = bEncode.Encode(torrentBase); Assert.Equal(expected, actual); }
public void Connect(IPAddress ip, int port, bool useUdp) { IPEndPoint ipep = new IPEndPoint(ip, port); if (!useUdp) { Socket s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.IP); s.Connect(ipep); MemoryStream ms = new MemoryStream(); Bencode.write("hello", ms); s.Send(ms.ToArray(), SocketFlags.None); } }
private void button1_Click(object sender, EventArgs e) { OpenFileDialog dialog = new OpenFileDialog(); dialog.Title = "Open Torrent File"; dialog.Filter = "Torrent files (*.torrent)|*.torrent"; if (dialog.ShowDialog() != DialogResult.OK) { return; } torrentName = dialog.FileName; torrentSize = 0; fileList.Clear(); listBoxInfo.Items.Clear(); textBox1.Text = torrentName; TorrentFile torrent = Bencode.DecodeTorrentFile(torrentName); add_info(torrent.Info["name"].ToString()); if (torrent.Info["source"] != null) { add_info(torrent.Info["source"].ToString()); } add_info(torrent.Comment); add_info(torrent.CreatedBy); add_info(torrent.CreationDate); fileNum = 0; BList files = (BList)torrent.Info["files"]; foreach (BDictionary file in files) { //add_info(file["length"].ToString()); BList path = (BList)file["path"]; string spath = ""; foreach (BString elem in path) { spath += "\\" + elem; } fileNum++; Int64 sz = Int64.Parse(file["length"].ToString()); torrentSize += sz; fileList.Add(spath, sz); } add_info(fileNum.ToString() + " file(s), " + FormatSize(torrentSize)); }
public void Create() { IPEndPoint ipep = new IPEndPoint(ip, port); Socket s = new Socket(ip.AddressFamily, SocketType.Stream, ProtocolType.IP); s.Bind(ipep); s.Listen(-1); Socket client = s.Accept(); MemoryStream ms = new MemoryStream(); Dictionary <string, object> helloMsg = new Dictionary <string, object>(); helloMsg.Add("ver", 1); helloMsg.Add("token", "mi"); Bencode.write(helloMsg, ms); }
private async void HandleClient(Socket client) { try { using var stream = new NetworkStream(client, ownsSocket: true) { ReadTimeout = 5000, WriteTimeout = 5000, }; BDict dict = (BDict)await Bencode.DecodeAsync(stream, this.client.NetworkPrefix); await this.client.P2P.TcpServer_GetValue(stream, dict); } catch { } }
public void ReadTorrentFile(string localfile) { TorrentFile torrent = Bencode.DecodeTorrentFile(localfile); Byte[] bytes = File.ReadAllBytes(localfile); String base64string = Convert.ToBase64String(bytes); this.MetaInfo = base64string; if (torrent.Info.ContainsKey("name")) { this.TorrentName = ((BString)torrent.Info["name"]).ToString(Encoding.UTF8); } this.TorrentComment = torrent.Comment; this.TorrentCreatedBy = torrent.CreatedBy; this.TorrentDate = torrent.CreationDate; long piecelen = (BNumber)torrent.Info["piece length"]; //var pieces = (BList)torrent.Info["pieces"]; ObservableCollection <Model.TorrentFileName> filelist = new ObservableCollection <Model.TorrentFileName>(); if (torrent.Info.ContainsKey("files")) { BList files = (BList)torrent.Info["files"]; foreach (BDictionary file in files) { GetTorrentFileList(filelist, file); } } else { GetTorrentFileList(filelist, torrent.Info); } this.TorrentFiles = filelist; RaisePropertyChanged("SelectedSize"); RaisePropertyChanged("TotalSize"); }
public void AddTorrent(string path) { string ext = Path.GetExtension(path); if (!File.Exists(path) || Path.GetExtension(path) != ".torrent") { Logman.Log("Incorrect torrent file specified. Check the path and extension of the file.", LOG_TYPE.WARNING); return; } TorrentFile torrent = Bencode.DecodeTorrentFile(path); Torrent newTorrent = new Torrent(torrent.CalculateInfoHash(), Path.GetFileNameWithoutExtension(path), torrent.Announce); torrentList.Add(newTorrent); newTorrentList.Add(newTorrent); Logman.Log("Torrent added to database."); Database.Save(torrentList.ToArray()); if (TorrentUpdateThresholdReached != null) { TorrentUpdateThresholdReached.Invoke(newTorrentList.ToArray()); } }
public void ReadLocalTorrentFile() { bool isMultiFile; //string file = @"C:\Users\dan.PARADOX\Downloads\ubuntu-14.10-desktop-amd64.iso.torrent"; string localfile = @"C:\Users\dan.PARADOX\Downloads\[kat.cr]deadpool.2016.1080p.bluray.x264.dts.jyk.torrent"; TorrentFile torrent = Bencode.DecodeTorrentFile(localfile); List <string> filelist = new List <string>(); if (torrent.Info.ContainsKey("files")) { BList files = (BList)torrent.Info["files"]; foreach (BDictionary file in files) { //http://stackoverflow.com/questions/32067409/decode-bencode-torrent-files // File size in bytes (BNumber has implicit conversion to int and long) int size = (BNumber)file["length"]; // List of all parts of the file path. 'dir1/dir2/file.ext' => dir1, dir2 and file.ext BList path = (BList)file["path"]; string fullpath = String.Join("|", path); // Last element is the file name BString fileName = (BString)path.Last(); // Converts fileName (BString = bytes) to a string string fileNameString = fileName.ToString(Encoding.UTF8); } } else { filelist.Add(torrent.Info["name"].ToString()); } }