Isolate add_user

This commit is contained in:
Sarina
2024-07-21 18:17:11 +03:30
parent 662b701199
commit 40599ecbf9
2 changed files with 40 additions and 32 deletions

32
menu.sh
View File

@ -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