From 40599ecbf97fea70b2b9c813c0cf6825893851cf Mon Sep 17 00:00:00 2001 From: Sarina Date: Sun, 21 Jul 2024 18:17:11 +0330 Subject: [PATCH] Isolate add_user --- core/scripts/hysteria2/add_user.sh | 40 ++++++++++++++++++++++++++++++ menu.sh | 32 ------------------------ 2 files changed, 40 insertions(+), 32 deletions(-) create mode 100644 core/scripts/hysteria2/add_user.sh diff --git a/core/scripts/hysteria2/add_user.sh b/core/scripts/hysteria2/add_user.sh new file mode 100644 index 0000000..36ecf34 --- /dev/null +++ b/core/scripts/hysteria2/add_user.sh @@ -0,0 +1,40 @@ +#!/bin/bash + +# Function to add a new user to the configuration +add_user() { + if [ $# -ne 3 ]; then + echo "Usage: $0 " + exit 1 + fi + + username=$1 + traffic_gb=$2 + expiration_days=$3 + + # Validate the username + if ! [[ "$username" =~ ^[a-z0-9]+$ ]]; then + echo -e "\033[0;31mError:\033[0m Username can only contain lowercase letters and numbers." + exit 1 + fi + + # Convert GB to bytes (1 GB = 1073741824 bytes) + traffic=$((traffic_gb * 1073741824)) + + password=$(pwgen -s 32 1) + creation_date=$(date +%Y-%m-%d) + + if [ ! -f "/etc/hysteria/users/users.json" ]; then + echo "{}" > /etc/hysteria/users/users.json + fi + + jq --arg username "$username" --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}' \ + /etc/hysteria/users/users.json > /etc/hysteria/users/users_temp.json && mv /etc/hysteria/users/users_temp.json /etc/hysteria/users/users.json + + restart_hysteria_service >/dev/null 2>&1 + + echo -e "\033[0;32mUser $username added successfully.\033[0m" +} + +# Call the function with the provided arguments +add_user "$1" "$2" "$3" diff --git a/menu.sh b/menu.sh index 5d583ab..b8fe0b6 100644 --- a/menu.sh +++ b/menu.sh @@ -96,38 +96,6 @@ modify_users() { python3 "$modify_script" } -# Function to add a new user to the configuration -add_user() { - while true; do - read -p "Enter the username: " username - - if [[ "$username" =~ ^[a-z0-9]+$ ]]; then - break - else - echo -e "\033[0;31mError:\033[0m Username can only contain lowercase letters and numbers." - fi - done - - read -p "Enter the traffic limit (in GB): " traffic_gb - # Convert GB to bytes (1 GB = 1073741824 bytes) - traffic=$((traffic_gb * 1073741824)) - - read -p "Enter the expiration days: " expiration_days - password=$(pwgen -s 32 1) - creation_date=$(date +%Y-%m-%d) - - if [ ! -f "/etc/hysteria/users/users.json" ]; then - echo "{}" > /etc/hysteria/users/users.json - fi - - jq --arg username "$username" --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}' \ - /etc/hysteria/users/users.json > /etc/hysteria/users/users_temp.json && mv /etc/hysteria/users/users_temp.json /etc/hysteria/users/users.json - - restart_hysteria_service >/dev/null 2>&1 - - echo -e "\033[0;32mUser $username added successfully.\033[0m" -} # Function to remove a user from the configuration