test.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  2. # █▀█ █ █ █ █▀█ █▀▄ █
  3. # © Copyright 2022
  4. # https://t.me/hikariatama
  5. #
  6. # 🔒 Licensed under the GNU AGPLv3
  7. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  8. import inspect
  9. import logging
  10. import os
  11. import random
  12. import time
  13. from io import BytesIO
  14. import typing
  15. from telethon.tl.functions.channels import EditAdminRequest, InviteToChannelRequest
  16. from telethon.tl.types import ChatAdminRights, Message
  17. from .. import loader, main, utils
  18. from ..inline.types import InlineCall
  19. logger = logging.getLogger(__name__)
  20. DEBUG_MODS_DIR = os.path.join(utils.get_base_dir(), "debug_modules")
  21. if not os.path.isdir(DEBUG_MODS_DIR):
  22. os.mkdir(DEBUG_MODS_DIR, mode=0o755)
  23. for mod in os.scandir(DEBUG_MODS_DIR):
  24. os.remove(mod.path)
  25. @loader.tds
  26. class TestMod(loader.Module):
  27. """Perform operations based on userbot self-testing"""
  28. _memory = {}
  29. strings = {
  30. "name": "Tester",
  31. "set_loglevel": "🚫 <b>Please specify verbosity as an integer or string</b>",
  32. "no_logs": "ℹ️ <b>You don't have any logs at verbosity {}.</b>",
  33. "logs_filename": "hikka-logs.txt",
  34. "logs_caption": (
  35. "<emoji document_id=5188377234380954537>🌘</emoji> <b>Hikka logs with"
  36. " verbosity </b><code>{}</code>\n\n<emoji"
  37. " document_id=5454390891466726015>👋</emoji> <b>Hikka version:"
  38. " {}.{}.{}</b>{}\n<emoji document_id=6321050180095313397>⏱</emoji>"
  39. " <b>Uptime: {}</b>\n<b>{}</b>\n\n<b>{} NoNick</b>\n<b>{} Grep</b>\n<b>{}"
  40. " InlineLogs</b>"
  41. ),
  42. "suspend_invalid_time": (
  43. "<emoji document_id=5416024721705673488>💀</emoji> <b>Invalid time to"
  44. " suspend</b>"
  45. ),
  46. "suspended": (
  47. "<emoji document_id=5452023368054216810>🥶</emoji> <b>Bot suspended"
  48. " for</b> <code>{}</code> <b>seconds</b>"
  49. ),
  50. "results_ping": (
  51. "<emoji document_id=6321050180095313397>⏱</emoji> <b>Telegram ping:</b>"
  52. " <code>{}</code> <b>ms</b>\n<emoji"
  53. " document_id=5377371691078916778>😎</emoji> <b>Uptime: {}</b>"
  54. ),
  55. "ping_hint": (
  56. "<emoji document_id=5472146462362048818>💡</emoji> <i>Telegram ping mostly"
  57. " depends on Telegram servers latency and other external factors and has"
  58. " nothing to do with the parameters of server on which userbot is"
  59. " installed</i>"
  60. ),
  61. "confidential": (
  62. "⚠️ <b>Log level </b><code>{}</code><b> may reveal your confidential info,"
  63. " be careful</b>"
  64. ),
  65. "confidential_text": (
  66. "⚠️ <b>Log level </b><code>{0}</code><b> may reveal your confidential info,"
  67. " be careful</b>\n<b>Type </b><code>.logs {0} force_insecure</code><b> to"
  68. " ignore this warning</b>"
  69. ),
  70. "choose_loglevel": "💁‍♂️ <b>Choose log level</b>",
  71. "bad_module": "🚫 <b>Module not found</b>",
  72. "debugging_enabled": (
  73. "🧑‍💻 <b>Debugging mode enabled for module </b><code>{0}</code>\n<i>Go to"
  74. " directory named `debug_modules`, edit file named `{0}.py` and see changes"
  75. " in real time</i>"
  76. ),
  77. "debugging_disabled": "✅ <b>Debugging disabled</b>",
  78. }
  79. strings_ru = {
  80. "set_loglevel": "🚫 <b>Укажи уровень логов числом или строкой</b>",
  81. "no_logs": "ℹ️ <b>У тебя нет логов уровня {}.</b>",
  82. "logs_filename": "hikka-logs.txt",
  83. "logs_caption": (
  84. "<emoji document_id=5188377234380954537>🌘</emoji> <b>Логи Hikka уровня"
  85. " </b><code>{}</code>\n\n<emoji document_id=5454390891466726015>👋</emoji>"
  86. " <b>Версия Hikka: {}.{}.{}</b>{}\n<emoji"
  87. " document_id=6321050180095313397>⏱</emoji> <b>Uptime:"
  88. " {}</b>\n<b>{}</b>\n\n<b>{} NoNick</b>\n<b>{} Grep</b>\n<b>{}"
  89. " InlineLogs</b>"
  90. ),
  91. "bad_module": "🚫 <b>Модуль не найден</b>",
  92. "debugging_enabled": (
  93. "🧑‍💻 <b>Режим разработчика включен для модуля"
  94. " </b><code>{0}</code>\n<i>Отправляйся в директорию `debug_modules`,"
  95. " изменяй файл `{0}.py`, и смотри изменения в режиме реального времени</i>"
  96. ),
  97. "debugging_disabled": "✅ <b>Режим разработчика выключен</b>",
  98. "suspend_invalid_time": (
  99. "<emoji document_id=5416024721705673488>💀</emoji> <b>Неверное время"
  100. " заморозки</b>"
  101. ),
  102. "suspended": (
  103. "<emoji document_id=5452023368054216810>🥶</emoji> <b>Бот заморожен на</b>"
  104. " <code>{}</code> <b>секунд</b>"
  105. ),
  106. "results_ping": (
  107. "<emoji document_id=6321050180095313397>⏱</emoji> <b>Скорость отклика"
  108. " Telegram:</b> <code>{}</code> <b>ms</b>\n<emoji"
  109. " document_id=5377371691078916778>😎</emoji> <b>Прошло с последней"
  110. " перезагрузки: {}</b>"
  111. ),
  112. "ping_hint": (
  113. "<emoji document_id=5472146462362048818>💡</emoji> <i>Скорость отклика"
  114. " Telegram в большей степени зависит от загруженности серверов Telegram и"
  115. " других внешних факторов и никак не связана с параметрами сервера, на"
  116. " который установлен юзербот</i>"
  117. ),
  118. "confidential": (
  119. "⚠️ <b>Уровень логов </b><code>{}</code><b> может содержать личную"
  120. " информацию, будь осторожен</b>"
  121. ),
  122. "confidential_text": (
  123. "⚠️ <b>Уровень логов </b><code>{0}</code><b> может содержать личную"
  124. " информацию, будь осторожен</b>\n<b>Напиши </b><code>.logs {0}"
  125. " force_insecure</code><b>, чтобы отправить логи игнорируя"
  126. " предупреждение</b>"
  127. ),
  128. "choose_loglevel": "💁‍♂️ <b>Выбери уровень логов</b>",
  129. "_cmd_doc_dump": "Показать информацию о сообщении",
  130. "_cmd_doc_logs": (
  131. "<уровень> - Отправляет лог-файл. Уровни ниже WARNING могут содержать"
  132. " личную инфомрацию."
  133. ),
  134. "_cmd_doc_suspend": "<время> - Заморозить бота на некоторое время",
  135. "_cmd_doc_ping": "Проверяет скорость отклика юзербота",
  136. "_cls_doc": "Операции, связанные с самотестированием",
  137. }
  138. def __init__(self):
  139. self.config = loader.ModuleConfig(
  140. loader.ConfigValue(
  141. "force_send_all",
  142. False,
  143. "⚠️ Do not touch, if you don't know what it does!\nBy default, Hikka"
  144. " will try to determine, which client caused logs. E.g. there is a"
  145. " module TestModule installed on Client1 and TestModule2 on Client2. By"
  146. " default, Client2 will get logs from TestModule2, and Client1 will get"
  147. " logs from TestModule. If this option is enabled, Hikka will send all"
  148. " logs to Client1 and Client2, even if it is not the one that caused"
  149. " the log.",
  150. validator=loader.validators.Boolean(),
  151. on_change=self._pass_config_to_logger,
  152. ),
  153. loader.ConfigValue(
  154. "tglog_level",
  155. "INFO",
  156. "⚠️ Do not touch, if you don't know what it does!\n"
  157. "Minimal loglevel for records to be sent in Telegram.",
  158. validator=loader.validators.Choice(
  159. ["INFO", "WARNING", "ERROR", "CRITICAL"]
  160. ),
  161. on_change=self._pass_config_to_logger,
  162. ),
  163. )
  164. def _pass_config_to_logger(self):
  165. logging.getLogger().handlers[0].force_send_all = self.config["force_send_all"]
  166. logging.getLogger().handlers[0].tg_level = self.config["tglog_level"]
  167. @loader.command(ru_doc="Ответь на сообщение, чтобы показать его дамп")
  168. async def dump(self, message: Message):
  169. """Use in reply to get a dump of a message"""
  170. if not message.is_reply:
  171. return
  172. await utils.answer(
  173. message,
  174. "<code>"
  175. + utils.escape_html((await message.get_reply_message()).stringify())
  176. + "</code>",
  177. )
  178. @loader.loop(interval=1)
  179. async def watchdog(self):
  180. if not os.path.isdir(DEBUG_MODS_DIR):
  181. return
  182. try:
  183. for module in os.scandir(DEBUG_MODS_DIR):
  184. last_modified = os.stat(module.path).st_mtime
  185. cls_ = module.path.split("/")[-1].split(".py")[0]
  186. if cls_ not in self._memory:
  187. self._memory[cls_] = last_modified
  188. continue
  189. if self._memory[cls_] == last_modified:
  190. continue
  191. self._memory[cls_] = last_modified
  192. logger.debug(f"Reloading debug module {cls_}")
  193. with open(module.path, "r") as f:
  194. try:
  195. await next(
  196. module
  197. for module in self.allmodules.modules
  198. if module.__class__.__name__ == "LoaderMod"
  199. ).load_module(
  200. f.read(),
  201. None,
  202. save_fs=False,
  203. )
  204. except Exception:
  205. logger.exception("Failed to reload module in watchdog")
  206. except Exception:
  207. logger.exception("Failed debugging watchdog")
  208. return
  209. @loader.command(
  210. ru_doc=(
  211. "[модуль] - Для разработчиков: открыть модуль в режиме дебага и применять"
  212. " изменения из него в режиме реального времени"
  213. )
  214. )
  215. async def debugmod(self, message: Message):
  216. """[module] - For developers: Open module for debugging
  217. You will be able to track changes in real-time"""
  218. args = utils.get_args_raw(message)
  219. instance = None
  220. for module in self.allmodules.modules:
  221. if (
  222. module.__class__.__name__.lower() == args.lower()
  223. or module.strings["name"].lower() == args.lower()
  224. ):
  225. if os.path.isfile(
  226. os.path.join(
  227. DEBUG_MODS_DIR,
  228. f"{module.__class__.__name__}.py",
  229. )
  230. ):
  231. os.remove(
  232. os.path.join(
  233. DEBUG_MODS_DIR,
  234. f"{module.__class__.__name__}.py",
  235. )
  236. )
  237. try:
  238. delattr(module, "hikka_debug")
  239. except AttributeError:
  240. pass
  241. await utils.answer(message, self.strings("debugging_disabled"))
  242. return
  243. module.hikka_debug = True
  244. instance = module
  245. break
  246. if not instance:
  247. await utils.answer(message, self.strings("bad_module"))
  248. return
  249. with open(
  250. os.path.join(
  251. DEBUG_MODS_DIR,
  252. f"{instance.__class__.__name__}.py",
  253. ),
  254. "wb",
  255. ) as f:
  256. f.write(inspect.getmodule(instance).__loader__.data)
  257. await utils.answer(
  258. message,
  259. self.strings("debugging_enabled").format(instance.__class__.__name__),
  260. )
  261. @loader.command(ru_doc="<уровень> - Показать логи")
  262. async def logs(
  263. self,
  264. message: typing.Union[Message, InlineCall],
  265. force: bool = False,
  266. lvl: typing.Union[int, None] = None,
  267. ):
  268. """<level> - Dump logs"""
  269. if not isinstance(lvl, int):
  270. args = utils.get_args_raw(message)
  271. try:
  272. try:
  273. lvl = int(args.split()[0])
  274. except ValueError:
  275. lvl = getattr(logging, args.split()[0].upper(), None)
  276. except IndexError:
  277. lvl = None
  278. if not isinstance(lvl, int):
  279. try:
  280. if not self.inline.init_complete or not await self.inline.form(
  281. text=self.strings("choose_loglevel"),
  282. reply_markup=[
  283. [
  284. {
  285. "text": "🚨 Critical",
  286. "callback": self.logs,
  287. "args": (False, 50),
  288. },
  289. {
  290. "text": "🚫 Error",
  291. "callback": self.logs,
  292. "args": (False, 40),
  293. },
  294. ],
  295. [
  296. {
  297. "text": "⚠️ Warning",
  298. "callback": self.logs,
  299. "args": (False, 30),
  300. },
  301. {
  302. "text": "ℹ️ Info",
  303. "callback": self.logs,
  304. "args": (False, 20),
  305. },
  306. ],
  307. [
  308. {
  309. "text": "🧑‍💻 Debug",
  310. "callback": self.logs,
  311. "args": (False, 10),
  312. },
  313. {
  314. "text": "👁 All",
  315. "callback": self.logs,
  316. "args": (False, 0),
  317. },
  318. ],
  319. [{"text": "🚫 Cancel", "action": "close"}],
  320. ],
  321. message=message,
  322. ):
  323. raise
  324. except Exception:
  325. await utils.answer(message, self.strings("set_loglevel"))
  326. return
  327. logs = "\n\n".join(
  328. [
  329. "\n".join(
  330. handler.dumps(lvl, client_id=self._client.tg_id)
  331. if "client_id" in inspect.signature(handler.dumps).parameters
  332. else handler.dumps(lvl)
  333. )
  334. for handler in logging.getLogger().handlers
  335. ]
  336. )
  337. named_lvl = (
  338. lvl
  339. if lvl not in logging._levelToName
  340. else logging._levelToName[lvl] # skipcq: PYL-W0212
  341. )
  342. if (
  343. lvl < logging.WARNING
  344. and not force
  345. and (
  346. not isinstance(message, Message)
  347. or "force_insecure" not in message.raw_text.lower()
  348. )
  349. ):
  350. try:
  351. if not self.inline.init_complete:
  352. raise
  353. cfg = {
  354. "text": self.strings("confidential").format(named_lvl),
  355. "reply_markup": [
  356. {
  357. "text": "📤 Send anyway",
  358. "callback": self.logs,
  359. "args": [True, lvl],
  360. },
  361. {"text": "🚫 Cancel", "action": "close"},
  362. ],
  363. }
  364. if isinstance(message, Message):
  365. if not await self.inline.form(**cfg, message=message):
  366. raise
  367. else:
  368. await message.edit(**cfg)
  369. except Exception:
  370. await utils.answer(
  371. message,
  372. self.strings("confidential_text").format(named_lvl),
  373. )
  374. return
  375. if len(logs) <= 2:
  376. if isinstance(message, Message):
  377. await utils.answer(message, self.strings("no_logs").format(named_lvl))
  378. else:
  379. await message.edit(self.strings("no_logs").format(named_lvl))
  380. await message.unload()
  381. return
  382. if btoken := self._db.get("hikka.inline", "bot_token", False):
  383. logs = logs.replace(
  384. btoken,
  385. f'{btoken.split(":")[0]}:***************************',
  386. )
  387. if hikka_token := self._db.get("HikkaDL", "token", False):
  388. logs = logs.replace(
  389. hikka_token,
  390. f'{hikka_token.split("_")[0]}_********************************',
  391. )
  392. if hikka_token := self._db.get("Kirito", "token", False):
  393. logs = logs.replace(
  394. hikka_token,
  395. f'{hikka_token.split("_")[0]}_********************************',
  396. )
  397. if os.environ.get("DATABASE_URL"):
  398. logs = logs.replace(
  399. os.environ.get("DATABASE_URL"),
  400. "postgre://**************************",
  401. )
  402. if os.environ.get("REDIS_URL"):
  403. logs = logs.replace(
  404. os.environ.get("REDIS_URL"),
  405. "postgre://**************************",
  406. )
  407. if os.environ.get("hikka_session"):
  408. logs = logs.replace(
  409. os.environ.get("hikka_session"),
  410. "StringSession(**************************)",
  411. )
  412. logs = BytesIO(logs.encode("utf-16"))
  413. logs.name = self.strings("logs_filename")
  414. ghash = utils.get_git_hash()
  415. other = (
  416. *main.__version__,
  417. " <i><a"
  418. f' href="https://github.com/hikariatama/Hikka/commit/{ghash}">({ghash[:8]})</a></i>'
  419. if ghash
  420. else "",
  421. utils.formatted_uptime(),
  422. utils.get_named_platform(),
  423. "✅" if self._db.get(main.__name__, "no_nickname", False) else "🚫",
  424. "✅" if self._db.get(main.__name__, "grep", False) else "🚫",
  425. "✅" if self._db.get(main.__name__, "inlinelogs", False) else "🚫",
  426. )
  427. if getattr(message, "out", True):
  428. await message.delete()
  429. if isinstance(message, Message):
  430. await utils.answer(
  431. message,
  432. logs,
  433. caption=self.strings("logs_caption").format(named_lvl, *other),
  434. )
  435. else:
  436. await self._client.send_file(
  437. message.form["chat"],
  438. logs,
  439. caption=self.strings("logs_caption").format(named_lvl, *other),
  440. )
  441. @loader.owner
  442. @loader.command(ru_doc="<время> - Заморозить бота на N секунд")
  443. async def suspend(self, message: Message):
  444. """<time> - Suspends the bot for N seconds"""
  445. try:
  446. time_sleep = float(utils.get_args_raw(message))
  447. await utils.answer(
  448. message,
  449. self.strings("suspended").format(time_sleep),
  450. )
  451. time.sleep(time_sleep)
  452. except ValueError:
  453. await utils.answer(message, self.strings("suspend_invalid_time"))
  454. @loader.command(ru_doc="Проверить скорость отклика юзербота")
  455. async def ping(self, message: Message):
  456. """Test your userbot ping"""
  457. start = time.perf_counter_ns()
  458. message = await utils.answer(message, "<code>🐻 Nofin...</code>")
  459. await utils.answer(
  460. message,
  461. self.strings("results_ping").format(
  462. round((time.perf_counter_ns() - start) / 10**6, 3),
  463. utils.formatted_uptime(),
  464. )
  465. + (
  466. ("\n\n" + self.strings("ping_hint"))
  467. if random.choice([0, 0, 1]) == 1
  468. else ""
  469. ),
  470. )
  471. async def client_ready(self):
  472. chat, is_new = await utils.asset_channel(
  473. self._client,
  474. "hikka-logs",
  475. "🌘 Your Hikka logs will appear in this chat",
  476. silent=True,
  477. avatar="https://github.com/hikariatama/assets/raw/master/hikka-logs.png",
  478. )
  479. self._logchat = int(f"-100{chat.id}")
  480. self.watchdog.start()
  481. if not is_new and any(
  482. participant.id == self.inline.bot_id
  483. for participant in (await self._client.get_participants(chat, limit=3))
  484. ):
  485. logging.getLogger().handlers[0].install_tg_log(self)
  486. logger.debug("Bot logging installed for %s", self._logchat)
  487. return
  488. logger.debug("New logging chat created, init setup...")
  489. try:
  490. await self._client(InviteToChannelRequest(chat, [self.inline.bot_username]))
  491. except Exception:
  492. logger.warning("Unable to invite logger to chat")
  493. try:
  494. await self._client(
  495. EditAdminRequest(
  496. channel=chat,
  497. user_id=self.inline.bot_username,
  498. admin_rights=ChatAdminRights(ban_users=True),
  499. rank="Logger",
  500. )
  501. )
  502. except Exception:
  503. pass
  504. logging.getLogger().handlers[0].install_tg_log(self)
  505. logger.debug("Bot logging installed for %s", self._logchat)