-
Notifications
You must be signed in to change notification settings - Fork 6
/
Torrent.cs
336 lines (306 loc) · 10.2 KB
/
Torrent.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace TorrentParser
{
public class Torrent
{
private readonly Dictionary<string, TValue> _root;
private readonly BinaryReader _torrent;
private long _infoStart;
private long _infoEnd;
public long TotalValues { get; private set; }
public string MagnetURI
{
get
{
var sb = new StringBuilder($"magnet:?xt=urn:btih:{SHAHash}");
if (!string.IsNullOrWhiteSpace(Name)) sb.Append($"&dn={System.Net.WebUtility.UrlEncode(Name)}");
if(AnnounceList.Length>0) sb.Append($"&tr={string.Join("&tr=", AnnounceList.Select(System.Net.WebUtility.UrlEncode))}");
return sb.ToString();
}
}
public string SHAHash { get; }
public byte[] ByteHash { get; private set; }
public TFile[] Files { get; private set; }
public long Size { get; private set; }
public long PieceLength => Info.FindNumber("piece length");
public Dictionary<string, TValue> Info => _root["info"].Value as Dictionary<string, TValue>;
public bool IsSingle => !Info.ContainsKey("files");
public byte[] Pieces => Info["pieces"].Value as byte[];
public string[] AnnounceList
{
get
{
if (!_root.ContainsKey("announce-list"))
{
return new string[0];
}
var strs = new List<string>();
foreach (var item in (List<TValue>)_root["announce-list"].Value)
{
foreach (var tVal in (List<TValue>)item.Value)
{
var str = (string)tVal.Value;
if (strs.Contains(str))
{
continue;
}
strs.Add(str);
}
}
return strs.ToArray();
}
}
public string AnnounceURL => _root.FindText("announce");
public string Comment => _root.FindText("comment");
public string CreatedBy => _root.FindText("created by");
public string Name => Info.FindText("name");
public bool Private => Info.FindText("private") == "1";
public DateTime CreationDate
{
get
{
DateTime dateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
dateTime = dateTime.AddSeconds(_root.FindNumber("creation date"));
return dateTime;
}
}
public string FileEncoding => Info.FindText("encoding");
public Torrent(byte[] data)
{
_torrent = new BinaryReader(new MemoryStream(data), Encoding.UTF8);
if (_torrent.ReadChar() != 'd')
{
throw new Exception("Torrent File Error");
}
_root = ProcessDict();
SHAHash = GetSHAHash();
Files = GetFiles();
_torrent.Close();
}
public static bool TryParse(string path, out Torrent torrent)
{
try
{
torrent = new Torrent(File.ReadAllBytes(path));
return true;
}
catch (Exception)
{
torrent = null;
return false;
}
}
public static bool TryParse(byte[] bytes, out Torrent torrent)
{
try
{
torrent = new Torrent(bytes);
return true;
}
catch (Exception)
{
torrent = null;
return false;
}
}
public static Torrent Parse(string path)
{
return new Torrent(File.ReadAllBytes(path));
}
#region PRC
private TFile[] GetFiles()
{
var tFiles = new List<TFile>();
if (!Info.ContainsKey("files"))
{
tFiles.Add(new TFile((long)Info["length"].Value, 1, Pieces.Length / 20, (string)Info["name"].Value));
Size = (long) Info["length"].Value;
}
else
{
long citem = 0;
var v = (List<TValue>)Info["files"].Value;
foreach (var tVal in v)
{
var strs = (Dictionary<string, TValue>)tVal.Value;
var pieceLength = (int)(citem / PieceLength) + 1;
citem = citem + (long)strs["length"].Value;
var num = (int)(citem / PieceLength) + 2 - pieceLength;
tFiles.Add(new TFile((long)strs["length"].Value, pieceLength, num, ((List<TValue>)strs["path"].Value).Select(c=>c.Value as string).ToArray()));
}
Size = citem;
}
return tFiles.Where(c=>!c.Name.StartsWith("_____padding_file")).ToArray();
}
private string GetSHAHash()
{
var sHA1Managed = new SHA1Managed();
_torrent.BaseStream.Position = _infoStart;
var numArray = _torrent.ReadBytes(Convert.ToInt32(_infoEnd));
ByteHash = sHA1Managed.ComputeHash(numArray);
return BitConverter.ToString(ByteHash).Replace("-", "");
}
private TValue ProcessVal()
{
TotalValues++;
var str = char.ConvertFromUtf32(_torrent.PeekChar());
if (str == "d")
{
_torrent.Read();
return new TValue(DataType.Dictionary, ProcessDict());
}
if (str == "l")
{
_torrent.Read();
return new TValue(DataType.List, ProcessList());
}
if (str == "i")
{
_torrent.Read();
return new TValue(DataType.Int, ProcessInt());
}
return new TValue(DataType.String, ProcessString());
}
private string ProcessString()
{
var str = new StringBuilder();
do
{
str.Append(char.ConvertFromUtf32(_torrent.Read()));
}
while (char.ConvertFromUtf32(_torrent.PeekChar()) != ":");
_torrent.Read();
var numArray = _torrent.ReadBytes(int.Parse(str.ToString()));
return Encoding.UTF8.GetString(numArray);
}
private List<TValue> ProcessList()
{
var tVals = new List<TValue>();
while (char.ConvertFromUtf32(_torrent.PeekChar()) != "e")
{
tVals.Add(ProcessVal());
}
_torrent.Read();
return tVals;
}
private long ProcessInt()
{
var str = new StringBuilder();
do
{
str = str.Append(char.ConvertFromUtf32(_torrent.Read()));
}
while (char.ConvertFromUtf32(_torrent.PeekChar()) != "e");
_torrent.Read();
return long.Parse(str.ToString());
}
private Dictionary<string, TValue> ProcessDict()
{
var strs = new Dictionary<string, TValue>();
while (_torrent.PeekChar() != 101)
{
string str = ProcessString();
if (str == "info")
{
_infoStart = _torrent.BaseStream.Position;
}
if (str != "pieces")
{
TValue tVal = ProcessVal();
strs.Add(str, tVal);
}
else
{
strs.Add(str, ProcessByte());
}
if (str != "info")
{
continue;
}
_infoEnd = _torrent.BaseStream.Position - _infoStart;
}
_torrent.Read();
return strs;
}
private TValue ProcessByte()
{
var str = new StringBuilder();
do
{
str.Append(char.ConvertFromUtf32(_torrent.Read()));
} while (char.ConvertFromUtf32(_torrent.PeekChar()) != ":");
_torrent.Read();
return new TValue(DataType.Byte, _torrent.ReadBytes(int.Parse(str.ToString())));
}
#endregion
}
internal static class TorrentExt
{
internal static T Find<T>(this Dictionary<string, TValue> dictToSearch, string key)
{
if (!dictToSearch.ContainsKey(key))
{
return default(T);
}
return (T)dictToSearch[key].Value;
}
internal static long FindNumber(this Dictionary<string, TValue> dictToSearch, string key)
{
return dictToSearch.Find<long>(key);
}
internal static string FindText(this Dictionary<string, TValue> dictToSearch, string key)
{
return dictToSearch.Find<string>(key);
}
}
#region Types
public class TFile
{
public int FirstPiece { get; }
public long Length { get; }
public string Name { get; }
public string Path { get; }
public long PieceLength { get; }
public TFile(long size, int pStart, int pLen, params string[] fullPath)
{
Length = size < 0 ? 0 : size;
FirstPiece = pStart;
PieceLength = pLen;
Path = string.Join("\\", fullPath);
var num = Path.LastIndexOf("\\", StringComparison.Ordinal);
if (num <= 0)
{
Name = Path;
Path = "";
return;
}
Name = Path.Substring(num + 1);
Path = Path.Substring(0, num);
}
}
public class TValue
{
public object Value { get; }
public DataType Type { get; }
public TValue(DataType dType, object dValue)
{
Type = dType;
Value = dValue;
}
}
public enum DataType
{
String,
Int,
List,
Dictionary,
Byte
}
#endregion
}