-
Notifications
You must be signed in to change notification settings - Fork 1
/
upload.aspx.cs
122 lines (116 loc) · 5.35 KB
/
upload.aspx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
using System.Text;
using System.Security.Cryptography;
namespace istracker_asp.net
{
public partial class upload : System.Web.UI.Page
{
static string DatabaseConnectionString = ConfigurationManager.ConnectionStrings["dbConStr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (Session["login"] == null || (Boolean)Session["login"] == false)
{
Response.Redirect("login.aspx");
}
}
private bool is_valid_torrent(ref BENObject obj)
{
if (obj.getType() == BENObjectType.Dictionary && obj.getDictonary().ContainsKey(new BENObject("info")))
{
BENObject info = obj.getDictonary()[new BENObject("info")];
if (info.getType() == BENObjectType.Dictionary &&
info.getDictonary().ContainsKey(new BENObject("piece length")) &&
info.getDictonary().ContainsKey(new BENObject("pieces")) &&
info.getDictonary().ContainsKey(new BENObject("name")) &&
(info.getDictonary().ContainsKey(new BENObject("length")) ||
(info.getDictonary().ContainsKey(new BENObject("files")) &&
info.getDictonary()[new BENObject("files")].getType() == BENObjectType.List)))
return true;
}
return false;
}
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";
}
}
}
}
}