2020-12-17 13:39:29 +00:00
|
|
|
import json
|
2021-04-03 17:36:01 +00:00
|
|
|
import logging
|
2021-08-11 20:29:31 +00:00
|
|
|
import asyncio
|
2021-08-10 20:17:56 +00:00
|
|
|
from urllib.parse import parse_qsl
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
from app.classes.shared.authentication import authentication
|
2021-08-10 20:17:56 +00:00
|
|
|
from app.classes.shared.helpers import helper
|
2022-01-26 01:45:30 +00:00
|
|
|
from app.classes.web.websocket_helper import websocket_helper
|
2020-12-17 13:39:29 +00:00
|
|
|
|
2021-08-11 20:29:31 +00:00
|
|
|
try:
|
|
|
|
import tornado.websocket
|
|
|
|
|
|
|
|
except ModuleNotFoundError as e:
|
2022-03-08 04:40:44 +00:00
|
|
|
helper.auto_installer_fix(e)
|
2020-12-17 13:39:29 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2022-01-15 00:23:50 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2021-03-01 00:54:20 +00:00
|
|
|
class SocketHandler(tornado.websocket.WebSocketHandler):
|
2022-01-15 00:23:50 +00:00
|
|
|
page = None
|
|
|
|
page_query_params = None
|
|
|
|
controller = None
|
|
|
|
tasks_manager = None
|
|
|
|
translator = None
|
|
|
|
io_loop = None
|
2021-03-01 00:54:20 +00:00
|
|
|
|
2021-03-26 13:57:50 +00:00
|
|
|
def initialize(self, controller=None, tasks_manager=None, translator=None):
|
2021-03-22 04:02:18 +00:00
|
|
|
self.controller = controller
|
|
|
|
self.tasks_manager = tasks_manager
|
2021-03-26 13:57:50 +00:00
|
|
|
self.translator = translator
|
2021-08-11 20:29:31 +00:00
|
|
|
self.io_loop = tornado.ioloop.IOLoop.current()
|
2021-03-22 04:02:18 +00:00
|
|
|
|
2021-03-01 00:54:20 +00:00
|
|
|
def get_remote_ip(self):
|
2022-03-23 02:50:12 +00:00
|
|
|
remote_ip = (
|
|
|
|
self.request.headers.get("X-Real-IP")
|
|
|
|
or self.request.headers.get("X-Forwarded-For")
|
|
|
|
or self.request.remote_ip
|
|
|
|
)
|
2021-03-01 00:54:20 +00:00
|
|
|
return remote_ip
|
|
|
|
|
2021-11-27 22:10:43 +00:00
|
|
|
def get_user_id(self):
|
2022-03-23 02:50:12 +00:00
|
|
|
_, _, user = authentication.check(self.get_cookie("token"))
|
|
|
|
return user["user_id"]
|
2021-11-27 22:10:43 +00:00
|
|
|
|
2021-03-01 00:54:20 +00:00
|
|
|
def check_auth(self):
|
2022-03-23 02:50:12 +00:00
|
|
|
return authentication.check_bool(self.get_cookie("token"))
|
2020-12-17 13:39:29 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
# pylint: disable=arguments-differ
|
2020-12-17 13:39:29 +00:00
|
|
|
def open(self):
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.debug("Checking WebSocket authentication")
|
2021-03-01 00:54:20 +00:00
|
|
|
if self.check_auth():
|
|
|
|
self.handle()
|
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
websocket_helper.send_message(
|
|
|
|
self, "notification", "Not authenticated for WebSocket connection"
|
|
|
|
)
|
2021-03-01 00:54:20 +00:00
|
|
|
self.close()
|
2022-03-23 02:50:12 +00:00
|
|
|
self.controller.management.add_to_audit_log_raw(
|
|
|
|
"unknown",
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
"Someone tried to connect via WebSocket without proper authentication",
|
|
|
|
self.get_remote_ip(),
|
|
|
|
)
|
|
|
|
websocket_helper.broadcast(
|
|
|
|
"notification",
|
|
|
|
"Someone tried to connect via WebSocket without proper authentication",
|
|
|
|
)
|
|
|
|
logger.warning(
|
|
|
|
"Someone tried to connect via WebSocket without proper authentication"
|
|
|
|
)
|
2021-03-01 00:54:20 +00:00
|
|
|
|
|
|
|
def handle(self):
|
2022-03-23 02:50:12 +00:00
|
|
|
self.page = self.get_query_argument("page")
|
|
|
|
self.page_query_params = dict(
|
|
|
|
parse_qsl(
|
|
|
|
helper.remove_prefix(self.get_query_argument("page_query_params"), "?")
|
|
|
|
)
|
|
|
|
)
|
2021-08-10 20:17:56 +00:00
|
|
|
websocket_helper.add_client(self)
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.debug("Opened WebSocket connection")
|
2020-12-17 13:39:29 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
# pylint: disable=arguments-renamed
|
2022-01-15 00:23:50 +00:00
|
|
|
@staticmethod
|
|
|
|
def on_message(raw_message):
|
2020-12-17 13:39:29 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.debug(f"Got message from WebSocket connection {raw_message}")
|
2022-01-15 00:23:50 +00:00
|
|
|
message = json.loads(raw_message)
|
2022-01-26 01:45:30 +00:00
|
|
|
logger.debug(f"Event Type: {message['event']}, Data: {message['data']}")
|
2020-12-17 13:39:29 +00:00
|
|
|
|
|
|
|
def on_close(self):
|
2021-08-10 20:17:56 +00:00
|
|
|
websocket_helper.remove_client(self)
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.debug("Closed WebSocket connection")
|
2020-12-17 13:39:29 +00:00
|
|
|
|
2021-08-11 20:29:31 +00:00
|
|
|
async def write_message_int(self, message):
|
|
|
|
self.write_message(message)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2021-08-11 20:29:31 +00:00
|
|
|
def write_message_helper(self, message):
|
2022-03-23 02:50:12 +00:00
|
|
|
asyncio.run_coroutine_threadsafe(
|
|
|
|
self.write_message_int(message), self.io_loop.asyncio_loop
|
|
|
|
)
|