Refactor: Implement user reset functionality in Python
This commit is contained in:
@ -25,7 +25,7 @@ class Command(Enum):
|
|||||||
GET_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'get_user.sh')
|
GET_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'get_user.sh')
|
||||||
ADD_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'add_user.sh')
|
ADD_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'add_user.sh')
|
||||||
EDIT_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'edit_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')
|
REMOVE_USER = os.path.join(SCRIPT_DIR, 'hysteria2', 'remove_user.py')
|
||||||
SHOW_USER_URI = os.path.join(SCRIPT_DIR, 'hysteria2', 'show_user_uri.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')
|
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.
|
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):
|
def remove_user(username: str):
|
||||||
|
|||||||
58
core/scripts/hysteria2/reset_user.py
Normal file
58
core/scripts/hysteria2/reset_user.py
Normal file
@ -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]} <username>")
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
username_to_reset = sys.argv[1]
|
||||||
|
exit_code = reset_user(username_to_reset)
|
||||||
|
sys.exit(exit_code)
|
||||||
@ -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 <username>"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
reset_user "$1"
|
|
||||||
Reference in New Issue
Block a user