From 21077f046c90a00a3d856e5be50af3d8b2b641eb Mon Sep 17 00:00:00 2001 From: Sarina Date: Tue, 23 Jul 2024 17:29:17 +0330 Subject: [PATCH] Add list users (for test) --- core/scripts/hysteria2/list_users.sh | 6 ++ menu.sh | 123 ++++++++++++++++----------- 2 files changed, 78 insertions(+), 51 deletions(-) diff --git a/core/scripts/hysteria2/list_users.sh b/core/scripts/hysteria2/list_users.sh index e69de29..b3d3608 100644 --- a/core/scripts/hysteria2/list_users.sh +++ b/core/scripts/hysteria2/list_users.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +source /etc/hysteria/core/scripts/path.sh + + +cat "$USER_FILE" \ No newline at end of file diff --git a/menu.sh b/menu.sh index 878fa39..e144587 100644 --- a/menu.sh +++ b/menu.sh @@ -36,56 +36,6 @@ hysteria2_add_user_handler() { python3 $CLI_PATH add-user --username "$username" --traffic-limit "$traffic_limit_GB" --expiration-days "$expiration_days" --password "$password" --creation-date "$creation_date" } -hysteria2_remove_user_handler() { - 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 - python3 $CLI_PATH remove-user --username "$username" -} - -hysteria2_show_user_uri_handler() { - 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 - python3 $CLI_PATH show-user-uri --username "$username" -} - -hysteria2_get_user_handler() { - 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 - python3 $CLI_PATH get-user --username "$username" -} - -hysteria2_change_port_handler() { - while true; do - read -p "Enter the new port number you want to use: " port - if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then - echo "Invalid port number. Please enter a number between 1 and 65535." - else - break - fi - done - python3 $CLI_PATH change-hysteria2-port --port "$port" -} - hysteria2_edit_user() { # Function to prompt for user input with validation prompt_for_input() { @@ -167,6 +117,77 @@ hysteria2_edit_user() { ${blocked:+--blocked} } +hysteria2_remove_user_handler() { + 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 + python3 $CLI_PATH remove-user --username "$username" +} + +hysteria2_get_user_handler() { + 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 + python3 $CLI_PATH get-user --username "$username" +} + +hysteria2_list_users_handler() { + users_json=$(python3 $CLI_PATH list-users) + users_array=($(echo "$users_json" | jq -r '.[] | .username')) + + if [[ ${#users_array[@]} -eq 0 ]]; then + echo -e "\033[0;31mError:\033[0m No users found." + return 1 + fi + + # Print headers + echo -e "\033[0;32mUsers:\033[0m" + printf "%-20s %-15s %-15s %-20s %-30s %-10s\n" "Username" "Traffic Limit (GB)" "Expiration (Days)" "Creation Date" "Password" "Blocked" + + # Print user details + echo "$users_json" | jq -r '.[] | "\(.username) \(.traffic_limit_GB) \(.expiration_days) \(.creation_date) \(.password) \(.blocked)"' | \ + while IFS= read -r line; do + IFS=' ' read -r username traffic_limit expiration_date creation_date password blocked <<< "$line" + printf "%-20s %-15s %-15s %-20s %-30s %-10s\n" "$username" "$traffic_limit" "$expiration_date" "$creation_date" "$password" "$blocked" + done +} + +hysteria2_show_user_uri_handler() { + 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 + python3 $CLI_PATH show-user-uri --username "$username" +} + +hysteria2_change_port_handler() { + while true; do + read -p "Enter the new port number you want to use: " port + if ! [[ "$port" =~ ^[0-9]+$ ]] || [ "$port" -lt 1 ] || [ "$port" -gt 65535 ]; then + echo "Invalid port number. Please enter a number between 1 and 65535." + else + break + fi + done + python3 $CLI_PATH change-hysteria2-port --port "$port" +} + warp_configure_handler() { # Placeholder function, add implementation here if needed echo "empty" @@ -252,7 +273,7 @@ hysteria2_menu() { 3) hysteria2_edit_user ;; 4) hysteria2_remove_user_handler ;; 5) hysteria2_get_user_handler ;; - 6) python3 $CLI_PATH list-users ;; + 6) hysteria2_list_users_handler ;; 7) python3 $CLI_PATH traffic-status ;; 8) hysteria2_show_user_uri_handler ;; 0) return ;;