# plugins/isup.py import logging import aiohttp import socket import simplematrixbotlib as botlib async def check_http(domain): """ Check if HTTP service is up for the given domain. Args: domain (str): The target domain. Returns: bool: True if HTTP service is up, False otherwise. """ try: async with aiohttp.ClientSession() as session: async with session.get(f"http://{domain}") as response: return response.status == 200 except aiohttp.ClientError: return False async def check_https(domain): """ Check if HTTPS service is up for the given domain. Args: domain (str): The target domain. Returns: bool: True if HTTPS service is up, False otherwise. """ try: async with aiohttp.ClientSession() as session: async with session.get(f"https://{domain}") as response: return response.status == 200 except aiohttp.ClientError: return False async def handle_command(room, message, bot, PREFIX): """ Function to handle the !isup command. Args: room (Room): The Matrix room where the command was invoked. message (RoomMessage): The message object containing the command. Returns: None """ match = botlib.MessageMatch(room, message, bot, PREFIX) if match.is_not_from_this_bot() and match.prefix() and match.command("isup"): logging.info("Received !isup command") args = match.args() if len(args) != 1: await bot.api.send_markdown_message(room.room_id, "Usage: !isup ") logging.info("Sent usage message to the room") return target = args[0] # DNS resolution try: ip_address = socket.gethostbyname(target) logging.info(f"DNS resolution successful for {target}: {ip_address}") await bot.api.send_markdown_message(room.room_id, f"✅ DNS resolution successful for **{target}**: **{ip_address}** (A record)") except socket.gaierror: logging.info(f"DNS resolution failed for {target}") await bot.api.send_markdown_message(room.room_id, f"❌ DNS resolution failed for **{target}**") return # Check HTTP/HTTPS services if await check_http(target): await bot.api.send_markdown_message(room.room_id, f"🖧 **{target}** HTTP service is up") logging.info(f"{target} HTTP service is up") elif await check_https(target): await bot.api.send_markdown_message(room.room_id, f"🖧 **{target}** HTTPS service is up") logging.info(f"{target} HTTPS service is up") else: await bot.api.send_markdown_message(room.room_id, f"😕 **{target}** HTTP/HTTPS services are down") logging.info(f"{target} HTTP/HTTPS services are down")