core.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. class Web(root.Web):
  34. def __init__(self, **kwargs):
  35. self.runner = None
  36. self.port = None
  37. self.running = asyncio.Event()
  38. self.ready = asyncio.Event()
  39. self.client_data = {}
  40. self.app = web.Application()
  41. aiohttp_jinja2.setup(
  42. self.app,
  43. filters={"getdoc": inspect.getdoc, "ascii": ascii},
  44. loader=jinja2.FileSystemLoader("web-resources"),
  45. )
  46. self.app["static_root_url"] = "/static"
  47. super().__init__(**kwargs)
  48. self.app.router.add_get("/favicon.ico", self.favicon)
  49. self.app.router.add_static("/static/", "web-resources/static")
  50. async def start_if_ready(self, total_count: int, port: int):
  51. if total_count <= len(self.client_data):
  52. if not self.running.is_set():
  53. await self.start(port)
  54. self.ready.set()
  55. async def _sleep_for_task(self, callback: callable, data: bytes, delay: int):
  56. await asyncio.sleep(delay)
  57. await callback(data.decode("utf-8"))
  58. async def _read_stream(self, callback: callable, stream, delay: int):
  59. last_task = None
  60. for getline in iter(stream.readline, ""):
  61. data_chunk = await getline
  62. if not data_chunk:
  63. if last_task:
  64. last_task.cancel()
  65. await callback(data_chunk.decode("utf-8"))
  66. if not self._stream_processed.is_set():
  67. self._stream_processed.set()
  68. break
  69. if last_task:
  70. last_task.cancel()
  71. last_task = asyncio.ensure_future(
  72. self._sleep_for_task(callback, data_chunk, delay)
  73. )
  74. def _kill_tunnel(self):
  75. try:
  76. self._sproc.kill()
  77. except Exception:
  78. pass
  79. else:
  80. logging.debug("Proxy pass tunnel killed")
  81. async def _reopen_tunnel(self):
  82. await asyncio.sleep(3600)
  83. self._kill_tunnel()
  84. self._stream_processed.clear()
  85. self._tunnel_url = None
  86. url = await asyncio.wait_for(self._get_proxy_pass_url(self.port), timeout=10)
  87. if not url:
  88. raise Exception("Failed to get proxy pass url")
  89. self._tunnel_url = url
  90. asyncio.ensure_future(self._reopen_tunnel())
  91. async def _process_stream(self, stdout_line: str):
  92. if self._stream_processed.is_set():
  93. return
  94. regex = r"[a-zA-Z0-9]\.lhrtunnel\.link tunneled.*(https:\/\/.*\.link)"
  95. if re.search(regex, stdout_line):
  96. logging.debug(f"Proxy pass tunneled: {stdout_line}")
  97. self._tunnel_url = re.search(regex, stdout_line)[1]
  98. self._stream_processed.set()
  99. atexit.register(self._kill_tunnel)
  100. async def _get_proxy_pass_url(self, port: int) -> str:
  101. logging.debug("Starting proxy pass shell")
  102. self._sproc = await asyncio.create_subprocess_shell(
  103. "ssh -o StrictHostKeyChecking=no -R"
  104. f" 80:localhost:{port} nokey@localhost.run",
  105. stdin=asyncio.subprocess.PIPE,
  106. stdout=asyncio.subprocess.PIPE,
  107. stderr=asyncio.subprocess.PIPE,
  108. )
  109. self._stream_processed = asyncio.Event()
  110. logging.debug("Starting proxy pass reader")
  111. asyncio.ensure_future(
  112. self._read_stream(
  113. self._process_stream,
  114. self._sproc.stdout,
  115. 1,
  116. )
  117. )
  118. await self._stream_processed.wait()
  119. return self._tunnel_url if hasattr(self, "_tunnel_url") else None
  120. async def get_url(self, proxy_pass: bool):
  121. url = None
  122. if all(option in os.environ for option in {"LAVHOST", "USER", "SERVER"}):
  123. return f"https://{os.environ['USER']}.{os.environ['SERVER']}.lavhost.ml"
  124. if "DYNO" not in os.environ and proxy_pass:
  125. with contextlib.suppress(Exception):
  126. self._kill_tunnel()
  127. url = await asyncio.wait_for(
  128. self._get_proxy_pass_url(self.port),
  129. timeout=10,
  130. )
  131. if not url:
  132. ip = (
  133. "127.0.0.1"
  134. if "DOCKER" not in os.environ
  135. else subprocess.run(
  136. ["hostname", "-i"],
  137. stdout=subprocess.PIPE,
  138. check=True,
  139. )
  140. .stdout.decode("utf-8")
  141. .strip()
  142. )
  143. url = f"http://{ip}:{self.port}"
  144. else:
  145. asyncio.ensure_future(self._reopen_tunnel())
  146. self.url = url
  147. return url
  148. async def start(self, port: int, proxy_pass: bool = False):
  149. self.runner = web.AppRunner(self.app)
  150. await self.runner.setup()
  151. self.port = os.environ.get("PORT", port)
  152. site = web.TCPSite(self.runner, None, self.port)
  153. await site.start()
  154. await self.get_url(proxy_pass)
  155. self.running.set()
  156. async def stop(self):
  157. await self.runner.shutdown()
  158. await self.runner.cleanup()
  159. self.running.clear()
  160. self.ready.clear()
  161. async def add_loader(
  162. self,
  163. client: "TelegramClient", # type: ignore
  164. loader: "Modules", # type: ignore
  165. db: "Database", # type: ignore
  166. ):
  167. self.client_data[client.tg_id] = (loader, client, db)
  168. @staticmethod
  169. async def favicon(_):
  170. return web.Response(
  171. status=301,
  172. headers={"Location": "https://i.imgur.com/IRAiWBo.jpeg"},
  173. )