This repository has been archived by the owner on Jan 11, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
youcube.py
423 lines (338 loc) · 10.6 KB
/
youcube.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#!/usr/bin/python3
# -*- coding: utf-8 -*-
"""
YouCube Server
"""
# built-in modules
from os.path import join
from os import getenv
from json import loads as load_json
from json.decoder import JSONDecodeError
from logging import Logger
from asyncio import get_event_loop
from typing import (
Callable,
Union,
Tuple,
Type,
List,
Any
)
from base64 import b64encode
from shutil import which
try:
from types import UnionType
except ImportError:
UnionType = Union[int, str]
# pip modules
from aiohttp.web import (
Request,
WebSocketResponse,
Response,
WSMsgType,
Application,
run_app
)
# local modules
from yc_logging import setup_logging, NO_COLOR
from yc_magic import run_function_in_thread_from_async_function
from yc_download import download, DATA_FOLDER, FFMPEG_PATH, SANJUUNI_PATH
from yc_colours import Foreground, RESET
from yc_utils import (
is_save,
cap_width_and_height,
get_video_name,
get_audio_name
)
VERSION = "0.0.0-poc.1.0.2"
API_VERSION = "0.0.0-poc.1.0.0" # https://commandcracker.github.io/YouCube/
# one dfpwm chunk is 16 bits
CHUNK_SIZE = 16
"""
CHUNKS_AT_ONCE should not be too big, [CHUNK_SIZE * 1024]
because then the CC Computer cant decode the string fast enough!
Also, it should not be too small because then the client would need to send thousands of WS messages
and that would also slow everything down! [CHUNK_SIZE * 1]
"""
CHUNKS_AT_ONCE = CHUNK_SIZE * 256
FRAMES_AT_ONCE = 10
# pylint settings
# pylint: disable=pointless-string-statement
# pylint: disable=fixme
# pylint: disable=multiple-statements
def get_vid(vid_file: str, tracker: int) -> List[str]:
"""
Returns given line of 32vid file
"""
with open(vid_file, "r", encoding="utf-8") as file:
file.seek(tracker)
lines = []
for _unused in range(FRAMES_AT_ONCE):
lines.append(file.readline()[:-1]) # remove \n
file.close()
return lines
def get_chunk(media_file: str, chunkindex: int) -> bytes:
"""
Returns a chunk of the given media file
"""
with open(media_file, "rb") as file:
file.seek(chunkindex * CHUNKS_AT_ONCE)
chunk = file.read(CHUNKS_AT_ONCE)
file.close()
return chunk
def get_peername_host(request: Request) -> str:
"""
Returns the Host of the web-request
"""
peername = request.transport.get_extra_info('peername')
if peername is not None:
host, *_ = peername
return host
return None
class UntrustedProxy(Exception):
"""
Occurs when someone connects through an untrusted proxy
"""
def __str__(self) -> str:
return "A client is not using a trusted proxy!"
def get_client_ip(request: Request, trusted_proxies: list) -> str:
"""
Returns the real client IP
"""
peername_host = get_peername_host(request)
if trusted_proxies is None:
return peername_host
if peername_host in trusted_proxies:
x_forwarded_for = request.headers.get('X-Forwarded-For')
if x_forwarded_for is not None:
x_forwarded_for = x_forwarded_for.split(",")[0]
return x_forwarded_for or request.headers.get('True-Client-Ip')
raise UntrustedProxy
def assert_resp(
__obj_name: str,
__obj: Any,
__class_or_tuple: Union[
Type, UnionType,
Tuple[
Union[
Type,
UnionType,
Tuple[Any, ...]
],
...
]
]
) -> Union[dict, None]:
"""
"assert" / isinstance that returns a dict that can be send as a ws response
"""
if not isinstance(__obj, __class_or_tuple):
return {
"action": "error",
"message": f"{__obj_name} must be a {__class_or_tuple.__name__}"
}
return None
class Actions:
"""
Default set of actions
Every action needs to be called with a message and needs to return a dict response
"""
# pylint: disable=missing-function-docstring
@staticmethod
async def request_media(message: dict, resp: WebSocketResponse):
loop = get_event_loop()
# get "url"
url = message.get("url")
if error := assert_resp("url", url, str): return error
# TODO: assert_resp width and height
return await run_function_in_thread_from_async_function(
download,
url,
resp,
loop,
message.get("width"),
message.get("height")
)
@staticmethod
async def get_chunk(message: dict, _unused):
# get "chunkindex"
chunkindex = message.get("chunkindex")
if error := assert_resp("chunkindex", chunkindex, int): return error
# get "id"
media_id = message.get("id")
if error := assert_resp("media_id", media_id, str): return error
if is_save(media_id):
file = join(
DATA_FOLDER,
get_audio_name(message.get("id"))
)
chunk = get_chunk(file, chunkindex)
return {
"action": "chunk",
"chunk": b64encode(chunk).decode("ascii")
}
return {
"action": "error",
"message": "You dare not use special Characters"
}
@staticmethod
async def get_vid(message: dict, _unused):
# get "line"
tracker = message.get("tracker")
if error := assert_resp("tracker", tracker, int): return error
# get "id"
media_id = message.get("id")
if error := assert_resp("id", media_id, str): return error
# get "width"
width = message.get('width')
if error := assert_resp("width", width, int): return error
# get "height"
height = message.get('height')
if error := assert_resp("height", height, int): return error
# cap height and width
width, height = cap_width_and_height(width, height)
if is_save(media_id):
file = join(
DATA_FOLDER,
get_video_name(message.get('id'), width, height)
)
return {
"action": "vid",
"lines": get_vid(file, tracker)
}
return {
"action": "error",
"message": "You dare not use special Characters"
}
@staticmethod
async def handshake(*_unused):
return {
"action": "handshake",
"server": {
"version": VERSION
},
"api": {
"version": API_VERSION
},
"capabilities": {
"video": [
"32vid"
],
"audio": [
"dfpwm"
]
}
}
# pylint: enable=missing-function-docstring
class Server:
"""
The Web socket server Object
"""
def __init__(self, logger: Logger, trusted_proxies: list) -> None:
self.logger = logger
self.trusted_proxies = trusted_proxies
self.actions = {}
# add all actions from default action set
for method in dir(Actions):
if not method.startswith('__'):
self.actions[method] = getattr(Actions, method)
@staticmethod
async def on_shutdown(app: Application):
"""
Clears all web-sockets from the list
"""
for websocket in app["sockets"]:
await websocket.close()
def init(self):
"""
Initialize the web-socket server
"""
app = Application()
app["sockets"] = []
app.router.add_get("/", self.wshandler)
app.on_shutdown.append(self.on_shutdown)
return app
def register_action(
self,
name: str,
func: Callable[
[dict, WebSocketResponse],
Union[dict, None]
]
):
"""
Add and action / "endpoint" to the ws server
"""
if name in self.actions:
return False, f"action \"{name}\" is already registerd!"
self.actions[name] = func
return True
async def wshandler(self, request: Request):
"""
Handels web-socket requests
"""
resp = WebSocketResponse()
available = resp.can_prepare(request)
if not available:
return Response(
body="You cannot access a WebSocket server directly. You need a WebSocket client.",
content_type="text"
)
await resp.prepare(request)
try:
request.app["sockets"].append(resp)
client_ip = get_client_ip(request, self.trusted_proxies)
if NO_COLOR:
prefix = f"[{client_ip}] "
else:
prefix = f"{Foreground.BLUE}[{client_ip}]{RESET} "
self.logger.info(prefix + "Connected!")
self.logger.debug(
prefix +
"My headers are: " +
str(request.headers)
)
async for msg in resp:
resp: WebSocketResponse
if msg.type == WSMsgType.TEXT:
self.logger.debug(prefix + "Message: " + msg.data)
try:
message: dict = load_json(msg.data)
if message.get("action") in self.actions:
response = await self.actions[message.get("action")](message, resp)
await resp.send_json(response)
except JSONDecodeError:
self.logger.debug(prefix + "Faild to parse Json")
await resp.send_json({
"action": "error",
"message": "Faild to parse Json"
})
else:
return resp
return resp
finally:
request.app["sockets"].remove(resp)
self.logger.info(prefix + "Disconnected!")
def main() -> None:
"""
Run all needed services
"""
logger = setup_logging()
if which(FFMPEG_PATH) is None:
logger.warning("FFmpeg not found.")
if which(SANJUUNI_PATH) is None:
logger.warning("Sanjuuni not found.")
port = int(getenv("PORT", "5000"))
host = getenv("HOST","0.0.0.0")
trusted_proxies = getenv("TRUSTED_PROXIES")
proxies = None
if trusted_proxies is not None:
proxies = []
for proxy in trusted_proxies.split(","):
proxies.append(proxy)
server = Server(logger, proxies)
if not NO_COLOR:
print(Foreground.BRIGHT_GREEN, end="")
run_app(server.init(), host=host, port=port)
if __name__ == "__main__":
main()