103 lines
3.8 KiB
Python
103 lines
3.8 KiB
Python
"""
|
|
This plugin provides a command to check if a website or server is up.
|
|
"""
|
|
|
|
# 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, config):
|
|
"""
|
|
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.
|
|
bot (Bot): The bot instance.
|
|
prefix (str): The bot command prefix.
|
|
config (FunguyConfig): The bot configuration instance.
|
|
|
|
Returns:
|
|
None
|
|
"""
|
|
# Check if the message matches the command pattern and is not from this bot
|
|
match = botlib.MessageMatch(room, message, bot, prefix)
|
|
if match.is_not_from_this_bot() and match.prefix() and match.command("isup"):
|
|
# Log that the !isup command has been received
|
|
logging.info("Received !isup command")
|
|
args = match.args()
|
|
# Check if the command has exactly one argument
|
|
if len(args) != 1:
|
|
# If the command does not have exactly one argument, send usage message
|
|
await bot.api.send_markdown_message(room.room_id, "Usage: !isup <ipv4/ipv6/domain>")
|
|
logging.info("Sent usage message to the room")
|
|
return
|
|
|
|
target = args[0]
|
|
|
|
# Perform DNS resolution
|
|
try:
|
|
ip_address = socket.gethostbyname(target)
|
|
# Log successful DNS resolution
|
|
logging.info(f"DNS resolution successful for {target}: {ip_address}")
|
|
# Send DNS resolution success message
|
|
await bot.api.send_markdown_message(room.room_id, f"✅ DNS resolution successful for **{target}**: **{ip_address}** (A record)")
|
|
except socket.gaierror:
|
|
# Log DNS resolution failure
|
|
logging.info(f"DNS resolution failed for {target}")
|
|
# Send DNS resolution failure message
|
|
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):
|
|
# If HTTP service is up, send HTTP service up message
|
|
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):
|
|
# If HTTPS service is up, send HTTPS service up message
|
|
await bot.api.send_markdown_message(room.room_id, f"🖧 **{target}** HTTPS service is up")
|
|
logging.info(f"{target} HTTPS service is up")
|
|
else:
|
|
# If both HTTP and HTTPS services are down, send service down message
|
|
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")
|
|
|