#!/bin/bash

vps_ip=$(wget -qO- ipv4.icanhazip.com)
data_acc=$(awk '/^### / {print $3; exit}' /etc/interflazz/access 2>/dev/null)

if [ -n "$data_acc" ]; then

  expiry_date=$(date -d "$data_acc" +%s)
  current_date=$(date +%s)
  remaining_days=$(( (expiry_date - current_date) / 86400 ))

if [ $expiry_date -gt $current_date ]; then

        # Variables
        green="\e[38;5;82m"
        red="\e[38;5;196m"
        neutral="\e[0m"
        orange="\e[38;5;130m"
        blue="\e[38;5;39m"
        yellow="\e[38;5;226m"
        purple="\e[38;5;141m"
        bold_white="\e[1;37m"
        reset="\e[0m"

        convert_size() {
            local -i bytes=$1
            if [[ $bytes -lt 1024 ]]; then
                echo "${bytes}B"
            elif [[ $bytes -lt 1048576 ]]; then
                echo "$(((bytes + 1023) / 1024))KB"
            elif [[ $bytes -lt 1073741824 ]]; then
                echo "$(((bytes + 1048575) / 1048576))MB"
            else
                echo "$(((bytes + 1073741823) / 1073741824))GB"
            fi
        }
        
# Function to print rainbow text
        print_rainbow() {
            local text="$1"
            local length=${#text}
            local start_color=(0 5 0)
            local mid_color=(0 200 0)
            local end_color=(0 5 0)

            for ((i = 0; i < length; i++)); do
                local progress=$((i * 100 / (length - 1)))

                if [ $progress -lt 50 ]; then
                    local factor=$((progress * 2))
                    r=$(( (start_color[0] * (100 - factor) + mid_color[0] * factor) / 100 ))
                    g=$(( (start_color[1] * (100 - factor) + mid_color[1] * factor) / 100 ))
                    b=$(( (start_color[2] * (100 - factor) + mid_color[2] * factor) / 100 ))
                else
                    local factor=$(((progress - 50) * 2))
                    r=$(( (mid_color[0] * (100 - factor) + end_color[0] * factor) / 100 ))
                    g=$(( (mid_color[1] * (100 - factor) + end_color[1] * factor) / 100 ))
                    b=$(( (mid_color[2] * (100 - factor) + end_color[2] * factor) / 100 ))
                fi

                printf "\e[38;2;%d;%d;%dm%s" "$r" "$g" "$b" "${text:$i:1}"
            done
            echo -e "$reset" # Reset color at the end
        }
        shadowsocks_members() {
            clear
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e "${green}    .:::. SHADOWSOCKS MEMBERS .:::.        ${neutral}"
            echo -e "${orange}─────────────────────────────────────────${neutral}"



    account_count=$(grep -cE "^### " "/etc/xray/shadowsocks/.shadowsocks.db")
    if [ $account_count -eq 0 ]; then
        echo -e "   ${red}No active Shadowsocks accounts.${neutral}"
        echo -e "${orange}─────────────────────────────────────────${neutral}"
        echo -e "   Press enter to return to the menu"
        read
        return
    fi
    echo -e " List of Shadowsocks Accounts:"
    echo -e "${orange}─────────────────────────────────────────${neutral}"
    echo "┌──┬────────────┬──────┬──────┬───┬──────┐"
    echo "│no│ username   │ exp  │quota │ip │usage │"
    echo "├──┼────────────┼──────┼──────┼───┼──────┤"

    grep -E "^### " "/etc/xray/shadowsocks/.shadowsocks.db" | nl -s ' ' | while read -r line; do
        no=$(echo "$line" | awk '{print $1}')
        user=$(echo "$line" | awk '{print $3}')
        exp=$(echo "$line" | awk '{print $4}')
        current_timestamp=$(date +%s)
        exp_timestamp=$(date -d "$exp" +%s)
        days_left=$(( (exp_timestamp - current_timestamp) / 86400 ))
        if [ $days_left -lt 0 ]; then
            days_left=0
        fi
        exp="${days_left}d"

        quota_file="/etc/xray/shadowsocks/${user}"
        ip_limit_file="/etc/xray/shadowsocks/${user}IP"
        usage_file="/etc/xray/shadowsocks/usage/${user}"

        quota="Unltd"
        if [ -f "$quota_file" ]; then
            quota=$(cat "$quota_file")
            if [ "$quota" != "" ] && [ "$quota" != "0" ]; then
                quota=$(convert_size "$quota")
            else
                quota="Unltd"
            fi
        fi

        ip_limit="Unltd"
        if [ -f "$ip_limit_file" ]; then
            ip_limit=$(cat "$ip_limit_file")
            if [ "$ip_limit" = "" ] || [ "$ip_limit" = "0" ]; then
                ip_limit="Unltd"
            fi
        fi

        usage="0GB"
        if [ -f "$usage_file" ]; then
            usage=$(cat "$usage_file")
            if [ "$usage" != "" ] && [ "$usage" != "0" ]; then
                usage=$(convert_size "$usage")
            else
                usage="0GB"
            fi
        fi

        printf "│%-2s│%-12s│%-6s│%-6s│%-3s│%-6s│\n" "$no" "$user" "$exp" "$quota" "$ip_limit" "$usage"
    done
    echo "└──┴────────────┴──────┴──────┴───┴──────┘"

    echo -e "   Press enter to return to menu"
    read

}

        change_shadowsocks_limit() {
            clear
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e "${green}  .:::. CHANGE SHADOWSOCKS LIMIT .:::.${neutral}"
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e " List of Shadowsocks Accounts:"
            echo -e "${orange}─────────────────────────────────────────${neutral}"
    if [ ! -e "/etc/xray/shadowsocks/.shadowsocks.db" ]; then
        echo -e "   ${red}No Shadowsocks accounts registered.${neutral}"
        echo -e "${orange}─────────────────────────────────────────${neutral}"
        echo -e "   Press enter to return to menu"
        read
        return
    fi

    account_count=$(grep -cE "^### " "/etc/xray/vmess/.vmess.db")
    if [ $account_count -eq 0 ]; then
        echo -e "   ${red}No active VMESS accounts.${neutral}"
        echo -e "${orange}─────────────────────────────────────────${neutral}"
        echo -e "   Press enter to return to the menu"
        read
        return
    fi
    echo "┌────┬────────────────────┬─────────────┐"
    echo "│ no │ username           │     exp     │"
    echo "├────┼────────────────────┼─────────────┤"
    grep -E "^### " "/etc/xray/shadowsocks/.shadowsocks.db" | awk '{
        cmd = "date -d \"" $3 "\" +%s"
        cmd | getline exp_timestamp
        close(cmd)
        current_timestamp = systime()
        days_left = int((exp_timestamp - current_timestamp) / 86400)
        if (days_left < 0) days_left = 0
        printf "│ %-2d │ %-18s │ %-11s │\n", NR, $2, days_left " days"
    }'
    echo "└────┴────────────────────┴─────────────┘"

    echo -e "${orange}─────────────────────────────────────────${neutral}"
    read -p "   Select the account number : " account_number
    
    # Check if input is a number
    if ! [[ "$account_number" =~ ^[0-9]+$ ]]; then
        echo -e "   ${red}Error: Input must be a number.${neutral}"
        echo -e "   Press enter to return to the menu"
        read
        return
    fi
    
    # Check if account number is valid
    if [ "$account_number" -lt 1 ] || [ "$account_number" -gt "$account_count" ]; then
        echo -e "   ${red}Error: Invalid account number.${neutral}"
        echo -e "   Press enter to return to the menu"
        read
        return
    fi

    username=$(grep -E "^### " "/etc/xray/shadowsocks/.shadowsocks.db" | cut -d ' ' -f 2 | sed -n "${account_number}p")

    if [ -z "$username" ]; then
        echo -e "   ${red}Invalid account number.${neutral}"
        echo -e "   Press enter to return to menu"
        read
        return
    fi

    echo -e "Changing limit for account: ${green}$username${neutral}"
    read -p "Enter new quota limit (in GB, 0 for unlimited): " new_quota
    read -p "Enter new IP limit (0 for unlimited): " new_ip_limit

    # Validate input
    if ! [[ "$new_quota" =~ ^[0-9]+$ ]]; then
        echo -e "${red}Error: Quota limit must be a number.${neutral}"
        echo -e "   Press enter to return to the menu"
        read
        return
    fi

    if ! [[ "$new_ip_limit" =~ ^[0-9]+$ ]]; then
        echo -e "${red}Error: IP limit must be a number.${neutral}"
        echo -e "   Press enter to return to the menu"
        read
        return
    fi

    # Convert quota to bytes
    new_quota_bytes=$((new_quota * 1024 * 1024 * 1024))

    # Update quota and IP limit files
    echo "${new_quota_bytes}" >"/etc/xray/shadowsocks/${username}"
    echo "${new_ip_limit}" >"/etc/xray/shadowsocks/${username}IP"

    # Check if update was successful
    if [ $? -eq 0 ]; then
        echo -e "${green}Limit for account $username successfully changed.${neutral}"
        echo -e "New quota: ${new_quota} GB"
        echo -e "New IP limit: ${new_ip_limit}"
    else
        echo -e "${red}Failed to change limit for account $username.${neutral}"
        echo -e "Please ensure you have sufficient permissions and try again."
    fi

    echo -e "${orange}─────────────────────────────────────────${neutral}"
    echo -e "   Press enter to return to menu"
    read

        }
        shadowsocks_details() {
            clear
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e "${green}     .:::. SHADOWSOCKS DETAIL .:::.     ${neutral}"
            echo -e "${orange}─────────────────────────────────────────${neutral}"

            if [ ! -e "/etc/xray/shadowsocks/.shadowsocks.db" ]; then
                echo -e "   ${red}No Shadowsocks accounts registered.${neutral}"
                echo -e "${orange}─────────────────────────────────────────${neutral}"
                echo -e "   Press enter to return to menu"
                read
                return
            fi

            echo -e " List of Shadowsocks Accounts:"
            echo -e "${orange}─────────────────────────────────────────${neutral}"

            while IFS= read -r line; do
                user=$(echo $line | cut -d ' ' -f 2 | sort -u)
                exp=$(echo $line | cut -d ' ' -f 3 | sort -u)
                password=$(echo $line | cut -d ' ' -f 4 | sort -u)

                echo -e "   Username      : ${green}$user${neutral}"
                echo -e "   Expired Date  : ${green}$exp${neutral}"
                echo -e "   UUID          : ${green}$uuid${neutral}"

                if [ -f "/etc/xray/shadowsocks/${user}" ]; then
                    quota=$(cat /etc/xray/shadowsocks/${user})
                    if [ -n "$quota" ] && [ "$quota" != "0" ]; then
                        quota_readable=$(convert_size $quota)
                        echo -e "   Quota         : ${green}$quota_readable${neutral}"
                    else
                        echo -e "   Quota         : ${green}Unlimited${neutral}"
                    fi
                else
                    echo -e "   Quota         : ${green}Unlimited${neutral}"
                fi

                if [ -f "/etc/xray/shadowsocks/${user}IP" ]; then
                    ip_limit=$(cat /etc/xray/shadowsocks/${user}IP)
                    if [ -n "$ip_limit" ] && [ "$ip_limit" != "0" ]; then
                        echo -e "   IP Limit      : ${green}$ip_limit${neutral}"
                    else
                        echo -e "   IP Limit      : ${green}Unlimited${neutral}"
                    fi
                else
                    echo -e "   IP Limit      : ${green}Unlimited${neutral}"
                fi

                echo -e "${orange}─────────────────────────────────────────${neutral}"
            done <"/etc/xray/shadowsocks/.shadowsocks.db"

            echo -e "    Press enter to return to menu"
            read
        }
custom_uuid_shadowsocks() {
    clear
    echo -e "${orange}─────────────────────────────────────────${neutral}"
    echo -e "  ${green}.:::. CUSTOM SHADOWSOCKS UUID .:::.${neutral}"
    echo -e "${orange}─────────────────────────────────────────${neutral}"
    if [ ! -e "/etc/xray/shadowsocks/.shadowsocks.db" ]; then
        echo -e "   ${red}No Shadowsocks accounts registered.${neutral}"
        echo -e "${orange}─────────────────────────────────────────${neutral}"
        echo -e "   Press enter to return to the menu"
        read
        return
    fi

    account_count=$(grep -cE "^### " "/etc/xray/shadowsocks/.shadowsocks.db")
    if [ $account_count -eq 0 ]; then
        echo -e "   ${red}No active Shadowsocks accounts.${neutral}"
        echo -e "${orange}─────────────────────────────────────────${neutral}"
        echo -e "   Press enter to return to the menu"
        read
        return
    fi
    echo -e " List of Shadowsocks Accounts:"
    echo -e "${orange}─────────────────────────────────────────${neutral}"
    echo "┌────┬────────────────────┬─────────────┐"
    echo "│ no │ username           │     exp     │"
    echo "├────┼────────────────────┼─────────────┤"
    grep -E "^### " "/etc/xray/shadowsocks/.shadowsocks.db" | awk '{
        cmd = "date -d \"" $3 "\" +%s"
        cmd | getline exp_timestamp
        close(cmd)
        current_timestamp = systime()
        days_left = int((exp_timestamp - current_timestamp) / 86400)
        if (days_left < 0) days_left = 0
        printf "│ %-2d │ %-18s │ %-11s │\n", NR, $2, days_left " days"
    }'
    echo "└────┴────────────────────┴─────────────┘"

    echo -e "${orange}─────────────────────────────────────────${neutral}"
    read -p "   Select the account number : " account_number
    
    # Check if input is a number
    if ! [[ "$account_number" =~ ^[0-9]+$ ]]; then
        echo -e "   ${red}Error: Input must be a number.${neutral}"
        echo -e "   Press enter to return to the menu"
        read
        return
    fi
    
    # Check if account number is valid
    if [ "$account_number" -lt 1 ] || [ "$account_number" -gt "$account_count" ]; then
        echo -e "   ${red}Error: Invalid account number.${neutral}"
        echo -e "   Press enter to return to the menu"
        read
        return
    fi

    user=$(grep -E "^### " "/etc/xray/shadowsocks/.shadowsocks.db" | cut -d ' ' -f 2 | sed -n "${account_number}p")

    if [ -z "$user" ]; then
        echo -e "   ${red}Invalid account number.${neutral}"
        echo -e "   Press enter to return to menu"
        read
        return
    fi
    while true; do
        read -p "   UUID (min 6 characters): " new_uuid
        if [[ ${#new_uuid} -ge 6 && "$new_uuid" =~ ^[a-zA-Z0-9_-]+$ ]]; then
            break
        else
            printf "\033[1A\033[0J"
            echo -e "${red}   UUID must be at least 6 characters${neutral}"
        fi
    done

    if [ -z "$new_uuid" ]; then
        echo -e "${red}Error: UUID cannot be empty.${neutral}"
        echo -e "   Press enter to return to menu"
        read
        return
    fi

    # Update UUID in config.json
    user_data=$(cat /etc/xray/shadowsocks/.shadowsocks.db | grep "^### $user")
    exp=$(echo "$user_data" | awk '{print $3}')
    config_file="/etc/xray/shadowsocks/config.json"
    db_file="/etc/xray/shadowsocks/.shadowsocks.db"
    method="aes-256-gcm"
    sed -i "/^### $user $exp/,/^},{/d" "$config_file"

    # Add configuration back to config_file

    sed -i '/#ssws$/a\### '"$user $exp"'\
    },{"password": "'"$new_uuid"'","method": "'"$method"'","email": "'"$user"'"}' $config_file

    sed -i '/#ssgrpc$/a\### '"$user $exp"'\
    },{"password": "'"$new_uuid"'","method": "'"$method"'","email": "'"$user"'"}' $config_file

    sed -i "/^### $user/d" "$db_file"
    echo "### $user $exp $new_uuid" >> "$db_file"
    systemctl restart shadowsocks@config

    if [ $? -eq 0 ]; then
        echo -e "UUID for account $user successfully changed.${neutral}"
    else
        echo -e "UUID for account ${green}$user${neutral} successfully changed."
        echo -e "Please ensure you have sufficient permissions and try again."
    fi

    echo -e "${orange}─────────────────────────────────────────${neutral}"
    echo -e "   Press enter to return to menu"
    read
}
        # Function to display Shadowsocks menu
        display_shadowsocks_menu() {
            clear
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e "${green}     .:::. SHADOWSOCKS MANAGER .:::.        ${neutral}"
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            echo -e "   ${green}1.${neutral} Create Shadowsocks Account"
            echo -e "   ${green}2.${neutral} Delete Shadowsocks Account"
            echo -e "   ${green}3.${neutral} Renew Shadowsocks Account"
            echo -e "   ${green}4.${neutral} Check Shadowsocks Login Account"
            echo -e "   ${green}5.${neutral} List Shadowsocks Members"
            echo -e "   ${green}6.${neutral} Change Shadowsocks Account Limit"
            echo -e "   ${green}7.${neutral} View Shadowsocks Account Details"
            echo -e "   ${green}8.${neutral} Create Trial Shadowsocks Account"
            echo -e "   ${green}9.${neutral} Custom Shadowsocks UUID"
            echo -e "   ${green}x.${neutral} Exit Shadowsocks Manager"
            echo -e "${orange}─────────────────────────────────────────${neutral}"
            print_rainbow "─────────────────────────────────────────"
            echo -e "          ${orange}───${green}───${yellow}───${blue}───${purple}───${red}───${neutral}"
            echo -e ""
            echo -e ""
            read -p " Enter your choice (1-9) or type 'x' to quit: " shadowsocks_choice

            case $shadowsocks_choice in
            1) addshadowsocks ;;
            2) dellshadowsocks ;;
            3) renewshadowsocks ;;
            4) checkshadowsocks ;;
            5) shadowsocks_members ;;
            6) change_shadowsocks_limit ;;
            7) shadowsocks_details ;;
            8) trialshadowsocks ;;
            9) custom_uuid_shadowsocks ;;
            x | X)
                echo -e ""
                echo -e "   ${green}Thank you for using our service.${neutral}"
                exit 0
                ;;
            *)
                echo -e "   ${red}Invalid choice. Please try again.${neutral}"
                sleep 2
                display_shadowsocks_menu
                ;;
            esac
        }

        # Call function to display menu
        display_shadowsocks_menu
    else
        echo -e "\e[38;5;130m────────────────────────────────────────────\033[0m"
        echo -e "\e[38;5;82m             AUTOSCRIPT INTERFLAZZ ID             \033[0m"
        echo -e "\e[38;5;130m────────────────────────────────────────────\033[0m"
        echo -e ""
        echo -e "            \033[31mPERMISSION DENIED !\033[0m"
        echo -e "   \033[0;33mYour VPS\033[0m $(wget -qO- ipv4.icanhazip.com) \033[0;33mHas been Banned\033[0m"
        echo -e "     \033[0;33mBuy access permissions for scripts\033[0m"
        echo -e "             \033[0;33mContact Admin :\033[0m"
        echo -e "      \033[0;32mWhatsApp\033[0m wa.me/+6285196156105"
        echo -e "      \033[0;96mTelegram\033[0m t.me/interflazzid"
        echo -e "\e[38;5;130m────────────────────────────────────────────\033[0m"
        exit
    fi
else
    echo -e "\e[38;5;130m────────────────────────────────────────────\033[0m"
    echo -e "\e[38;5;82m             AUTOSCRIPT INTERFLAZZ ID             \033[0m"
    echo -e "\e[38;5;130m────────────────────────────────────────────\033[0m"
    echo -e ""
    echo -e "            \033[31mPERMISSION DENIED !\033[0m"
    echo -e "   \033[0;33mYour VPS\033[0m $(wget -qO- ipv4.icanhazip.com) \033[0;33mHas been Banned\033[0m"
    echo -e "     \033[0;33mBuy access permissions for scripts\033[0m"
    echo -e "             \033[0;33mContact Admin :\033[0m"
    echo -e "      \033[0;32mWhatsApp\033[0m wa.me/+6285196156105"
    echo -e "      \033[0;96mTelegram\033[0m t.me/interflazzid"
    echo -e "\e[38;5;130m────────────────────────────────────────────\033[0m"
    exit
fi