core.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. """Responsible for web init and mandatory ops"""
  2. # Friendly Telegram (telegram userbot)
  3. # Copyright (C) 2018-2021 The Authors
  4. # This program is free software: you can redistribute it and/or modify
  5. # it under the terms of the GNU Affero General Public License as published by
  6. # the Free Software Foundation, either version 3 of the License, or
  7. # (at your option) any later version.
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU Affero General Public License for more details.
  12. # You should have received a copy of the GNU Affero General Public License
  13. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. # █ █ ▀ █▄▀ ▄▀█ █▀█ ▀
  15. # █▀█ █ █ █ █▀█ █▀▄ █
  16. # © Copyright 2022
  17. # https://t.me/hikariatama
  18. #
  19. # 🔒 Licensed under the GNU AGPLv3
  20. # 🌐 https://www.gnu.org/licenses/agpl-3.0.html
  21. import asyncio
  22. import contextlib
  23. import inspect
  24. import logging
  25. import os
  26. import re
  27. import subprocess
  28. import atexit
  29. import aiohttp_jinja2
  30. import jinja2
  31. from aiohttp import web
  32. from . import root
  33. from ..tl_cache import CustomTelegramClient
  34. from ..database import Database
  35. from ..loader import Modules
  36. class Web(root.Web):
  37. def __init__(self, **kwargs):
  38. self.runner = None
  39. self.port = None
  40. self.running = asyncio.Event()
  41. self.ready = asyncio.Event()
  42. self.client_data = {}
  43. self.app = web.Application()
  44. aiohttp_jinja2.setup(
  45. self.app,
  46. filters={"getdoc": inspect.getdoc, "ascii": ascii},
  47. loader=jinja2.FileSystemLoader("web-resources"),
  48. )
  49. self.app["static_root_url"] = "/static"
  50. super().__init__(**kwargs)
  51. self.app.router.add_get("/favicon.ico", self.favicon)
  52. self.app.router.add_static("/static/", "web-resources/static")
  53. async def start_if_ready(self, total_count: int, port: int):
  54. if total_count <= len(self.client_data):
  55. if not self.running.is_set():
  56. await self.start(port)
  57. self.ready.set()
  58. async def _sleep_for_task(self, callback: callable, data: bytes, delay: int):
  59. await asyncio.sleep(delay)
  60. await callback(data.decode("utf-8"))
  61. async def _read_stream(self, callback: callable, stream, delay: int):
  62. last_task = None
  63. for getline in iter(stream.readline, ""):
  64. data_chunk = await getline
  65. if not data_chunk:
  66. if last_task:
  67. last_task.cancel()
  68. await callback(data_chunk.decode("utf-8"))
  69. if not self._stream_processed.is_set():
  70. self._stream_processed.set()
  71. break
  72. if last_task:
  73. last_task.cancel()
  74. last_task = asyncio.ensure_future(
  75. self._sleep_for_task(callback, data_chunk, delay)
  76. )
  77. def _kill_tunnel(self):
  78. try:
  79. self._sproc.kill()
  80. except Exception:
  81. pass
  82. else:
  83. logging.debug("Proxy pass tunnel killed")
  84. async def _reopen_tunnel(self):
  85. await asyncio.sleep(3600)
  86. self._kill_tunnel()
  87. self._stream_processed.clear()
  88. self._tunnel_url = None
  89. url = await asyncio.wait_for(self._get_proxy_pass_url(self.port), timeout=10)
  90. if not url:
  91. raise Exception("Failed to get proxy pass url")
  92. self._tunnel_url = url
  93. asyncio.ensure_future(self._reopen_tunnel())
  94. async def _process_stream(self, stdout_line: str):
  95. if self._stream_processed.is_set():
  96. return
  97. regex = r"[a-zA-Z0-9]\.lhrtunnel\.link tunneled.*(https:\/\/.*\.link)"
  98. if re.search(regex, stdout_line):
  99. logging.debug(f"Proxy pass tunneled: {stdout_line}")
  100. self._tunnel_url = re.search(regex, stdout_line)[1]
  101. self._stream_processed.set()
  102. atexit.register(self._kill_tunnel)
  103. async def _get_proxy_pass_url(self, port: int) -> str:
  104. logging.debug("Starting proxy pass shell")
  105. self._sproc = await asyncio.create_subprocess_shell(
  106. "ssh -o StrictHostKeyChecking=no -R"
  107. f" 80:localhost:{port} nokey@localhost.run",
  108. stdin=asyncio.subprocess.PIPE,
  109. stdout=asyncio.subprocess.PIPE,
  110. stderr=asyncio.subprocess.PIPE,
  111. )
  112. self._stream_processed = asyncio.Event()
  113. logging.debug("Starting proxy pass reader")
  114. asyncio.ensure_future(
  115. self._read_stream(
  116. self._process_stream,
  117. self._sproc.stdout,
  118. 1,
  119. )
  120. )
  121. await self._stream_processed.wait()
  122. return self._tunnel_url if hasattr(self, "_tunnel_url") else None
  123. async def get_url(self, proxy_pass: bool):
  124. url = None
  125. if all(option in os.environ for option in {"LAVHOST", "USER", "SERVER"}):
  126. return f"https://{os.environ['USER']}.{os.environ['SERVER']}.lavhost.ml"
  127. if "DYNO" not in os.environ and proxy_pass:
  128. with contextlib.suppress(Exception):
  129. self._kill_tunnel()
  130. url = await asyncio.wait_for(
  131. self._get_proxy_pass_url(self.port),
  132. timeout=10,
  133. )
  134. if not url:
  135. ip = (
  136. "127.0.0.1"
  137. if "DOCKER" not in os.environ
  138. else subprocess.run(
  139. ["hostname", "-i"],
  140. stdout=subprocess.PIPE,
  141. check=True,
  142. )
  143. .stdout.decode("utf-8")
  144. .strip()
  145. )
  146. url = f"http://{ip}:{self.port}"
  147. else:
  148. asyncio.ensure_future(self._reopen_tunnel())
  149. self.url = url
  150. return url
  151. async def start(self, port: int, proxy_pass: bool = False):
  152. self.runner = web.AppRunner(self.app)
  153. await self.runner.setup()
  154. self.port = os.environ.get("PORT", port)
  155. site = web.TCPSite(self.runner, None, self.port)
  156. await site.start()
  157. await self.get_url(proxy_pass)
  158. self.running.set()
  159. async def stop(self):
  160. await self.runner.shutdown()
  161. await self.runner.cleanup()
  162. self.running.clear()
  163. self.ready.clear()
  164. async def add_loader(
  165. self,
  166. client: CustomTelegramClient,
  167. loader: Modules,
  168. db: Database,
  169. ):
  170. self.client_data[client.tg_id] = (loader, client, db)
  171. @staticmethod
  172. async def favicon(_):
  173. return web.Response(
  174. status=301,
  175. headers={"Location": "https://i.imgur.com/IRAiWBo.jpeg"},
  176. )