-
Notifications
You must be signed in to change notification settings - Fork 1
/
torrentcontroller.py
348 lines (262 loc) · 8.45 KB
/
torrentcontroller.py
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
337
338
339
340
341
342
343
344
345
346
347
348
import os
import sys
import math
import json
import libtorrent
from PyQt4.QtCore import *
from torrentdownload import TorrentDownload
def formatSize(val):
prefix = ['B', 'kB', 'MB', 'GB', 'TB']
for i in range(len(prefix)):
if math.fabs(val) < 1000:
return '{}{}'.format(val, prefix[i])
val /= 1000
return '{}{}'.format(val, "PB")
def formatSpeed(val):
return formatSize(val) +"/s"
#prevent cross side attacks
#convert to html unicode
def formatText(text):
s = []
for c in text:
n = ord(c)
if n < 32 or n > 126 or "<>[](){}".find(c) > -1:
s.append( "&#"+str(n))
else:
s.append(c)
return "".join(s)
#find the first single '/'
def findSingleSlash(href):
offset = 0
pos = 0
while True:
pos = href.find('/', offset)
if pos < 0:
return len(href)
if (pos+1) < len(href) and href[pos+1] == '/':
pos = pos + 2
while (pos+1) < len(href) and href[pos+1] == '/':
pos = pos + 2
offset = pos
else:
return pos
class TorrentController(QObject):
'''send console input and output'''
download_added = pyqtSignal(TorrentDownload)
download_removed = pyqtSignal(TorrentDownload)
def __init__(self, cache_path):
super(TorrentController, self).__init__()
settings = libtorrent.session_settings()
settings.user_agent = 'torrent_browser/' + libtorrent.version
settings.use_dht_as_fallback = False
settings.volatile_read_cache = False
ses = libtorrent.session()
ses.set_download_rate_limit(0)
ses.set_upload_rate_limit(0)
ses.listen_on(6881, 6891)
ses.set_alert_mask(0xfffffff)
ses.set_settings(settings)
#start local source discovery
ses.start_lsd()
#start distributed hash table
ses.start_dht()
#start router port forwarding
ses.start_upnp()
ses.start_natpmp()
#add bootstrapping nodes
ses.add_dht_router("router.bittorrent.com", 6881)
ses.add_dht_router("router.utorrent.com", 6881)
ses.add_dht_router("router.bitcomet.com", 6881)
self.session = ses
self.downloads = list()
self.alive = True
self.cache_path = cache_path
print("(I) Cache directory: {}".format(cache_path))
if not os.path.exists(self.cache_path):
os.makedirs(self.cache_path)
self.timer = QTimer()
self.timer.timeout.connect(self.sendUpdates)
self.timer.start(500)
def loadSettings(self, path):
if not os.path.exists(path):
return
text = open(path, 'rb').read()
obj = json.loads(text)
self.session.set_download_rate_limit(int(obj.get('download_rate_limit', 0)))
self.session.set_upload_rate_limit(int(obj.get('upload_rate_limit', 0)))
def storeSettings(self, path):
obj = {}
settings = libtorrent.session_settings()
obj['download_rate_limit'] = int(settings.download_rate_limit)
obj['upload_rate_limit'] = int(settings.upload_rate_limit)
data = json.dumps(obj)
file = open(path,'wb')
file.write(data)
file.close()
def sendUpdates(self):
for download in self.downloads:
download.progressed.emit()
def createTorrent(self, torrent_file_path, content_folder_path, progress_cb):
TorrentController.createTorrent(torrent_file_path, content_folder_path, progress_cb)
def addTorrentFile(self, file_path, paused = False):
if not os.path.isfile(file_path):
print("File does not exist: {}".format(file_path))
return None
info = libtorrent.torrent_info(file_path)
info_hash = str(info.info_hash()).lower()
download = self.findTorrentByHash(info_hash)
if download:
return download
else:
save_path = self.cache_path
atp = {}
atp['ti'] = info
atp["save_path"] = save_path
if paused:
atp['flags'] = libtorrent.add_torrent_params_flags_t.flag_paused
else:
atp['flags'] = 0
try:
atp["resume_data"] = open(file_path + '.fastresume', 'rb').read()
except:
pass
handle = self.session.add_torrent(atp)
handle.set_max_connections(60)
handle.set_max_uploads(-1)
download = TorrentDownload(handle, self, save_path)
self.downloads.append(download)
self.download_added.emit(download)
return download
def addMagnetLink(self, url, paused = False):
params = libtorrent.parse_magnet_uri(url)
info_hash = str(params['info_hash']).lower()
download = self.findTorrentByHash(info_hash)
if download:
return download
else:
save_path = self.cache_path
atp = {}
atp["save_path"] = save_path
atp["storage_mode"] = libtorrent.storage_mode_t.storage_mode_sparse
atp["auto_managed"] = True
atp["duplicate_is_error"] = True
#atp["paused"] = paused
atp["url"] = url
if paused:
atp['flags'] = libtorrent.add_torrent_params_flags_t.flag_paused
else:
atp['flags'] = 0
handle = self.session.add_torrent(atp)
handle.set_max_connections(60)
handle.set_max_uploads(-1)
download = TorrentDownload(handle, self, save_path)
self.downloads.append(download)
self.download_added.emit(download)
return download
def settings(self):
return libtorrent.session_settings()
def findTorrentByHash(self, hash):
for download in self.downloads:
if download.info_hash() == hash:
return download
return None
@staticmethod
def createTorrent(torrent_file_path, content_folder_path, progress_cb = None):
print("createTorrent")
fs = libtorrent.file_storage()
if not os.path.isabs(torrent_file_path):
raise "Torrent path not absolute"
if not os.path.isabs(content_folder_path):
raise "Content path not absolute"
libtorrent.add_files(fs, content_folder_path)
if fs.num_files() == 0:
print("No files to add.")
return
t = libtorrent.create_torrent(fs, piece_size=0, pad_file_limit=(4 * 1024 * 1024))
def progress(piece_num):
if progress_cb:
pc = int((100.0 * (1.0+ piece_num)) / fs.num_pieces())
progress_cb(pc)
parent = os.path.dirname(content_folder_path)
libtorrent.set_piece_hashes(t, parent, progress)
data = libtorrent.bencode(t.generate())
file = open(torrent_file_path,'wb')
file.write(data)
file.close()
#TODO: remove and use html interface
def dumpTorrent(self, torrent_file):
size = os.path.getsize(torrent_file)
if size < 0 or size > (40 * 1000000):
return 'error'
t = libtorrent.torrent_info(torrent_file)
site = []
site.append('File: {}'.format(torrent_file))
nodes = t.nodes()
if nodes:
site.append('Nodes:')
for node in nodes:
site.append(" - "+formatText(node))
trackers = t.trackers()
if trackers:
site.append('Trackers:')
for tracker in trackers:
site.append(" - "+formatText(tracker))
magnet_uri = libtorrent.make_magnet_uri(t)
site.append('name: {}'.format(formatText(t.name())))
site.append('num_files: {}'.format(t.num_files()))
site.append('info_hash: {}'.format(t.info_hash()))
site.append('num_pieces: {}'.format(t.num_pieces()))
site.append('piece_length: {}'.format(t.piece_length()))
site.append('magnet_uri: {}'.format(magnet_uri))
comment = t.comment()
if comment:
site.append('comment: {}'.format(formatText(comment)))
creator = t.creator()
if creator:
site.append('creator: {}'.format(formatText(creator)))
fs = t.files()
site.append('Files:')
for f in fs:
site.append(' - {} ({})</li>'.format(path, formatSize(f.size)))
return '\n'.join(site)
def foundAddresses(self, addrs):
print("foundAddresses: {}".format(addrs))
def dht_lookup(self, hash, announce=False):
print("lookupHash {}".format(hash))
self.session.dht_announce("abcdefg", 1234, announce, self.foundAddresses)
def removeDownload(self, download):
print("Controler.removeDownload")
for i, d in enumerate(self.downloads):
if d == download:
print("Found download")
del self.downloads[i]
self.download_removed.emit(download)
self.session.remove_torrent(download.handle, libtorrent.options_t.delete_files)
path = os.path.join(self.cache_path, d.info_hash())
if os.path.exists(path+'.torrent'):
os.remove(path+'.torrent')
if os.path.exists(path+'.torrent.fastresume'):
os.remove(path+'.torrent.fastresume')
break
def setLSD(self, enable):
pass
def getLSD(self):
return True
def setDHT(self, enable):
pass
def getDHT(self):
return True
def shutdown(self):
print("TorrentController.shutdown")
self.session.pause()
for d in self.downloads:
h = d.handle
if h.is_valid() and h.has_metadata():
file = os.path.join(self.cache_path, d.info_hash())
data = libtorrent.bdecode(h.get_torrent_info().metadata())
data = libtorrent.bencode({ "info" : data })
open(file+'.torrent', 'wb').write(data)
data = h.write_resume_data()
data = libtorrent.bencode(data)
open(file + '.torrent.fastresume' , 'wb').write(data)
print("done")