#!/bin/bash
echo ""
    echo -ne "\e[33mdelete all expired accounts\e[0m"
    for i in {1..2}; do
        for j in ⠋ ⠙ ⠹ ⠸ ⠼ ⠴ ⠦ ⠧ ⠇ ⠏; do
            echo -ne "\r\e[33mdelete all expired accounts $j\e[0m"
            sleep 0.1
        done
    done
    echo -ne "\r\e[33mdelete all expired accounts successful!    \e[0m\n"
    sleep 1
    clear


##----- Auto Remove Vmess
data=($(cat /etc/xray/config.json | grep '^###' | cut -d ' ' -f 2 | sort | uniq))
now=$(date +"%Y-%m-%d")
for user in "${data[@]}"; do
    uuid=$(grep -wE "$user" "/etc/xray/config.json" | cut -d '"' -f 4 | tail -n1 )
    exp=$(grep -w "^### $user" "/etc/xray/config.json" | cut -d ' ' -f 3 | sort | uniq)
    d1=$(date -d "$exp" +%s)
    d2=$(date -d "$now" +%s)
    exp2=$(((d1 - d2) / 86400))
    if [[ "$exp2" -le "0" ]]; then
		echo "### $user $uuid" >> /etc/xray/.userall.db
        sed -i "/^### $user $exp/,/^},{/d" /etc/xray/config.json
        sed -i "/^### $user $exp/,/^},{/d" /etc/vmess/.vmess.db
        rm -rf /etc/vmess/$user
        rm -f /etc/biji/limit/vmess/ip/$user
	    rm -f /etc/limit/vmess/$user
	    rm -f /etc/xray/log-create-${user}.log
        rm -f /etc/xray/${user}-non.json
        rm -f /etc/xray/${user}-tls.json
        rm -f /etc/xray/${user}-grpc.json
    fi
done

#----- Auto Remove Vless
data=($(cat /etc/xray/config.json | grep '^#&' | cut -d ' ' -f 2 | sort | uniq))
now=$(date +"%Y-%m-%d")
for user in "${data[@]}"; do
    uuid=$(grep -wE "$user" "/etc/xray/config.json" | cut -d '"' -f 4 | tail -n1)
    exp=$(grep -w "^#& $user" "/etc/xray/config.json" | cut -d ' ' -f 3 | sort | uniq)
    d1=$(date -d "$exp" +%s)
    d2=$(date -d "$now" +%s)
    exp2=$(((d1 - d2) / 86400))
    if [[ "$exp2" -le "0" ]]; then
		echo "#& $user $uuid" >> /etc/xray/.userall.db
        sed -i "/^#& $user $exp/,/^},{/d" /etc/xray/config.json
        sed -i "/^### $user $exp/,/^},{/d" /etc/vless/.vless.db
        rm -rf /etc/vless/$user
        rm -f /etc/biji/limit/vless/ip/$user
	    rm -f /etc/limit/vless/$user
	    rm -f /etc/xray/log-create-${user}.log
        rm -f /etc/xray/${user}-non.json
        rm -f /etc/xray/${user}-tls.json
        rm -f /etc/xray/${user}-grpc.json
    fi
done

#----- Auto Remove Trojan
data=($(cat /etc/xray/config.json | grep '^#!' | cut -d ' ' -f 2 | sort | uniq))
now=$(date +"%Y-%m-%d")
for user in "${data[@]}"; do
    uuid=$(grep -wE "$user" "/etc/xray/config.json" | cut -d '"' -f 4 | tail -n1)
    exp=$(grep -w "^#! $user" "/etc/xray/config.json" | cut -d ' ' -f 3 | sort | uniq)
    d1=$(date -d "$exp" +%s)
    d2=$(date -d "$now" +%s)
    exp2=$(((d1 - d2) / 86400))
    if [[ "$exp2" -le "0" ]]; then
		echo "#! $user $uuid" >> /etc/xray/.userall.db
        sed -i "/^#! $user $exp/,/^},{/d" /etc/xray/config.json
        sed -i "/^### $user $exp/,/^},{/d" /etc/trojan/.trojan.db
        rm -rf /etc/trojan/$user
        rm -f /etc/biji/trojan/ip/$user
	    rm -f /etc/limit/trojan/$user
	    rm -f /etc/xray/log-create-${user}.log
        rm -f /etc/xray/${user}-non.json
        rm -f /etc/xray/${user}-tls.json
        rm -f /etc/xray/${user}-grpc.json
    fi
done
systemctl restart xray

##----- Auto Remove SSH
data=($(grep '^#ssh#' /etc/ssh/.ssh.db | cut -d ' ' -f 2 | sort | uniq))
now=$(date +"%Y-%m-%d")
for user in "${data[@]}"; do
    exp=$(grep -w "^#ssh# $user" "/etc/ssh/.ssh.db" | cut -d ' ' -f 6- | sort | uniq)
    d1=$(date -d "$exp" +%s)
    d2=$(date -d "$now" +%s)
    exp2=$(((d1 - d2) / 86400))
    
    if [[ "$exp2" -le "0" ]]; then
        rm -f /etc/xray/log-createssh-$user.log
        rm -f /etc/ssh/$user
        rm -f /etc/biji/limit/ssh/ip/${user}
        sed -i "/^#ssh# $user/d" /etc/ssh/.ssh.db
        userdel -f $user 2>/dev/null
    fi
done
systemctl restart sshd