# !/bin/bash
# ████████╗░█████╗░██╗░░██╗░█████╗░██╗░░░██╗██████╗░███╗░░██╗
# ╚══██╔══╝██╔══██╗██║░██╔╝██╔══██╗██║░░░██║██╔══██╗████╗░██║
# ░░░██║░░░██║░░██║█████═╝░██║░░██║╚██╗░██╔╝██████╔╝██╔██╗██║
# ░░░██║░░░██║░░██║██╔═██╗░██║░░██║░╚████╔╝░██╔═══╝░██║╚████║
# ░░░██║░░░╚█████╔╝██║░╚██╗╚█████╔╝░░╚██╔╝░░██║░░░░░██║░╚███║
# ░░░╚═╝░░░░╚════╝░╚═╝░░╚═╝░╚════╝░░░░╚═╝░░░╚═╝░░░░░╚═╝░░╚══╝

user="$2"
exp="$3"
uuid="$4"

function ssh(){
passwd -u $user > /dev/null 2>&1
systemctl restart ssh >/dev/null 2>&1
systemctl restart sshd >/dev/null 2>&1
}

function vmess(){
exp=$(grep -wE "^### $user" "/etc/xray/config.json" | cut -d ' ' -f 3 | sort | uniq)
uuid=$(grep -w "^### $user $exp" -A 1 "/etc/xray/config.json" | awk -F'"' '/"id":/ {print $4}' | sort | uniq)
uuid1=$(grep -w "^### $user $exp" -A 1 "/etc/xray/config.json" | awk -F'"' '/"id":/ {print $4}' | sed 's/LOCKED//g' | sort | uniq)
uuid2=$(grep -w "^### $user $exp" -A 1 "/etc/xray/config.json" | awk -F'"' '/"id":/ {print $4}' | sed 's/.*\(LOCKED\)/\1/' | sort | uniq)
uuid3=$(grep -w "^### $user" /etc/vmess/.vmess.db | awk '{print $4}')

if [[ "$uuid2" == "LOCKED" ]]; then
    echo > /dev/null   
else
    echo -e "Account has been unlocked!"
    exit 1
fi

DATADB=$(cat /etc/vmess/.vmess.db | grep "^###" | grep -w "${user}" | awk '{print $2}')
if [[ "${DATADB}" != '' ]]; then
sed -i "/^### $user/d" /etc/vmess/.vmess.db
fi
echo "### ${user} ${exp} ${uuid3}" >> /etc/vmess/.vmess.db
sed -i "/^### $user $exp/,/^},{/d" /etc/xray/config.json

sed -i '/#vmess$/a\### '"$user $exp"'\
},{"id": "'""$uuid3""'","alterId": '"0"',"email": "'""$user""'"' /etc/xray/config.json
sed -i '/#vmessgrpc$/a\### '"$user $exp"'\
},{"id": "'""$uuid3""'","alterId": '"0"',"email": "'""$user""'"' /etc/xray/config.json
systemctl restart xray > /dev/null 2>&1
}

function vless(){
exp=$(grep -wE "^#& $user" "/etc/xray/config.json" | cut -d ' ' -f 3 | sort | uniq)
uuid=$(grep -w "^#& $user $exp" -A 1 "/etc/xray/config.json" | awk -F'"' '/"id":/ {print $4}' | sort | uniq)
uuid1=$(grep -w "^#& $user $exp" -A 1 "/etc/xray/config.json" | awk -F'"' '/"id":/ {print $4}' | sed 's/LOCKED//g' | sort | uniq)
uuid2=$(grep -w "^#& $user $exp" -A 1 "/etc/xray/config.json" | awk -F'"' '/"id":/ {print $4}' | sed 's/.*\(LOCKED\)/\1/' | sort | uniq)
uuid3=$(grep -w "^### $user" /etc/vless/.vless.db | awk '{print $4}')

if [[ "$uuid2" == "LOCKED" ]]; then
    echo > /dev/null   
else
    echo -e "Account has been unlocked!"
    exit 1
fi

DATADB=$(cat /etc/vless/.vless.db | grep "^###" | grep -w "${user}" | awk '{print $2}')
if [[ "${DATADB}" != '' ]]; then
sed -i "/^### $user/d" /etc/vless/.vless.db
fi
echo "### ${user} ${exp} ${uuid3}" >> /etc/vless/.vless.db
sed -i "/^#& $user $exp/,/^},{/d" /etc/xray/config.json

sed -i '/#vless$/a\#& '"$user $exp"'\
},{"id": "'""$uuid3""'","email" : "'""$user""'"' /etc/xray/config.json
sed -i '/#vlessgrpc$/a\#& '"$user $exp"'\
},{"id": "'""$uuid3""'","email" : "'""$user""'"' /etc/xray/config.json
systemctl restart xray > /dev/null 2>&1
}

function trojan(){
exp=$(grep -wE "^#! $user" "/etc/xray/config.json" | cut -d ' ' -f 3 | sort | uniq)
uuid=$(grep -w "^#! $user $exp" -A 1 "/etc/xray/config.json" | awk -F'"' '/"password":/ {print $4}' | sort | uniq)
uuid1=$(grep -w "^#! $user $exp" -A 1 "/etc/xray/config.json" | awk -F'"' '/"password":/ {print $4}' | sed 's/LOCKED//g' | sort | uniq)
uuid2=$(grep -w "^#! $user $exp" -A 1 "/etc/xray/config.json" | awk -F'"' '/"password":/ {print $4}' | sed 's/.*\(LOCKED\)/\1/' | sort | uniq)
uuid3=$(grep -w "^### $user" /etc/trojan/.trojan.db | awk '{print $4}')

if [[ "$uuid2" == "LOCKED" ]]; then
    echo > /dev/null   
else
    echo -e "Account has been unlocked!"
    exit 1
fi

DATADB=$(cat /etc/trojan/.trojan.db | grep "^###" | grep -w "${user}" | awk '{print $2}')
if [[ "${DATADB}" != '' ]]; then
sed -i "/^### $user/d" /etc/trojan/.trojan.db
fi
echo "### ${user} ${exp} ${uuid3}" >> /etc/trojan/.trojan.db
sed -i "/^#! $user $exp/,/^},{/d" /etc/xray/config.json

sed -i '/#trojanws$/a\#! '"$user $exp"'\
},{"password": "'""$uuid3""'","email" : "'""$user""'"' /etc/xray/config.json
sed -i '/#trojangrpc$/a\#! '"$user $exp"'\
},{"password": "'""$uuid3""'","email" : "'""$user""'"' /etc/xray/config.json
systemctl restart xray > /dev/null 2>&1
}

function ssws(){
sed -i '/#ssws$/a\##@ '"$user $exp"'\
},{"id": "'""$uuid""'","alterId": '"0"',"email": "'""$user""'"' /etc/xray/config.json
sed -i '/#ssgrpc$/a\##@ '"$user $exp"'\
},{"id": "'""$uuid""'","alterId": '"0"',"email": "'""$user""'"' /etc/xray/config.json

systemctl restart xray > /dev/null 2>&1
}

if [[ ${1} == "vmess" ]]; then
vmess
elif [[ ${1} == "vless" ]]; then
vless
elif [[ ${1} == "trojan" ]]; then
trojan
elif [[ ${1} == "ssh" ]]; then
ssh
elif [[ ${1} == "ssws" ]]; then
ssws
fi