forked from breakdowns/slam-mirrorbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wserver.py
309 lines (244 loc) · 8.24 KB
/
wserver.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
# -*- coding: utf-8 -*-
# (c) YashDK [yash-dk@github]
import os
import time
import logging
import qbittorrentapi as qba
import asyncio
from aiohttp import web
import nodes
LOGGER = logging.getLogger(__name__)
routes = web.RouteTableDef()
page = """
<html>
<head>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha256-4+XzXVhsDmqanXGHaHvgh1gMQKX40OUvDEBTu8JcmNs=" crossorigin="anonymous"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
body {
padding: 20px;
}
ul {
list-style: none;
margin: 5px 20px;
}
li {
margin: 10px 0;
}
p { font-size: 12px; margin: 24px;}
</style>
</head>
<body>
<h1>slam-mirrorbot: <a href="https://github.com/breakdowns/slam-mirrorbot">@Github</a></h1>
<form action="{form_url}" method="POST">
{My_content}
<input type="submit" name="Select these files ;)">
</form>
<script>
$('input[type="checkbox"]').change(function(e) {
var checked = $(this).prop("checked"),
container = $(this).parent(),
siblings = container.siblings();
/*
$(this).attr('value', function(index, attr){
return attr == 'yes' ? 'noo' : 'yes';
});
*/
container.find('input[type="checkbox"]').prop({
indeterminate: false,
checked: checked
});
function checkSiblings(el) {
var parent = el.parent().parent(),
all = true;
el.siblings().each(function() {
let returnValue = all = ($(this).children('input[type="checkbox"]').prop("checked") === checked);
return returnValue;
});
if (all && checked) {
parent.children('input[type="checkbox"]').prop({
indeterminate: false,
checked: checked
});
checkSiblings(parent);
} else if (all && !checked) {
parent.children('input[type="checkbox"]').prop("checked", checked);
parent.children('input[type="checkbox"]').prop("indeterminate", (parent.find('input[type="checkbox"]:checked').length > 0));
checkSiblings(parent);
} else {
el.parents("li").children('input[type="checkbox"]').prop({
indeterminate: true,
checked: false
});
}
}
checkSiblings(container);
});
</script>
</body>
</html>
"""
code_page = """
<html>
<head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous">
<title>
Slam Torrent Files
</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<div class="container">
<form action="{form_url}">
<div class="form-group">
<label for="pin_code">Pin Code</label>
<input type="text" class="form-control" name="pin_code" placeholder="Enter code to access the torrent">
<small class="form-text text-muted">Dont mess around. You download will get messed up.</small>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</body>
</html>
"""
@routes.get('/slam/files/{hash_id}')
async def list_torrent_contents(request):
torr = request.match_info["hash_id"]
gets = request.query
if not "pin_code" in gets.keys():
rend_page = code_page.replace("{form_url}",f"/slam/files/{torr}")
return web.Response(text=rend_page,content_type='text/html')
client = qba.Client(host="localhost",port="8090",username="admin",password="adminadmin")
client.auth_log_in()
try:
res = client.torrents_files(torrent_hash=torr)
except qba.NotFound404Error:
raise web.HTTPNotFound()
count = 0
passw = ""
for n in str(torr):
if n.isdigit():
passw += str(n)
count += 1
if count == 4:
break
if isinstance(passw, bool):
raise web.HTTPNotFound()
pincode = passw
if gets["pin_code"] != pincode:
return web.Response(text="Incorrect pin code")
par = nodes.make_tree(res)
cont = ["",0]
nodes.create_list(par,cont)
rend_page = page.replace("{My_content}",cont[0])
rend_page = rend_page.replace("{form_url}",f"/slam/files/{torr}?pin_code={pincode}")
client.auth_log_out()
return web.Response(text=rend_page,content_type='text/html')
async def re_verfiy(paused, resumed, client, torr):
paused = paused.strip()
resumed = resumed.strip()
if paused:
paused = paused.split("|")
if resumed:
resumed = resumed.split("|")
k = 0
while True:
res = client.torrents_files(torrent_hash=torr)
verify = True
for i in res:
if str(i.id) in paused:
if i.priority == 0:
continue
else:
verify = False
break
if str(i.id) in resumed:
if i.priority != 0:
continue
else:
verify = False
break
if not verify:
LOGGER.info("Reverification Failed :- correcting stuff")
# reconnect and issue the request again
client.auth_log_out()
client = qba.Client(host="localhost",port="8090",username="admin",password="adminadmin")
client.auth_log_in()
try:
client.torrents_file_priority(torrent_hash=torr,file_ids=paused,priority=0)
except:
LOGGER.error("Errored in reverification paused")
try:
client.torrents_file_priority(torrent_hash=torr,file_ids=resumed,priority=1)
except:
LOGGER.error("Errored in reverification resumed")
client.auth_log_out()
else:
break
k += 1
if k >= 2:
# avoid an infite loop here
return False
return True
@routes.post('/slam/files/{hash_id}')
async def set_priority(request):
torr = request.match_info["hash_id"]
client = qba.Client(host="localhost",port="8090",username="admin",password="adminadmin")
client.auth_log_in()
data = await request.post()
resume = ""
pause = ""
data = dict(data)
for i in data.keys():
if i.find("filenode") != -1:
node_no = i.split("_")[-1]
if data[i] == "on":
resume += f"{node_no}|"
else:
pause += f"{node_no}|"
pause = pause.strip("|")
resume = resume.strip("|")
LOGGER.info(f"Paused {pause} of {torr}")
LOGGER.info(f"Resumed {resume} of {torr}")
try:
client.torrents_file_priority(torrent_hash=torr,file_ids=pause,priority=0)
except qba.NotFound404Error:
raise web.HTTPNotFound()
except:
LOGGER.info("Errored in paused")
try:
client.torrents_file_priority(torrent_hash=torr,file_ids=resume,priority=1)
except qba.NotFound404Error:
raise web.HTTPNotFound()
except:
LOGGER.info("Errored in resumed")
await asyncio.sleep(2)
if not await re_verfiy(pause,resume,client,torr):
LOGGER.error("The torrent choose errored reverification failed")
client.auth_log_out()
return await list_torrent_contents(request)
@routes.get('/')
async def homepage(request):
return web.Response(text="<h1>See slam-mirrorbot <a href='https://github.com/breakdowns/slam-mirrorbot'>@GitHub</a> By <a href='https://github.com/breakdowns'>Breakdowns</a></h1>",content_type="text/html")
async def e404_middleware(app, handler):
async def middleware_handler(request):
try:
response = await handler(request)
if response.status == 404:
return web.Response(text="<h1>404: Page not found</h2><br><h3>slam-mirrorbot</h3>",content_type="text/html")
return response
except web.HTTPException as ex:
if ex.status == 404:
return web.Response(text="<h1>404: Page not found</h2><br><h3>slam-mirrorbot</h3>",content_type="text/html")
raise
return middleware_handler
async def start_server():
app = web.Application(middlewares=[e404_middleware])
app.add_routes(routes)
return app
async def start_server_async(port = 8080):
app = web.Application(middlewares=[e404_middleware])
app.add_routes(routes)
runner = web.AppRunner(app)
await runner.setup()
await web.TCPSite(runner,"0.0.0.0", port).start()