fixed authorization issue

This commit is contained in:
hornet 2024-12-27 21:34:32 +05:00
parent 1b52ceb881
commit e52703fe44

View file

@ -32,7 +32,7 @@ except FileNotFoundError:
# Load the authorized users # Load the authorized users
try: try:
authorized_users = [line.strip() for line in open(auth_users_path, 'r').readlines()] authorized_users = [str(line.strip()) for line in open(auth_users_path, 'r').readlines()]
except FileNotFoundError: except FileNotFoundError:
logging.error('Authorized users file not found. Exiting...') logging.error('Authorized users file not found. Exiting...')
exit(1) exit(1)
@ -72,7 +72,7 @@ def get_service_status(service):
subprocess.run(['sudo', 'systemctl', 'is-active', '--quiet', service], check=True) subprocess.run(['sudo', 'systemctl', 'is-active', '--quiet', service], check=True)
return f'{service} is active' return f'{service} is active'
except subprocess.CalledProcessError: except subprocess.CalledProcessError:
return f'{service} is inactive' return f'{service} is inactive/not present'
# Function to ping a Tailscale node # Function to ping a Tailscale node
def ping_node(node, hostname): def ping_node(node, hostname):
@ -150,10 +150,16 @@ def reboot():
logging.info('Rebooting system...') logging.info('Rebooting system...')
subprocess.run(['sudo', 'reboot'], check=True) subprocess.run(['sudo', 'reboot'], check=True)
# Populate teh variables on first start
get_system_info()
# Message handlers # Message handlers
@bot.message_handler(commands=['start', 'help', 'status', 'restart', 'reboot', 'ping']) @bot.message_handler(commands=['start', 'help', 'status', 'restart', 'reboot', 'ping'])
def handle(message): def handle(message):
user_id = str(message.from_user.id) user_id = str(message.from_user.id)
if user_id not in authorized_users:
bot.reply_to(message, 'You are not authorized for this action')
else:
if message.text == '/start': if message.text == '/start':
bot.reply_to(message, 'lainmonitor v1.0 --- standing by...') bot.reply_to(message, 'lainmonitor v1.0 --- standing by...')
elif message.text == '/help': elif message.text == '/help':
@ -173,17 +179,16 @@ def handle(message):
) )
bot.reply_to(message, status_message) bot.reply_to(message, status_message)
bot.reply_to(message, f'Filesystem info for {hostname}:\n\n{disk}') bot.reply_to(message, f'Filesystem info for {hostname}:\n\n{disk}')
elif message.text == f'/restart {hostname}' and user_id in authorized_users: elif message.text == f'/restart {hostname}':
bot.send_message(message.chat.id, 'Select a service to restart:', reply_markup=restart_menu()) bot.send_message(message.chat.id, 'Select a service to restart:', reply_markup=restart_menu())
elif message.text == f'/reboot {hostname}' and user_id in authorized_users: elif message.text == f'/reboot {hostname}':
bot.reply_to(message, f'Rebooting {hostname}...') bot.reply_to(message, f'Rebooting {hostname}...')
reboot() reboot()
elif message.text == '/ping' and user_id in authorized_users: elif message.text == '/ping':
reach = check_tailscale_nodes() reach = check_tailscale_nodes()
bot.reply_to(message, f'Ping status:\n\n{"\n".join(reach)}') bot.reply_to(message, f'Ping status:\n\n{"\n".join(reach)}')
else: else:
bot.reply_to(message, 'You are not authorized for this action') pass
# Polling with timeout and error handling # Polling with timeout and error handling
try: try:
bot.polling(none_stop=True, timeout=60, long_polling_timeout=60) bot.polling(none_stop=True, timeout=60, long_polling_timeout=60)