""" This plugin provides a command to check if a website or server is up. """ import logging import aiohttp import socket import simplematrixbotlib as botlib from plugins.utils import is_public_destination async def check_http(domain): """Check if HTTP service is up for the given domain.""" 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.""" 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, config): """Handle the !isup command.""" 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 ") return target = args[0] try: ip_address = socket.gethostbyname(target) 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 if not is_public_destination(ip_address): await bot.api.send_text_message(room.room_id, "❌ Checking internal/private IPs is not allowed.") return await bot.api.send_markdown_message(room.room_id, f"✅ DNS resolution successful for **{target}**: **{ip_address}** (A record)") if await check_http(target): await bot.api.send_markdown_message(room.room_id, 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") else: await bot.api.send_markdown_message(room.room_id, f"😕 **{target}** HTTP/HTTPS services are down") # --------------------------------------------------------------------------- # Plugin Metadata # --------------------------------------------------------------------------- __version__ = "1.0.1" __author__ = "Funguy Bot" __description__ = "Check if a site is up" __help__ = """
!isup – Is it up?

!isup <domain or IP> – Performs DNS resolution and checks HTTP/HTTPS availability.

"""