protected void Page_Load(object sender, EventArgs e) { Dictionary <BENObject, BENObject> dict = new Dictionary <BENObject, BENObject>(); BENObject obj = new BENObject(dict); Dictionary <BENObject, BENObject> files = new Dictionary <BENObject, BENObject>(); dict[new BENObject("files")] = new BENObject(files); string info_hash = bin2hex(queryBin("info_hash=", Request.RawUrl)); if (info_hash != null && info_hash.Length == 40) { try { files[new BENObject(hex2bin(info_hash))] = new BENObject(handleSha(info_hash)); } catch { } } else { try { using (SqlConnection myConnection = new SqlConnection(DatabaseConnectionString)) { myConnection.Open(); string stmt = "SELECT sha FROM torrents;"; using (SqlCommand cmdCount = new SqlCommand(stmt, myConnection)) { using (SqlDataReader reader = cmdCount.ExecuteReader()) { while (reader.Read()) { string sha = reader.GetString(0); try { files[new BENObject(hex2bin(info_hash))] = new BENObject(handleSha(info_hash)); } catch { } } } } } } catch { } } Response.BinaryWrite(obj.ToBytes()); }
protected void Page_Load(object sender, EventArgs e) { BENObject obj = new BENObject(new Dictionary <BENObject, BENObject>()); if (Request.QueryString["info_hash"] == null) { obj.getDictonary()[new BENObject("failure reason")] = new BENObject("Missing info_hash"); obj.getDictonary()[new BENObject("failure code")] = new BENObject(100); Response.Write(obj.ToString()); return; } if (Request.QueryString["peer_id"] == null) { obj.getDictonary()[new BENObject("failure reason")] = new BENObject("Missing peer_id"); obj.getDictonary()[new BENObject("failure code")] = new BENObject(102); Response.Write(obj.ToString()); return; } if (Request.QueryString["port"] == null) { obj.getDictonary()[new BENObject("failure reason")] = new BENObject("Missing port"); obj.getDictonary()[new BENObject("failure code")] = new BENObject(103); Response.Write(obj.ToString()); return; } string sha = bin2hex(queryBin("info_hash=", Request.RawUrl)); string peer_id = bin2hex(queryBin("peer_id=", Request.RawUrl)); if (sha != null && sha.Length != 40) { obj.getDictonary()[new BENObject("failure reason")] = new BENObject("Invalid infohash: infohash is not 20 bytes long"); obj.getDictonary()[new BENObject("failure code")] = new BENObject(150); Response.Write(obj.ToString()); return; } if (peer_id != null && peer_id.Length != 40) { obj.getDictonary()[new BENObject("failure reason")] = new BENObject("Invalid peerid: peerid is not 20 bytes long"); obj.getDictonary()[new BENObject("failure code")] = new BENObject(151); Response.Write(obj.ToString()); return; } try { using (SqlConnection myConnection = new SqlConnection(DatabaseConnectionString)) { string stmt; myConnection.Open(); int interval; if (Int32.TryParse(ConfigurationManager.AppSettings["config_interval"], out interval) == false) { interval = 300; } stmt = "DELETE FROM peers WHERE date < DATEADD(SECOND, -@interval, GETDATE())"; using (SqlCommand command = new SqlCommand(stmt, myConnection)) { command.Parameters.AddWithValue("interval", (int)(interval * 1.2)); command.ExecuteScalar(); } string ip; if (Request.QueryString["ip"] != null) { ip = Request.QueryString["ip"]; } else { ip = GetIPAddress(); } int port; Int32.TryParse(Request.QueryString["port"], out port); if (Request.QueryString["event"] != null && Request.QueryString["event"].Equals("stopped")) { stmt = "DELETE FROM peers WHERE sha=@sha AND port=@port AND peer_id=@peer_id"; using (SqlCommand command = new SqlCommand(stmt, myConnection)) { command.Parameters.AddWithValue("sha", sha); command.Parameters.AddWithValue("peer_id", peer_id); command.Parameters.AddWithValue("port", port); command.ExecuteNonQuery(); return; } } if (Request.QueryString["event"] != null && Request.QueryString["event"].Equals("completed")) { stmt = "UPDATE torrents SET downloaded = downloaded + 1 WHERE sha=@sha"; using (SqlCommand command = new SqlCommand(stmt, myConnection)) { command.Parameters.AddWithValue("sha", sha); command.ExecuteNonQuery(); } } int numwant; int numwant_max; if (Int32.TryParse(Request.QueryString["numwant"], out numwant) == false) { numwant = 50; } if (Int32.TryParse(ConfigurationManager.AppSettings["config_max_numwant"], out numwant_max) == false) { numwant_max = 200; } if (numwant > numwant_max) { obj.getDictonary()[new BENObject("failure reason")] = new BENObject("Invalid numwant.Client requested more peers than allowed by tracker."); obj.getDictonary()[new BENObject("failure code")] = new BENObject(152); Response.Write(obj.ToString()); return; } long uploaded; long downloaded; long left; Int64.TryParse(Request.QueryString["uploaded"], out uploaded); Int64.TryParse(Request.QueryString["downloaded"], out downloaded); Int64.TryParse(Request.QueryString["left"], out left); stmt = "SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;\n" + "BEGIN TRANSACTION;\n" + "UPDATE peers SET peer_id=@peer_id, date=@date, uploaded=@uploaded, downloaded=@downloaded, dl_left=@dl_left WHERE sha=@sha AND ip=@ip AND port=@port\n" + "IF @@ROWCOUNT = 0\n" + "INSERT INTO peers(sha, ip, port, peer_id, uploaded, downloaded, dl_left) VALUES(@sha, @ip, @port, @peer_id, @uploaded, @downloaded, @dl_left)\n" + "COMMIT TRANSACTION;"; try { using (SqlCommand command = new SqlCommand(stmt, myConnection)) { command.Parameters.AddWithValue("sha", sha); command.Parameters.AddWithValue("peer_id", peer_id); command.Parameters.AddWithValue("ip", ip); command.Parameters.AddWithValue("port", port); command.Parameters.AddWithValue("date", DateTime.Now); command.Parameters.AddWithValue("uploaded", uploaded); command.Parameters.AddWithValue("downloaded", downloaded); command.Parameters.AddWithValue("dl_left", left); command.ExecuteNonQuery(); } } catch { obj.getDictonary()[new BENObject("failure reason")] = new BENObject("info_hash not found in the database"); obj.getDictonary()[new BENObject("failure code")] = new BENObject(200); Response.Write(obj.ToString()); return; } int no_peer_id; Int32.TryParse(Request.QueryString["no_peer_id"], out no_peer_id); no_peer_id = 0; obj.getDictonary()[new BENObject("interval")] = new BENObject(interval); List <BENObject> peers = new List <BENObject>(); obj.getDictonary()[new BENObject("peers")] = new BENObject(peers); if (numwant > 0) { if (no_peer_id == 0) { stmt = String.Format("SELECT TOP {0} ip, port, peer_id FROM peers WHERE sha=@sha;", numwant); } else { stmt = String.Format("SELECT TOP {0} ip, port FROM peers WHERE sha=@sha;", numwant); } using (SqlCommand command = new SqlCommand(stmt, myConnection)) { command.Parameters.AddWithValue("sha", sha); using (SqlDataReader reader = command.ExecuteReader()) { BENObject ben_peer_id = new BENObject("peer id"); BENObject ben_ip = new BENObject("ip"); BENObject ben_port = new BENObject("port"); while (reader.Read()) { Dictionary <BENObject, BENObject> dic = new Dictionary <BENObject, BENObject>(); if (no_peer_id == 0) { dic.Add(ben_peer_id, new BENObject(hex2bin(reader.GetString(2)))); } dic.Add(ben_ip, new BENObject(reader.GetString(0))); dic.Add(ben_port, new BENObject(reader.GetInt32(1))); peers.Add(new BENObject(dic)); } } } } Response.BinaryWrite(obj.ToBytes()); } } catch { obj.getDictonary()[new BENObject("failure reason")] = new BENObject("Generic error"); obj.getDictonary()[new BENObject("failure code")] = new BENObject(900); Response.Write(obj.ToString()); } }
protected void Button1_Click(object sender, EventArgs e) { if (fileToUpload.HasFile) { Stream fileStream; int length = fileToUpload.PostedFile.ContentLength; Byte[] Input = new Byte[length]; fileStream = fileToUpload.FileContent; fileStream.Read(Input, 0, length); BENObject obj; try { obj = new BENObject(ref Input); } catch { BotLabel.Text = "Torrent parsing fail"; return; } if (is_valid_torrent(ref obj)) { obj.getDictonary()[new BENObject("announce")] = new BENObject(ConfigurationManager.AppSettings["config_announce"]); obj.getDictonary().Remove(new BENObject("announce-list")); BENObject info = obj.getDictonary()[new BENObject("info")]; SHA1 sha_crypto = new SHA1CryptoServiceProvider(); byte[] result = sha_crypto.ComputeHash(info.ToBytes()); string sha = BitConverter.ToString(result).Replace("-", string.Empty); BotLabel.Text = sha + "<br />"; try { using (SqlConnection myConnection = new SqlConnection(DatabaseConnectionString)) { myConnection.Open(); string stmt = "INSERT INTO torrents (sha, name, username) values(@sha, @name, @username);"; using (SqlCommand myCommand = new SqlCommand(stmt, myConnection)) { myCommand.Parameters.AddWithValue("sha", sha); myCommand.Parameters.AddWithValue("name", info.getDictonary()[new BENObject("name")].getString()); myCommand.Parameters.AddWithValue("username", (String)Session["username"]); myCommand.ExecuteNonQuery(); } } } catch (Exception ex) { BotLabel.Text += "Error: " + ex.Message; return; } try { string file_path = String.Format("{0}/{1}.torrent", ConfigurationManager.AppSettings["config_upload_dir"], sha); FileInfo file = new FileInfo(Server.MapPath(file_path)); file.Directory.Create(); File.WriteAllBytes(file.FullName, obj.ToBytes()); BotLabel.Text += "New record created successfully"; } catch (Exception ex) { try { using (SqlConnection myConnection = new SqlConnection(DatabaseConnectionString)) { myConnection.Open(); string stmt = "DELETE FROM torrents WHERE sha=@sha;"; using (SqlCommand cmdCount = new SqlCommand(stmt, myConnection)) { cmdCount.Parameters.AddWithValue("sha", sha); cmdCount.ExecuteNonQuery(); } } } catch { } BotLabel.Text += "Error: " + ex.Message; } } else { BotLabel.Text = "Torrent validation fail"; } } }