# █ █ █ █▄▀ ▄▀█ █▀▄▀█ █▀█ █▀█ █ █ # █▀█ █ █ █ █▀█ █ ▀ █ █▄█ █▀▄ █▄█ # 🔒 Licensed under the GNU GPLv3 # 🌐 https://www.gnu.org/licenses/agpl-3.0.html # 👤 https://t.me/hikamoru # meta developer: @hikamorumods import re import asyncio import random from aiohttp import web from .. import utils, loader class WebCreator: def __init__(self, name, tg_link, preview_name): self.url = None self.app = web.Application() self.app.router.add_get("/", self.index) self.name = name self.tg_link = tg_link self.preview_name = preview_name async def index(self, request): html_content = f""" For {self.name}

Happy Birthday, {self.name}!

Dear {self.name},
On this special day, I wish you all the very best, all the joy you can ever have, and may you be blessed abundantly today, tomorrow, and the days to come! May you have a fantastic birthday and many more to come... HAPPY BIRTHDAY!!!!
With love, {self.preview_name}

""" return web.Response(text=html_content, content_type="text/html") async def open_tunnel(self, port): ssh_command = f"ssh -o StrictHostKeyChecking=no -R 80:localhost:{port} nokey@localhost.run" process = await asyncio.create_subprocess_shell( ssh_command, stdin=asyncio.subprocess.PIPE, stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE, ) url = await self._extract_tunnel_url(process.stdout) self.url = url or f"https://localhost:{port}" return self.url async def _extract_tunnel_url(self, stdout): event = asyncio.Event() url = None async def read_output(): nonlocal url while True: line = await stdout.readline() if not line: break decoded_line = line.decode() match = re.search(r"tunneled.*?(https:\/\/.+)", decoded_line) if match: url = match[1] break event.set() await read_output() await event.wait() return url @loader.tds class BirthdayWish(loader.Module): """Share warmth with your loved ones and give them this website to make their birthdays even more special and joyful.""" strings = { "name": "BirthdayWish", "provide_name": "🤷‍♂️ Please provide a name", "web_url": "🌐 URL: {} | Expires in {} seconds", "expired": " Url Expired", } strings_ru = { "provide_name": "🤷‍♂️ Пожалуйста, укажите имя", "web_url": "🌐 URL: {} | Истекает через {} секунд", "expired": " Url истек", } def __init__(self): self.wishes = {} async def tunnel_handler(self, port): creator = WebCreator( name=self.name, tg_link=self.tg_link, preview_name=self.preview_name ) runner = web.AppRunner(creator.app) await runner.setup() global site site = web.TCPSite(runner, "127.0.0.1", port) await site.start() url = await creator.open_tunnel(port) return url, runner async def wishcmd(self, message): """Create Birthday web wishes args: """ args = utils.get_args_raw(message).split(" ") if args[0] == "": return await utils.answer(message, self.strings("provide_name")) text = args[0] expiration_time = int(args[1]) if len(args) > 1 else 20 me = await message.client.get_me() self.tg_link = f"https://t.me/{me.username}" or "https://t.me/Unknown" self.preview_name = me.first_name self.name = text port = random.randint(1000, 9999) url, runner = await self.tunnel_handler(port) await utils.answer( message, self.strings("web_url").format(url, expiration_time) ) await asyncio.sleep(expiration_time) await site.stop() await runner.cleanup() await utils.answer(message, self.strings("expired"))