-
Notifications
You must be signed in to change notification settings - Fork 5
/
TorrentPlugin.py
273 lines (226 loc) · 10.6 KB
/
TorrentPlugin.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
import logging
import time
import base64
import gevent
import sys
import libtorrent
from util.Flag import flag
from .AlertEncoder import AlertEncoder
from Plugin import PluginManager
libtorrent = libtorrent.libtorrent
VERSION = '0.4.8'
def popAlerts(session):
while 1:
alertsList = session.pop_alerts()
# Need encoding
for alert in alertsList:
result = AlertEncoder(alert)
for instanceOfUiWebsocketPlugin in UiWebsocketPlugin._instances:
if alert.what() == 'read_piece' and not 'error' in result.get():
for instanceOfUiRequestPlugin in UiRequestPlugin._instances:
if result.get()["pieceIndex"] in instanceOfUiRequestPlugin.piece_index_requested:
instanceOfUiRequestPlugin.requested_pieces.append(result.get())
instanceOfUiWebsocketPlugin.cmd(alert.what(), result.get())
gevent.sleep(0.250)
# Initiate libtorrent session
session = libtorrent.session({'listen_interfaces':'0.0.0.0:6881', 'alert_mask': libtorrent.alert.category_t.progress_notification+libtorrent.alert.category_t.status_notification})
gevent.spawn(popAlerts, session)
@PluginManager.registerTo("UiRequest")
class UiRequestPlugin(object):
_instances = []
def __init__(self, *args, **kwargs):
super(UiRequestPlugin, self).__init__(*args, **kwargs)
UiRequestPlugin._instances.append(self)
self.requested_pieces = []
self.file = None
self.piece_index_requested = []
def getTorrentFile(self, file_path):
torrent_handles = session.get_torrents()
for h in torrent_handles:
for file_index in range(0,files.num_files()):
file = ti.file_at(file_index)
if file.path in file_path:
print(file.path)
return ti.info_hash()
return False
def actionFile(self, file_path, *args, **kwargs):
if "info_hash" in self.get:
print("info hash :", self.get["info_hash"])
print("File index :", self.get["file_index"])
info_hash = libtorrent.sha1_hash(bytes.fromhex(self.get["info_hash"]))
h = session.find_torrent(info_hash)
if h.is_valid():
ti = h.torrent_file()
files = ti.files()
for file_index in range(0,files.num_files()):
file = ti.file_at(file_index)
if file.path in file_path:
# priotarize file (7 max priority)
h.file_priority(file_index, 7)
kwargs["block_size"] = ti.piece_length()
print("piece_size : {}".format(kwargs["block_size"]))
self.file = file
kwargs["file_size"] = file.size
kwargs["file_obj"] = TorrentFile(h, file, self)
return super(UiRequestPlugin, self).actionFile(file_path, *args, **kwargs)
@PluginManager.registerTo("UiWebsocket")
class UiWebsocketPlugin(object):
_instances = []
def __init__(self, *args, **kwargs):
super(UiWebsocketPlugin, self).__init__(*args, **kwargs)
UiWebsocketPlugin._instances.append(self)
def hasSitePermission(self, address, cmd=None):
if "Torrent" in self.site.settings["permissions"]:
return True
else:
return False
def actionGetVersion(self, to):
self.response(to, {'version': VERSION})
@flag.no_multiuser
def actionAddTorrent(self, to, torrentIdentifier):
if not self.hasSitePermission(self.site.address):
return self.response(to, {"error": "Forbidden"})
save_path = './data/' + self.site.address + '/downloads/'
try:
e = libtorrent.bdecode(base64.b64decode(torrentIdentifier))
info = libtorrent.torrent_info(e)
params = { 'save_path': save_path, \
'storage_mode': libtorrent.storage_mode_t.storage_mode_sparse, \
'ti': info }
except Exception as exception:
try:
# Test if a magnet
params = libtorrent.parse_magnet_uri(torrentIdentifier)
params.save_path = save_path
# HACK:
# Doesn't recognise sha1_hash python object when added to session if not converted to string
# 'No registered converter was able to produce a C++ rvalue of type bytes from this Python object of type sha1_hash'
#params['info_hash'] = params['info_hash'].to_string()
except Exception as exception:
params = { 'save_path': save_path, \
'storage_mode': libtorrent.storage_mode_t.storage_mode_sparse, \
'info_hash': torrentIdentifier.decode('hex') }
try:
session.async_add_torrent(params)
except Exception as exception:
self.response(to, {'error': str(exception)})
else:
if type(params) is libtorrent.add_torrent_params:
info_hash = params.info_hashes.get_best()
else:
if not info is None:
info_hash = info.info_hashes().get_best()
else:
info_hash = params['info_hash']
self.response(to, {'info_hash': str(info_hash)})
def actionTorrentStatus(self, to, info_hash):
if not self.hasSitePermission(self.site.address):
return self.response(to, {"error": "Forbidden"})
info_hash = libtorrent.sha1_hash(bytes.fromhex(info_hash))
h = session.find_torrent(info_hash)
if h.is_valid():
s = h.status()
self.response(to, {'progress': s.progress, \
'download_rate': s.download_rate, \
'upload_rate': s.upload_rate, \
'num_peers': s.num_peers, \
'state': str(s.state) })
else:
self.response(to, {'error': 'Torrent not found'})
def actionGetTorrentInfo(self, to, info_hash):
if not self.hasSitePermission(self.site.address):
return self.response(to, {"error": "Forbidden"})
info_hash = libtorrent.sha1_hash(bytes.fromhex(info_hash))
h = session.find_torrent(info_hash)
if h.is_valid():
ti = h.get_torrent_info()
files = ti.files()
arrayFiles = []
for file in files:
arrayFiles.append({'path': file.path, \
'offset': file.offset, \
'size': file.size \
})
self.response(to, {'name': ti.name(), \
'num_files' : ti.num_files(), \
'files': arrayFiles, \
'piece_length': ti.piece_length() \
})
else:
self.response(to, {'error': 'Torrent not found'})
def actionReadPiece(self, to, info_hash, piece_index):
if not self.hasSitePermission(self.site.address):
return self.response(to, {"error": "Forbidden"})
info_hash = libtorrent.sha1_hash(bytes.fromhex(info_hash))
h = session.find_torrent(info_hash)
if h.is_valid():
h.read_piece(piece_index)
self.response(to, 'ok')
else:
self.response(to, {'error': 'Torrent not found'})
def actionHavePiece(self, to, info_hash, piece_index):
if not self.hasSitePermission(self.site.address):
return self.response(to, {"error": "Forbidden"})
info_hash = libtorrent.sha1_hash(bytes.fromhex(info_hash))
h = session.find_torrent(info_hash)
if h.is_valid():
response = h.have_piece(piece_index)
self.response(to, response)
else:
self.response(to, {'error': 'Torrent not found'})
def actionPrioritizePiece(self, to, info_hash, piece_index, new_priority):
if not self.hasSitePermission(self.site.address):
return self.response(to, {"error": "Forbidden"})
info_hash = libtorrent.sha1_hash(bytes.fromhex(info_hash))
h = session.find_torrent(info_hash)
if h.is_valid():
if 0 <= new_priority <= 7 :
h.piece_priority(piece_index, new_priority)
self.response(to, 'ok')
else :
self.response(to, {'error': 'new_priority should be an integer bewteen 0 and 7'})
else:
self.response(to, {'error': 'Torrent not found'})
class TorrentFile(object):
def __init__(self, torrent_handle, file, uirequest):
self.torrent_handle = torrent_handle
self.read_bytes = 0
self.file = file
self._offset = file.offset
self.uirequest = uirequest
self.cache = []
def read(self, buff=64 * 1024):
chunk_file = 0x00
ti = self.torrent_handle.torrent_file()
piece_index = (self.file.offset + self.read_bytes) // ti.piece_length()
#print("Piece Index requested : {} ( ({} + {}) // {})".format(piece_index, self.file.offset, self.read_bytes, ti.piece_length()))
self.uirequest.piece_index_requested.append(piece_index)
# TODO: `set_piece_deadline` has the same behavior as read when piece already available
if self.torrent_handle.have_piece(piece_index):
self.torrent_handle.read_piece(piece_index)
else:
print("Piece not available!")
# deadline in milliseconds so we have a 900 seconds deadline after what maybe we can have a timeout ?
self.torrent_handle.set_piece_deadline(piece_index, 900 * 1000, libtorrent.deadline_flags_t.alert_when_available)
while chunk_file == 0x00:
for piece in self.uirequest.requested_pieces:
if piece["pieceIndex"] == piece_index:
if self._offset:
chunk_file = piece["buffer"][self._offset:]
self._offset = 0
else:
chunk_file = piece["buffer"]
self.read_bytes += len(chunk_file)
self.uirequest.requested_pieces.remove(piece)
break
gevent.sleep(0.1)
pass
return chunk_file
def seek(self, pos, whence=0):
#print("SEEKING {}".format(pos))
self.read_bytes = pos
ti = self.torrent_handle.torrent_file()
self._offset = (self.file.offset + self.read_bytes) % ti.piece_length()
return
def close(self):
pass