#!/system/bin/sh

# 增强型颜色定义
RED='\033[1;31m'      # 错误
GREEN='\033[1;32m'    # 成功
YELLOW='\033[1;33m'   # 警告
BLUE='\033[1;34m'     # 信息
PURPLE='\033[1;35m'   # 状态
CYAN='\033[1;36m'     # 进度
NC='\033[0m'          # 重置颜色

# 目标文件路径
TARGET_FILES="
/data/data/com.tencent.tmgp.dfm/files/ano_tmp/mrpcs_a_cd.data
/data/data/com.tencent.tmgp.dfm/files/ano_tmp/custom_cache/mrpcs_a_cd.data
"

# 显示主菜单
show_menu() {
    clear
    echo -e "${PURPLE}╔════════════════════════════════════╗"
    echo -e "║${GREEN}🛡️ DFM防3/7/1/1个月 v7.0${PURPLE}║"
    echo -e "╚════════════════════════════════════╝${NC}"
    echo -e "${CYAN}1. 开启预防设备脸"
    echo -e "${CYAN}2. 开启修改权限监控"
    echo -e "${RED}3. 退出程序${NC}"
    echo -e "${PURPLE}══════════════════════════════════════${NC}"
    echo -n "请选择操作 (1-3): "
}

# 伪开启预防设备脸
fake_prevention() {
    echo -e "${YELLOW}正在开启预防设备脸功能...${NC}"
    
    # 生成随机设备ID
    name=$(tr -dc '1-9' < /dev/urandom | head -c 8)
    while echo "$name" | grep -q "'"; do
        name=$(tr -dc '1-9' < /dev/urandom | head -c 8)
    done 
    
    # 修改设备序列号
    resetprop ro.serialno $name
    echo -e "${GREEN}设备序列号已修改为: $name${NC}"
    
    # 清理残留文件
    rm -rf /data/user/0/com.tencent.tmgp.pubgmhd/files/ano_tmp
    rm -rf /data/data/com.tencent.tmgp.pubgmhd/files/ano_tmp
    
    # 修改IP地址
    INTERFACE="wlan0"
    IP=$(ip addr show $INTERFACE | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d/ -f1)
    IP_PREFIX=$(echo $IP | cut -d. -f1-3)
    NEW_IP_LAST_PART1=$(($RANDOM % 254 + 1))
    NEW_IP_LAST_PART2=$(($RANDOM % 254 + 1))
    NEW_IP1="${IP_PREFIX}.${NEW_IP_LAST_PART1}"
    NEW_IP2="${IP_PREFIX}.${NEW_IP_LAST_PART2}"
    ip addr add $NEW_IP1/24 dev $INTERFACE
    ip addr add $NEW_IP2/24 dev $INTERFACE
    
    echo -e "${GREEN}IP地址已修改为: $NEW_IP1 和 $NEW_IP2${NC}"
    echo -e "${YELLOW}开启预防设备脸功能完成!${NC}"
    sleep 2
}

# 终极权限修复
fix_permission() {
    file="$1"
    filename="${file##*/}"
    
    # 方法1：使用busybox
    if [ -x /system/xbin/busybox ]; then
        /system/xbin/busybox chmod 000 "$file"
        /system/xbin/busybox chattr +i "$file"
        if [ $? -eq 0 ]; then
            echo -e "${GREEN}✅ ${filename} 修复成功 (BusyBox方式)${NC}"
            return 0
        fi
    fi
    
    # 方法2：使用toybox
    if [ -x /system/bin/toybox ]; then
        /system/bin/toybox chmod 000 "$file"
        if [ $? -eq 0 ]; then
            echo -e "${GREEN}✅ ${filename} 修复成功 (ToyBox方式)${NC}"
            return 0
        fi
    fi
    
    # 方法3：直接操作inode
    if dd if=/dev/null of="$file" bs=1 count=0 2>/dev/null; then
        echo -n > "$file"
        chmod 000 "$file" 2>/dev/null
        if [ $? -eq 0 ]; then
            echo -e "${GREEN}✅ ${filename} 修复成功 (底层写入)${NC}"
            return 0
        fi
    fi
    
    echo -e "${RED}❌ ${filename} 所有修复方式均失败${NC}"
    return 1
}

# 权限检查
check_permission() {
    file="$1"
    perm=$(stat -c "%A" "$file" 2>/dev/null || ls -ld "$file" 2>/dev/null | awk '{print $1}')
    
    case "$perm" in
        "----------"|"---------") return 0 ;;
        *) return 1 ;;
    esac
}

# 权限监控
monitor_permissions() {
    CHECK_INTERVAL=5
    CHECK_COUNT=0
    
    # 准备环境
    echo -e "${YELLOW}正在准备系统环境...${NC}"
    
    # SELinux处理
    SELINUX_STATUS=$(getenforce 2>/dev/null)
    case "$SELINUX_STATUS" in
        "Enforcing")
            echo -e "${YELLOW}临时禁用SELinux强制模式...${NC}"
            setenforce 0
            trap 'echo -e "${YELLOW}恢复SELinux强制模式...${NC}"; setenforce 1' EXIT
            ;;
    esac
    
    # 挂载检查
    if grep -q " /data .*ro" /proc/mounts; then
        echo -e "${YELLOW}重新挂载/data为读写模式...${NC}"
        {
            mount -o remount,rw /data ||
            [ -x /system/xbin/busybox ] && /system/xbin/busybox mount -o remount,rw /data
        } 2>/dev/null
    fi
    
    echo -e "${GREEN}系统准备就绪，开始监控...${NC}"
    
    while true; do
        CHECK_COUNT=$((CHECK_COUNT + 1))
        echo -e "${PURPLE}🔍 检查轮次: ${CHECK_COUNT} [$(date '+%H:%M:%S')]${NC}"
        
        for file in $TARGET_FILES; do
            file=$(echo "$file" | tr -d '\r\n' | sed 's/\/\//\//g')
            filename=${file##*/}
            
            if [ -f "$file" ]; then
                if check_permission "$file"; then
                    echo -e "${GREEN}✓ ${filename} 状态正常${NC}"
                else
                    echo -e "${RED}⚠ 检测到异常权限: ${filename}${NC}"
                    fix_permission "$file"
                fi
            else
                echo -e "${BLUE}⏳ 等待文件: ${filename}...${NC}"
            fi
        done
        
        i=$CHECK_INTERVAL
        while [ $i -gt 0 ]; do
            printf "\r${CYAN}⏳ 下次检查: ${YELLOW}%2d秒后 ${RED}(Ctrl+C退出)${NC}" $i
            sleep 1
            i=$((i - 1))
        done
        printf "\n"
    done
}

# 主程序
main() {
    # 检查root权限
    if [ "$(id -u)" -ne 0 ]; then
        echo -e "${RED}✖ 需要root权限执行！${NC}"
        exit 1
    fi

    while true; do
        show_menu
        read choice
        
        case $choice in
            1) fake_prevention ;;
            2) monitor_permissions ;;
            3) 
                echo -e "${GREEN}退出程序...${NC}"
                exit 0
                ;;
            *) 
                echo -e "${RED}无效选择，请重新输入!${NC}"
                sleep 1
                ;;
        esac
    done
}

# 启动主程序
main