#!/bin/bash

clear
unset readvalue
[[ ! -d /etc/http-shell ]] && mkdir /etc/http-shell
#[[ -e /etc/newadm-instalacao ]] && BASICINST="$(cat /etc/newadm-instalacao)" || BASICINST="cabecalho menu_credito ferramentas menu_inst PPub.py usercodes payloads ssl paysnd.sh verifica PDirect.py v-local.log PGet.py ultrahost menu POpen.py shadowsocks.sh fai2ban PPriv.py"
IVAR="/etc/http-instas"
source <(curl -sSL https://gitea.com/drowkid01/dpbt/raw/branch/main/Otros/msg) >/dev/null

SCPT_DIR="/etc/SCRIPT"
[[ ! -e ${SCPT_DIR} ]] && mkdir ${SCPT_DIR}
[[ ! -e ${SCPT_DIR} ]] && bash /bin/ejecutar/echo-ram.sh
DIR="/etc/http-shell"
LIST="-SPVweN"
list_fix() {

    rm ${SCPT_DIR}/*.x.c &>/dev/null
    unset KEY
    KEY="$1"
    name="$2"
    #CRIA DIR
    [[ ! -e ${DIR} ]] && mkdir ${DIR}
    #ENVIA ARQS
    i=0
    VALUE+="gerar.sh http-server.py $BASICINST"
    for arqx in $(ls ${SCPT_DIR}); do
        [[ $(echo $VALUE | grep -w "${arqx}") ]] && continue
        echo -e "[$i] -> ${arqx}"
        arq_list[$i]="${arqx}"
        let i++
    done
    msg -bar
    #read -p " OPCION : " readvalue
    #CRIA KEY
    [[ ! -e ${DIR}/${KEY} ]] && mkdir ${DIR}/${KEY}
    #PASSA ARQS
    [[ -z $readvalue ]] && readvalue="1"
    [[ -z $nombrevalue ]] && nombrevalue="$nomkey$name"
    if [[ $readvalue = @(cgh|1) ]]; then
        #ADM BASIC
        echo -e "  -  KEY SCRIPT BASE ADM  -  "
        arqslist="$BASICINST"
        for arqx in $(echo "${arqslist}"); do
            [[ -e ${DIR}/${KEY}/$arqx ]] && continue #ANULA ARQUIVO CASO EXISTA
            cp ${SCPT_DIR}/$arqx ${DIR}/${KEY}/
            echo "$arqx" >>${DIR}/${KEY}/${LIST}
        done
    else
        for arqx in $(echo "${readvalue}"); do
            #UNE ARQ
            [[ -e ${DIR}/${KEY}/${arq_list[$arqx]} ]] && continue #ANULA ARQUIVO CASO EXISTA
            rm ${SCPT_DIR}/*.x.c &>/dev/null
            cp ${SCPT_DIR}/${arq_list[$arqx]} ${DIR}/${KEY}/
            echo "${arq_list[$arqx]}" >>${DIR}/${KEY}/${LIST}
        done
        echo "TRUE" >>${DIR}/${KEY}/FERRAMENTA
    fi
    rm ${SCPT_DIR}/*.x.c &>/dev/null
    echo "$nombrevalue" >${DIR}/${KEY}.name
    [[ ! -z $IPFIX ]] && echo "$IPFIX" >${DIR}/${KEY}/keyfixa
    echo -e "-------------------------------------------------"
}

ofus() {
    unset txtofus
    number=$(expr length $1)
    for ((i = 1; i < $number + 1; i++)); do
        txt[$i]=$(echo "$1" | cut -b $i)
        case ${txt[$i]} in
        ".") txt[$i]="x" ;;
        "x") txt[$i]="." ;;
        "5") txt[$i]="s" ;;
        "s") txt[$i]="5" ;;
        "1") txt[$i]="@" ;;
        "@") txt[$i]="1" ;;
        "2") txt[$i]="?" ;;
        "?") txt[$i]="2" ;;
        "4") txt[$i]="0" ;;
        "0") txt[$i]="4" ;;
        "/") txt[$i]="K" ;;
        "K") txt[$i]="/" ;;
        esac
        txtofus+="${txt[$i]}"
    done
    echo "$txtofus" | rev
}

fix_key() {
    unset nomkey
    echo " Bienvenido, Porfavor dijita el Nombre del DUE�O de la KEYs"
    echo "             DIJITA  0  PARA REGRESAR "
    read -p " RESELLER deL Keys : " nomkey
    [[ $nomkey = 0 ]] && return
    [[ -z $nomkey ]] && {
        [[ -e /etc/menu_ito ]] && nomkey="$(cat /etc/menu_ito)" || nomkey="$(curl -sSL "https://www.dropbox.com/s/z38hj1mz3q9krso/menu_credito")"
    }
    msg -bar
    echo " Bienvenido, Porfavor ingresa el numero de keys a generar"
    read -p "Numero de Keys : " numk
    [[ -z ${numk} ]] && numk=0
    msg -bar
    read -p "INGRESA NOMBRE DE VENDEDOR : " vkey
    clear
    msg -bar
    echo -e "$numk Keys activas,de $nomkey y esperando instalaci�n!"
    echo "$nomkey" >${SCPT_DIR}/menu_credito
    for ((w = 0; w < $numk; w++)); do
        valuekey="$(date | md5sum | head -c11)"
        valuekey+="$(echo $(($RANDOM * 10)) | head -c 6)"
        xyz+="$(echo $(($RANDOM * 3)) | head -c 4)"
        #valuekey+="$(echo $(($RANDOM*10))|head -c 5)"
        list_fix "$valuekey" "$(($w + 1))"
        keyfinal=$(ofus "$(wget -qO- ipv4.icanhazip.com):8888/$valuekey/$LIST")
        echo -e " ${CHeko} Key $(($w + 1)) Exitosa! $(printf '%(%D-%H:%M:%S)T')  \n"
        echo -e " ${TTcent}  ${keyfinal}  ${TTcent}" | pv -qL 80
    done
    [[ -e /etc/menu_ito ]] && cat /etc/menu_ito >${SCPT_DIR}/menu_credito || echo -e "\nCreditos Aplicados Exitosamente"
    #$(cat < /etc/ADM-db/resell)
    msg -bar
    echo -e " ${ScT}  *INSTALADOR UNIVERSAL*   ${ScT}"
    msg -bar && echo -ne "$(msg -verd "apt update -y &&") $(msg -aqua "apt upgrade -y &&\n") " && msg -bra "\033[7;49;35m wget -q https://raw.githubusercontent.com/ChumoGH/ScriptCGH/main/setup && chmod 777 setup && ./setup --install"
    msg -bar
    [[ -e /etc/menu_numito ]] && menumito="$(cat /etc/menu_numito)" || menumito="https://t.me/ChumoGH_bot"
    [[ -z $vkey ]] && vkey="@drowkid01"
    echo -e " ${ceLL} Soporte : Ubuntu 14.04 - 16.04 - 18.04 - 20.04 - 21.04 - 22.04\n         S.O PREFERENTE 18.04"
    echo -e " Verificada: $vkey ${ScT} RESELLER ACTUAL : $nomkey" | pv -qL 80
    msg -bar
    read -p "Enter para finalizar"
}



start_gen() {
    unset bot_ini
    PIDGEN=$(ps x | grep -v grep | grep "http-server.sh")
    if [[ ! $PIDGEN ]]; then
        msg -bar
        echo -ne "\033[1;97m Poner en linea despues de un reinicio [s/n]: "
        read bot_ini
        msg -bar
        [[ $bot_ini = @(s|S|y|Y) ]] && {
            crontab -l >/root/cron
            echo "@reboot screen -dmS generador /bin/http-server.sh -start" >>/root/cron
            crontab /root/cron
            rm /root/cron
            echo '#!/bin/bash
killall http-server.sh
screen -dmS generador /bin/http-server.sh -start
echo  #' >/bin/genon
            chmod +rwx /bin/genon
            echo "00 * * * * root bash /bin/genon" >>/etc/crontab
            service cron restart
        } || {
            crontab -l >/root/cron
            sed -i '/http-server.sh/ d' /root/cron
            crontab /root/cron
            rm /root/cron
            sed '/genon/ d' /etc/crontab >/bin/ejecutar/crontab
            cat /bin/ejecutar/crontab >/etc/crontab
            service cron restart

        }
        genon
    else
        killall http-server.sh
    fi
}
message_gen() {
    read -p "Ingresa el Mensaje: " MSGNEW
    [[ -z $MSGNEW ]] && return
    echo $MSGNEW >/etc/menu_ito
    cat /etc/menu_ito >${SCPT_DIR}/menu_credito
    read -p "Ingresa tu Numero de Contacto o tu ALIAS de TELEGRAM: " MSGNEW
    echo $MSGNEW >/etc/menu_numito && chmod +rwx /etc/menu_numito
    msg -bar
}

act_gen() {
    while [[ ${varread} != @([0-2]) ]]; do
        echo -e "Bienvenido al Actualizador, Escoje que vas a Actualizar\n 1).- KEY ACTIVA\n 2).- Creditos del ADM\n " | lolcat
        echo -ne "${cor[6]}"
        read -p " Escoje :" varread
    done
    msg -bar
    if [[ ${varread} = 0 ]]; then
        return
    elif [[ ${varread} = 1 ]]; then
        att_gen_key
    elif [[ ${varread} = 2 ]]; then
        message_gen
    fi

    #source <(curl -sL https://raw.githubusercontent.com/ChumoGH/ChuKK-SCRIPT/master/Key-gerar/insta.sh)
}

rmv_iplib() {
    echo -e "SERVIDORES DE KEY ATIVOS!"
    rm /var/www/html/newlib && touch /var/www/html/newlib
    rm ${SCPT_DIR}/*.x.c &>/dev/null
    [[ -z $(ls $DIR | grep -v "ERROR-KEY") ]] && return
    for arqs in $(ls $DIR | grep -v "ERROR-KEY" | grep -v ".name"); do
        if [[ $(cat ${DIR}/${arqs}.name | grep GERADOR) ]]; then
            var=$(cat ${DIR}/${arqs}.name)
            ip=$(cat ${DIR}/${arqs}/keyfixa)
            # echo -ne "\033[1;31m[USUARIO]:(\033[1;32m${var%%[*}\033[1;31m) \033[1;33m[GERADOR]:\033[1;32m ($ip)\033[0m"
            echo "$ip" >>/var/www/html/newlib && echo -e " \033[1;36m[ATUALIZADO]"
        fi
    done
    echo "51.222.29.216" >>/var/www/html/newlib
    msg -bar
    read -p "Enter"
}

fum_ver() {
    while [[ ! $Keey ]]; do
        clear
        export PATH=$PATH:/usr/sbin:/usr/local/sbin:/usr/local/bin:/usr/bin:/sbin:/bin:/usr/games
        echo -e "\n      \033[1;32m DIGITA TU KEY A VERIFICAR "
        msg -ne "Script Key: " && read Keey
        [[ ! -z $Keey ]] && Keey="$(echo "$Keey" | tr -d '[[:space:]]')"
        tput cuu1 && tput dl1
    done
    REQUEST=$(ofus "$Keey" | cut -d'/' -f2)
    echo -e "\n"
    echo -e " FILE Contend : ${REQUEST} $(echo ${REQUEST} | wc -c)"
    echo -e "\n"
    echo -e " VERIFICA, Si tu key Contiene \033[1;45m KEY DE ChumoGH! \033[0m "
    echo -e "\n"
    msg -ne " Link Key: http://$(ofus $Keey) \n                      "
    IiP=$(ofus "$Keey" | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}')
    [[ $(curl -s --connect-timeout 2 $IiP:8888) ]] && echo -e "\033[1;42mCONEXION CON SERVIDOR EXITOSA\033[0m" || echo -e "\033[1;43mCONEXION CON SERVIDOR FALLIDA\033[0m"
    wget --no-check-certificate -O $HOME/list-key $(ofus $Keey)/$(wget -qO- ipv4.icanhazip.com) >/dev/null 2>&1 && echo -ne "\033[1;32m  [ VERIFICANDO ]" || echo -e "\033[1;31m [ No Existe Acceso al KEY ]" #&& echo -e "\033[1;32m [ Key  ]\n" || echo -e "\033[1;31m [ No Existe Acceso al KEY ]"
    ofen=$(wget -qO- $(ofus $Keey))
    unset arqx
    [[ -d $HOME/install ]] && rm -rf $HOME/install/* || mkdir $HOME/install
    verificar_arq() {
        echo "$1" >>$HOME/install/log.txt
    }
    n=1
    IP=$(ofus "$Keey" | grep -vE '127\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}' | grep -o -E '[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}') && echo "$IP" >/usr/bin/vendor_code
    pontos="."
    stopping=" COMPROBANDO " | sed -e 's/[^a-z -]//ig'
    for arqx in $(cat $HOME/list-key); do
        msg -verm "${stopping}${pontos}" && sleep 0.3s
        wget --no-check-certificate -O $HOME/install/${arqx} ${IP}:81/${REQUEST}/${arqx} >/dev/null 2>&1 && verificar_arq "${arqx}"
        tput cuu1 && tput dl1
        pontos+="."
        n=$(($n + 1))
    done
    echo -ne " ---> ESTADO : \033[1;45m$ofen\033[0m  con "
    [[ ! -e $HOME/install/log.txt ]] && touch $HOME/install/log.txt
    echo " $(cat <$HOME/install/log.txt | wc -l) FILES " && rm -f $HOME/install/log.txt
    msg -ne " \033[1;42mESTADO :\033[0m "
    [[ -e $HOME/list-key ]] && {
        echo -ne "  "
        [[ $ofen = "KEY DE ChumoGH!" ]] &&
            echo -e "KEY FUNCIONAL" && rm -f $HOME/list-key && echo -ne "\033[0m"
    } || echo -e " KEY INVALIDA O USADA\033[0m\n"
    #curl -s --connect-timeout 2 ${IiP}:81/${REQUEST}/menu_credito > menu_credito
    msg -ne " RESELLER del Key :\033[0m  "
    [[ -e $HOME/install/menu_credito ]] && {
        echo -ne "  "
        [[ "$(cat $HOME/install/menu_credito)" = "" ]] && {
            echo -e "SIN RESELLER\033[0m"
        } || echo -e "$(cat $HOME/install/menu_credito)\033[0m" && rm -rf $HOME/install && echo -ne "\033[0m"
    } || echo -e " NO HAY CONTENIDO DE KEY Key\033[0m\n"
    read -p "Enter"
}

alter_id() {
    while [[ ${varread} != @([0-3]) ]]; do
        tittle
        msg -bar
        echo -e " \033[0;35m[\033[0;36m1\033[0;35m] \033[0;34m<\033[0;33m Alterar Creditos POR ID   \033[0;32m(#OFICIAL)"
        echo -e " \033[0;35m[\033[0;36m2\033[0;35m] \033[0;34m<\033[0;33m REINICIAR CONTADOR TOTAL  \033[0;32m(#OFICIAL)"
        msg -bar
        echo -e " \033[0;35m[\033[0;36m0\033[0;35m] \033[0;34m<\033[0;33m Regresar"
        msg -bar
        echo -ne "${cor[6]}"
        read -p " Opcion : " varread
    done
    msg -bar
    if [[ ${varread} = 0 ]]; then
        return 0
    elif [[ ${varread} = 1 ]]; then
        echo -e "\033[1;32m DIGITA ID A REINICIAR "
        msg -ne " Paste ID : " && read newli
        sed -i "/${newli}/d" /etc/ADM-db/num-key.cont
        echo "REGISTRO EN $(grep -o -i $newli /etc/ADM-db/num-key.cont | wc -l) KEYS"
        read -p "Limite Aplicado Exitosamente"
    elif [[ ${varread} = 2 ]]; then
        echo -e "\033[1;32m BORRAREMOS EL CONTEO TOTAL DEL GENERADOR "
        rm -rf /etc/ADM-db/num-key.cont && touch /etc/ADM-db/num-key.cont
        echo "REGISTRO EN $(cat /etc/ADM-db/num-key.cont | wc -l) KEYS"
        read -p "Limite Aplicado Exitosamente"
    fi
}

alter_limit() {
    echo -e "\033[1;32m DIGITA TU NUEVO LIMITE "
    msg -ne "New Limit: " && read newli
    echo $newli >/etc/ADM-db/limit
    read -p "Limite Aplicado Exitosamente"
}



menk(){

while :; do
    menau
    selection=$(selection_fun $lim_menu)
    case ${selection} in
    0) cd $HOME && break ;;
    1) alter_limit ;;
    2) remover_key ;;
    3) remover_key_usada ;;
    4) fix_key ;;
    5) start_gen ;;
    6)
        echo -ne "\033[1;36m"
        echo -e "$(cat /etc/gerar-sh-log)" 2>/dev/null || echo "NINGUN LOG DE MOMENTO"
        echo -ne "\033[0m" && read -p "Enter"
        ;;
    7) fum_ver ;;
    8) act_gen ;;
    9) att_gen_key ;;
    10) bot_menu ;;
    11) alter_id ;;
    esac
done
}

