Check username (case-insensitive)
case-insensitive Handle Error
This commit is contained in:
@ -123,9 +123,11 @@ def add_user(username: str, traffic_limit: int, expiration_days: int, password:
|
|||||||
exit(1)
|
exit(1)
|
||||||
if not creation_date:
|
if not creation_date:
|
||||||
creation_date = datetime.now().strftime('%Y-%m-%d')
|
creation_date = datetime.now().strftime('%Y-%m-%d')
|
||||||
|
try:
|
||||||
run_cmd(['bash', Command.ADD_USER.value, username, str(traffic_limit), str(expiration_days), password, creation_date])
|
run_cmd(['bash', Command.ADD_USER.value, username, str(traffic_limit), str(expiration_days), password, creation_date])
|
||||||
|
except subprocess.CalledProcessError as e:
|
||||||
|
click.echo(f"{e.output.decode()}", err=True)
|
||||||
|
exit(1)
|
||||||
|
|
||||||
@cli.command('edit-user')
|
@cli.command('edit-user')
|
||||||
@click.option('--username', '-u', required=True, help='Username for the user to edit', type=str)
|
@click.option('--username', '-u', required=True, help='Username for the user to edit', type=str)
|
||||||
|
|||||||
@ -1,11 +1,7 @@
|
|||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
# Source the path.sh script to load the necessary variables
|
|
||||||
source /etc/hysteria/core/scripts/path.sh
|
source /etc/hysteria/core/scripts/path.sh
|
||||||
# source /etc/hysteria/core/scripts/utils.sh
|
|
||||||
# define_colors
|
|
||||||
|
|
||||||
# Function to add a new user to the configuration
|
|
||||||
add_user() {
|
add_user() {
|
||||||
if [ $# -ne 3 ] && [ $# -ne 5 ]; then
|
if [ $# -ne 3 ] && [ $# -ne 5 ]; then
|
||||||
echo "Usage: $0 <username> <traffic_limit_GB> <expiration_days> [password] [creation_date]"
|
echo "Usage: $0 <username> <traffic_limit_GB> <expiration_days> [password] [creation_date]"
|
||||||
@ -18,45 +14,49 @@ add_user() {
|
|||||||
password=$4
|
password=$4
|
||||||
creation_date=$5
|
creation_date=$5
|
||||||
|
|
||||||
|
username_lower=$(echo "$username" | tr '[:upper:]' '[:lower:]')
|
||||||
|
|
||||||
if [ -z "$password" ]; then
|
if [ -z "$password" ]; then
|
||||||
password=$(pwgen -s 32 1)
|
password=$(pwgen -s 32 1)
|
||||||
fi
|
fi
|
||||||
if [ -z "$creation_date" ]; then
|
if [ -z "$creation_date" ]; then
|
||||||
creation_date=$(date +%Y-%m-%d)
|
creation_date=$(date +%Y-%m-%d)
|
||||||
else
|
else
|
||||||
# Validate the date format (YYYY-MM-DD)
|
|
||||||
if ! [[ "$creation_date" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
|
if ! [[ "$creation_date" =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}$ ]]; then
|
||||||
echo "Invalid date format. Expected YYYY-MM-DD."
|
echo "Invalid date format. Expected YYYY-MM-DD."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
# Check if the date is valid
|
|
||||||
if ! date -d "$creation_date" >/dev/null 2>&1; then
|
if ! date -d "$creation_date" >/dev/null 2>&1; then
|
||||||
echo "Invalid date. Please provide a valid date in YYYY-MM-DD format."
|
echo "Invalid date. Please provide a valid date in YYYY-MM-DD format."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Validate the username
|
|
||||||
if ! [[ "$username" =~ ^[a-zA-Z0-9]+$ ]]; then
|
if ! [[ "$username" =~ ^[a-zA-Z0-9]+$ ]]; then
|
||||||
echo -e "${red}Error:${NC} Username can only contain letters and numbers."
|
echo -e "${red}Error:${NC} Username can only contain letters and numbers."
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Convert GB to bytes (1 GB = 1073741824 bytes)
|
|
||||||
traffic=$(echo "$traffic_gb * 1073741824" | bc)
|
traffic=$(echo "$traffic_gb * 1073741824" | bc)
|
||||||
|
|
||||||
if [ ! -f "$USERS_FILE" ]; then
|
if [ ! -f "$USERS_FILE" ]; then
|
||||||
echo "{}" > "$USERS_FILE"
|
echo "{}" > "$USERS_FILE"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
jq --arg username "$username" --arg password "$password" --argjson traffic "$traffic" --argjson expiration_days "$expiration_days" --arg creation_date "$creation_date" \
|
user_exists=$(jq --arg username "$username_lower" '
|
||||||
|
to_entries[] | select(.key | ascii_downcase == $username) | .key' "$USERS_FILE")
|
||||||
|
|
||||||
|
if [ -n "$user_exists" ]; then
|
||||||
|
echo "User already exists."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
jq --arg username "$username_lower" --arg password "$password" --argjson traffic "$traffic" --argjson expiration_days "$expiration_days" --arg creation_date "$creation_date" \
|
||||||
'.[$username] = {password: $password, max_download_bytes: $traffic, expiration_days: $expiration_days, account_creation_date: $creation_date, blocked: false}' \
|
'.[$username] = {password: $password, max_download_bytes: $traffic, expiration_days: $expiration_days, account_creation_date: $creation_date, blocked: false}' \
|
||||||
"$USERS_FILE" > "${USERS_FILE}.temp" && mv "${USERS_FILE}.temp" "$USERS_FILE"
|
"$USERS_FILE" > "${USERS_FILE}.temp" && mv "${USERS_FILE}.temp" "$USERS_FILE"
|
||||||
|
|
||||||
# python3 "$CLI_PATH" restart-hysteria2 > /dev/null 2>&1
|
|
||||||
|
|
||||||
echo -e "User $username added successfully."
|
echo -e "User $username added successfully."
|
||||||
}
|
}
|
||||||
|
|
||||||
# Call the function with the provided arguments
|
|
||||||
add_user "$1" "$2" "$3" "$4" "$5"
|
add_user "$1" "$2" "$3" "$4" "$5"
|
||||||
|
|||||||
@ -55,9 +55,12 @@ def process_add_user_step1(message):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
users = json.loads(result)
|
users = json.loads(result)
|
||||||
|
existing_users = {user.lower() for user in users.keys()}
|
||||||
|
|
||||||
if username in users:
|
if username.lower() in existing_users:
|
||||||
bot.reply_to(message, f"Username '{username}' already exists. Please choose a different username.")
|
bot.reply_to(message, f"Username '{username}' already exists. Please choose a different username.")
|
||||||
|
msg = bot.reply_to(message, "Enter a new username:")
|
||||||
|
bot.register_next_step_handler(msg, process_add_user_step1)
|
||||||
return
|
return
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
if "No such file or directory" in result or result.strip() == "":
|
if "No such file or directory" in result or result.strip() == "":
|
||||||
|
|||||||
Reference in New Issue
Block a user