From 50bb7fe7ff7e3751cbca8fba58c1f2ca4d1910cf Mon Sep 17 00:00:00 2001 From: Whispering Wind <151555003+ReturnFI@users.noreply.github.com> Date: Fri, 2 May 2025 11:53:22 +0330 Subject: [PATCH] Refactor: Implement user reset functionality in Python --- core/cli_api.py | 4 +- core/scripts/hysteria2/reset_user.py | 58 ++++++++++++++++++++++++++++ core/scripts/hysteria2/reset_user.sh | 44 --------------------- 3 files changed, 60 insertions(+), 46 deletions(-) create mode 100644 core/scripts/hysteria2/reset_user.py delete mode 100644 core/scripts/hysteria2/reset_user.sh diff --git a/core/cli_api.py b/core/cli_api.py index 14dd1c2..be7e508 100644 --- a/core/cli_api.py +++ b/core/cli_api.py @@ -25,7 +25,7 @@ class Command(Enum): GET_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'get_user.sh') ADD_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'add_user.sh') EDIT_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'edit_user.sh') - RESET_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'reset_user.sh') + RESET_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'reset_user.py') REMOVE_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'remove_user.py') SHOW_USER_URI = os.path.join(SCRIPT_DIR, 'hysteria2', 'show_user_uri.py') WRAPPER_URI = os.path.join(SCRIPT_DIR, 'hysteria2', 'wrapper_uri.py') @@ -296,7 +296,7 @@ def reset_user(username: str): ''' Resets a user's configuration. ''' - run_cmd(['bash', Command.RESET_USER.value, username]) + run_cmd(['python3', Command.RESET_USER.value, username]) def remove_user(username: str): diff --git a/core/scripts/hysteria2/reset_user.py b/core/scripts/hysteria2/reset_user.py new file mode 100644 index 0000000..98598eb --- /dev/null +++ b/core/scripts/hysteria2/reset_user.py @@ -0,0 +1,58 @@ +#!/usr/bin/env python3 + +import json +import sys +import os +from datetime import date +from init_paths import * +from paths import * + +def reset_user(username): + """ + Resets the data usage, status, and creation date of a user in the USERS_FILE. + + Args: + username (str): The username to reset. + + Returns: + int: 0 on success, 1 on failure. + """ + if not os.path.isfile(USERS_FILE): + print(f"Error: File '{USERS_FILE}' not found.") + return 1 + + try: + with open(USERS_FILE, 'r') as f: + users_data = json.load(f) + except json.JSONDecodeError: + print(f"Error: {USERS_FILE} contains invalid JSON.") + return 1 + + if username not in users_data: + print(f"Error: User '{username}' not found in '{USERS_FILE}'.") + return 1 + + today = date.today().strftime("%Y-%m-%d") + users_data[username]['upload_bytes'] = 0 + users_data[username]['download_bytes'] = 0 + users_data[username]['status'] = "Offline" + users_data[username]['account_creation_date'] = today + users_data[username]['blocked'] = False + + try: + with open(USERS_FILE, 'w') as f: + json.dump(users_data, f, indent=4) + print(f"User '{username}' has been reset successfully.") + return 0 + except IOError: + print(f"Error: Failed to reset user '{username}' in '{USERS_FILE}'.") + return 1 + +if __name__ == "__main__": + if len(sys.argv) != 2: + print(f"Usage: {sys.argv[0]} ") + sys.exit(1) + + username_to_reset = sys.argv[1] + exit_code = reset_user(username_to_reset) + sys.exit(exit_code) \ No newline at end of file diff --git a/core/scripts/hysteria2/reset_user.sh b/core/scripts/hysteria2/reset_user.sh deleted file mode 100644 index 0a26b88..0000000 --- a/core/scripts/hysteria2/reset_user.sh +++ /dev/null @@ -1,44 +0,0 @@ -#!/bin/bash - -# Source required scripts -source /etc/hysteria/core/scripts/path.sh - -reset_user() { - local username=$1 - - if [ ! -f "$USERS_FILE" ]; then - echo "Error: File '$USERS_FILE' not found." - return 1 - fi - - user_exists=$(jq -e --arg username "$username" '.[$username]' "$USERS_FILE") - if [ $? -ne 0 ]; then - echo "Error: User '$username' not found in '$USERS_FILE'." - return 1 - fi - - today=$(date +%Y-%m-%d) - jq --arg username "$username" \ - --arg today "$today" \ - ' - .[$username].upload_bytes = 0 | - .[$username].download_bytes = 0 | - .[$username].status = "Offline" | - .[$username].account_creation_date = $today | - .[$username].blocked = false - ' "$USERS_FILE" > tmp.$$.json && mv tmp.$$.json "$USERS_FILE" - - if [ $? -ne 0 ]; then - echo "Error: Failed to reset user '$username' in '$USERS_FILE'." - return 1 - fi - - echo "User '$username' has been reset successfully." -} - -if [ $# -ne 1 ]; then - echo "Usage: $0 " - exit 1 -fi - -reset_user "$1"