blivechat/api/plugin.py

255 lines
8.6 KiB
Python
Raw Normal View History

2023-11-04 16:52:07 +08:00
# -*- coding: utf-8 -*-
import asyncio
import json
import logging
from typing import *
import tornado.web
import tornado.websocket
import api.base
2023-11-08 00:21:17 +08:00
import api.chat
2023-11-04 16:52:07 +08:00
import blcsdk.models as models
2024-03-03 11:18:14 +08:00
import config
2023-11-08 00:21:17 +08:00
import services.avatar
import services.chat
2023-11-04 16:52:07 +08:00
import services.plugin
logger = logging.getLogger(__name__)
2024-03-03 11:18:14 +08:00
class _AdminHandlerBase(api.base.ApiHandler):
def prepare(self):
cfg = config.get_config()
if not cfg.enable_admin_plugins:
raise tornado.web.HTTPError(403)
2024-03-07 21:25:02 +08:00
logger.info('client=%s requesting admin plugin, cls=%s', self.request.remote_ip, type(self).__name__)
2024-03-03 11:18:14 +08:00
super().prepare()
def _get_plugin(self):
plugin_id = self.json_args.get('pluginId', None)
if not isinstance(plugin_id, str) or plugin_id == '':
raise tornado.web.MissingArgumentError('pluginId')
plugin = services.plugin.get_plugin(plugin_id)
if plugin is None:
raise tornado.web.HTTPError(404, 'no plugin, plugin_id=%s', plugin_id)
return plugin
# 不继承_AdminHandlerBase为了忽略enable_admin_plugins
class PluginsHandler(api.base.ApiHandler):
async def get(self):
plugin_dicts = []
for plugin in services.plugin.iter_plugins():
plugin_cfg = plugin.config
plugin_dicts.append({
'id': plugin.id,
'name': plugin_cfg.name,
'version': plugin_cfg.version,
'author': plugin_cfg.author,
'description': plugin_cfg.description,
'enabled': plugin.enabled,
'isStarted': plugin.is_started,
'isConnected': plugin.is_connected,
})
self.write({'plugins': plugin_dicts})
class EnableHandler(_AdminHandlerBase):
async def post(self):
enabled = bool(self.json_args.get('enabled', False))
plugin = self._get_plugin()
2024-03-07 21:25:02 +08:00
old_enabled = plugin.enabled
is_switch_success = True
2024-03-03 11:18:14 +08:00
msg = ''
try:
plugin.enabled = enabled
2024-03-07 21:25:02 +08:00
except services.plugin.SwitchTooFrequently as e:
is_switch_success = False
2024-03-03 11:18:14 +08:00
msg = str(e)
2024-03-07 21:25:02 +08:00
plugin.enabled = old_enabled
except services.plugin.SwitchPluginError as e:
is_switch_success = False
2024-03-03 11:18:14 +08:00
msg = str(e)
self.write({
'enabled': plugin.enabled,
2024-03-07 21:25:02 +08:00
'isSwitchSuccess': is_switch_success,
2024-03-03 11:18:14 +08:00
'msg': msg
})
class OpenAdminUiHandler(_AdminHandlerBase):
async def post(self):
plugin = self._get_plugin()
plugin.send_cmd_data(models.Command.OPEN_PLUGIN_ADMIN_UI, {})
self.write({})
class _PluginApiHandlerBase(api.base.ApiHandler):
"""给插件用的接口"""
2023-11-04 16:52:07 +08:00
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.plugin: Optional[services.plugin.Plugin] = None
def prepare(self):
try:
auth = self.request.headers['Authorization']
if not auth.startswith('Bearer '):
raise ValueError(f'Bad authorization: {auth}')
token = auth[7:]
self.plugin = services.plugin.get_plugin_by_token(token)
if self.plugin is None:
raise ValueError(f'Token error: {token}')
except (KeyError, ValueError) as e:
logger.warning('client=%s failed to find plugin: %r', self.request.remote_ip, e)
raise tornado.web.HTTPError(403)
super().prepare()
def make_message_body(cmd, data, extra: Optional[dict] = None):
body = {'cmd': cmd, 'data': data}
if extra:
body['extra'] = extra
return json.dumps(body).encode('utf-8')
2024-03-03 11:18:14 +08:00
class PluginWsHandler(_PluginApiHandlerBase, tornado.websocket.WebSocketHandler):
2024-03-12 23:52:17 +08:00
HEARTBEAT_INTERVAL = 30
2023-11-04 16:52:07 +08:00
RECEIVE_TIMEOUT = HEARTBEAT_INTERVAL + 5
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._heartbeat_timer_handle = None
self._receive_timeout_timer_handle = None
def open(self):
logger.info('plugin=%s connected, client=%s', self.plugin.id, self.request.remote_ip)
self._heartbeat_timer_handle = asyncio.get_running_loop().call_later(
self.HEARTBEAT_INTERVAL, self._on_send_heartbeat
)
self._refresh_receive_timeout_timer()
self.plugin.on_client_connect(self)
def _on_send_heartbeat(self):
self.send_cmd_data(models.Command.HEARTBEAT, {})
self._heartbeat_timer_handle = asyncio.get_running_loop().call_later(
self.HEARTBEAT_INTERVAL, self._on_send_heartbeat
)
def _refresh_receive_timeout_timer(self):
if self._receive_timeout_timer_handle is not None:
self._receive_timeout_timer_handle.cancel()
self._receive_timeout_timer_handle = asyncio.get_running_loop().call_later(
self.RECEIVE_TIMEOUT, self._on_receive_timeout
)
def _on_receive_timeout(self):
logger.info('plugin=%s timed out', self.plugin.id)
self._receive_timeout_timer_handle = None
self.close()
def on_close(self):
logger.info('plugin=%s disconnected', self.plugin.id)
self.plugin.on_client_close(self)
2023-11-05 16:29:11 +08:00
if self._heartbeat_timer_handle is not None:
self._heartbeat_timer_handle.cancel()
self._heartbeat_timer_handle = None
if self._receive_timeout_timer_handle is not None:
self._receive_timeout_timer_handle.cancel()
self._receive_timeout_timer_handle = None
def on_message(self, message):
try:
body = json.loads(message)
cmd = int(body['cmd'])
2023-11-08 00:21:17 +08:00
data = body['data']
2023-11-05 16:29:11 +08:00
if cmd == models.Command.HEARTBEAT:
self._refresh_receive_timeout_timer()
2023-11-08 00:21:17 +08:00
elif cmd == models.Command.LOG_REQ:
logger.log(int(data['level']), '[%s] %s', self.plugin.id, data['msg'])
elif cmd == models.Command.ADD_TEXT_REQ:
self._on_add_text_req(data)
2023-11-05 16:29:11 +08:00
else:
logger.warning('plugin=%s unknown cmd=%d, body=%s', self.plugin.id, cmd, body)
except Exception: # noqa
logger.exception('plugin=%s on_message error, message=%s', self.plugin.id, message)
2023-11-04 16:52:07 +08:00
2023-11-08 00:21:17 +08:00
def _on_add_text_req(self, data: dict):
room_key_dict = data['roomKey']
if room_key_dict is not None:
room_key = services.chat.RoomKey.from_dict(room_key_dict)
room = services.chat.client_room_manager.get_room(room_key)
if room is not None:
rooms = [room]
else:
rooms = []
else:
rooms = list(services.chat.client_room_manager.iter_rooms())
if not rooms:
return
author_name = str(data['authorName'])
if author_name == '':
author_name = self.plugin.id
uid = str(data['uid'])
2023-11-08 00:21:17 +08:00
avatar_url = str(data['avatarUrl'])
if avatar_url == '':
avatar_url = services.avatar.get_default_avatar_url(username=author_name)
2023-11-08 00:21:17 +08:00
data_to_send = api.chat.make_text_message_data(
content=str(data['content']),
author_name=author_name,
uid=uid,
avatar_url=avatar_url,
author_type=int(data['authorType']),
privilege_type=int(data['guardLevel']),
medal_level=int(data['medalLevel']),
translation=str(data['translation']),
)
body_for_room = api.chat.make_message_body(api.chat.Command.ADD_TEXT, data_to_send)
for room in rooms:
room.send_body_no_raise(body_for_room)
extra = services.chat.make_plugin_msg_extra_from_client_room(room)
extra['isFromPlugin'] = True
services.plugin.broadcast_cmd_data(models.Command.ADD_TEXT, data_to_send, extra)
2023-11-04 16:52:07 +08:00
def send_cmd_data(self, cmd, data, extra: Optional[dict] = None):
self.send_body_no_raise(make_message_body(cmd, data, extra))
def send_body_no_raise(self, body: Union[bytes, str, Dict[str, Any]]):
try:
self.write_message(body)
except tornado.websocket.WebSocketClosedError:
self.close()
2024-03-03 11:18:14 +08:00
class RoomsHandler(_PluginApiHandlerBase):
2023-11-08 00:21:17 +08:00
async def get(self):
rooms = [
{
'roomId': live_client.room_id,
'roomKey': live_client.room_key.to_dict(),
}
for live_client in services.chat.iter_live_clients()
]
self.write({'rooms': rooms})
2023-11-04 16:52:07 +08:00
ROUTES = [
2024-03-03 11:18:14 +08:00
(r'/api/plugin/plugins', PluginsHandler),
(r'/api/plugin/enable_plugin', EnableHandler),
(r'/api/plugin/open_admin_ui', OpenAdminUiHandler),
2023-11-04 16:52:07 +08:00
(r'/api/plugin/websocket', PluginWsHandler),
2023-11-08 00:21:17 +08:00
(r'/api/plugin/rooms', RoomsHandler),
2023-11-04 16:52:07 +08:00
]