#!/bin/bash
function send-log(){
CHATID=$(grep -E "^#bot# " "/etc/bot/.bot.db" | cut -d ' ' -f 3)
KEY=$(grep -E "^#bot# " "/etc/bot/.bot.db" | cut -d ' ' -f 2)
TIME="10"
URL="https://api.telegram.org/bot$KEY/sendMessage"
TEXT="
<code>────────────────────</code>
<b>⚠️NOTIF QUOTA HABIS⚠️</b>
<code>────────────────────</code>
<code>Username  : </code><code>$user</code>
<code>Usage     : </code><code>$total</code>
<code>────────────────────</code>
"
curl -s --max-time $TIME -d "chat_id=$CHATID&disable_web_page_preview=1&text=$TEXT&parse_mode=html" $URL >/dev/null
}

function con() {
    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
}

  while true; do
  sleep 30
  data=($(cat /etc/shadowsocks/.shadowsocks.db | grep '^###' | cut -d ' ' -f 2 | sort | uniq))
  if [[ ! -e /etc/limit/shadowsocks ]]; then
  mkdir -p /etc/limit/shadowsocks
  fi
  for user in ${data[@]}
  do
  xray api stats --server=127.0.0.1:10000 -name "user>>>${user}>>>traffic>>>downlink" >& /tmp/${user}
  getThis=$(cat /tmp/${user} | awk '{print $1}');
  if [[ ${getThis} != "failed" ]]; then
        downlink=$(xray api stats --server=127.0.0.1:10000 -name "user>>>${user}>>>traffic>>>downlink" | grep -w "value" | awk '{print $2}' | cut -d '"' -f2);
        if [ -e /etc/limit/shadowsocks/${user} ]; then
        plus2=$(cat /etc/limit/shadowsocks/${user});
        if [[ ${#plus2} -gt 0 ]]; then
        plus3=$(( ${downlink} + ${plus2} ));
        echo "${plus3}" > /etc/limit/shadowsocks/"${user}"
        xray api stats --server=127.0.0.1:10000 -name "user>>>${user}>>>traffic>>>downlink" -reset > /dev/null 2>&1
        else
        echo "${downlink}" > /etc/limit/shadowsocks/"${user}"
        xray api stats --server=127.0.0.1:10000 -name "user>>>${user}>>>traffic>>>downlink" -reset > /dev/null 2>&1
        fi
        else
        echo "${downlink}" > /etc/limit/shadowsocks/"${user}"
        xray api stats --server=127.0.0.1:10000 -name "user>>>${user}>>>traffic>>>downlink" -reset > /dev/null 2>&1
        fi
        else
      echo ""
   fi
done
# Check ur Account
for user in ${data[@]}
  do
    if [ -e /etc/shadowsocks/${user} ]; then
      checkLimit=$(cat /etc/shadowsocks/${user});
      if [[ ${#checkLimit} -gt 1 ]]; then
      if [ -e /etc/limit/shadowsocks/${user} ]; then
      Usage=$(cat /etc/limit/shadowsocks/${user});
      if [[ ${Usage} -gt ${checkLimit} ]]; then
      exp=($(cat /etc/shadowsocks/.shadowsocks.db | grep '^###' | cut -d ' ' -f 2 | sort | uniq))
      sed -i "/^#!# $user $exp/,/^},{/d" /etc/xray/config.json
      sed -i "/^### $user $exp/d" /etc/shadowsocks/.shadowsocks.db
      bol=$(cat /etc/limit/shadowsocks/${user});
      total=$(con ${bol})
      send-log
        rm -f /etc/shadowsocks/$user
        rm -f /etc/funny/limit/shadowsocks/ip/${user}
        rm -f /etc/limit/shadowsocks/$user
        rm -f /etc/limit/shadowsocks/quota/$user
        rm -f /var/www/html/sodosokws-$user.txt
      systemctl restart xray >> /dev/null 2>&1
      else
      echo ""
      fi
      else
      echo ""
      fi
      else
      echo ""
      fi
      else
      echo ""
    fi
  done
done
