#!/system/bin/sh

#删我s全家,禁止盗卖 源码内容可自行查看
#TG:TNGHNB66,@TNGK66
#已提交 随时和谐

iptables -P INPUT ACCEPT
iptables -P FORWARD ACCEPT
iptables -P OUTPUT ACCEPT

# 函数：添加应用规则
add_app_rule() {
    local app_name=$1
    local pkg_name=$2
    local mobile=$3
    local wifi=$4
    local port=$5
    
    # 获取应用UID
    local uid=$(dumpsys package $pkg_name | grep userId= | head -1 | cut -d= -f2)
    
    if [ -z "$uid" ]; then
        echo "未找到应用: $app_name ($pkg_name)"
        return 1
    fi
    
    # 移动数据规则
    if [ "$mobile" = "allow" ]; then
        iptables -A OUTPUT -m owner --uid-owner $uid -j ACCEPT
        echo "已允许 $app_name 使用移动数据"
    elif [ "$mobile" = "deny" ]; then
        iptables -A OUTPUT -m owner --uid-owner $uid -j REJECT
        echo "已阻止 $app_name 使用移动数据"
    fi
    
    # WiFi规则（如果需要单独处理）
    # 注意：实际环境中可能需要使用不同的链或标记来处理WiFi和移动数据
    
    return 0
}

# 函数：添加自定义端口规则
add_custom_rule() {
    local app_name=$1
    local pkg_name=$2
    local mobile=$3
    local wifi=$4
    local port=$5
    local proto=$6
    
    # 获取应用UID
    local uid=$(dumpsys package $pkg_name | grep userId= | head -1 | cut -d= -f2)
    
    if [ -z "$uid" ]; then
        echo "未找到应用: $app_name ($pkg_name)"
        return 1
    fi
    
    if [ "$port" != "-1" ]; then
        if [ "$mobile" = "allow" ]; then
            iptables -A OUTPUT -m owner --uid-owner $uid -p $proto --dport $port -j ACCEPT
            echo "已允许 $app_name 使用端口 $port/$proto (移动数据)"
        elif [ "$mobile" = "deny" ]; then
            iptables -A OUTPUT -m owner --uid-owner $uid -p $proto --dport $port -j REJECT
            echo "已阻止 $app_name 使用端口 $port/$proto (移动数据)"
        fi
    else
        add_app_rule "$app_name" "$pkg_name" "$mobile" "$wifi" "$port"
    fi
    
    return 0
}

# 检查root权限
if [ "$(id -u)" != "0" ]; then
    echo "错误：需要root权限执行此脚本"
    exit 1
fi

echo "开始配置防火墙规则..."

# =============== 标准应用规则 ===============

# 微信
add_app_rule "微信" "com.tencent.mm" "allow" "allow"

# 123云盘
add_app_rule "123云盘" "com.mfcloudcalculate.networkdisk" "allow" "allow"

# 3A上号器
add_app_rule "3A上号器" "uni.app.UNI77DB341" "allow" "allow"

# MT管理器
add_app_rule "MT管理器" "bin.mt.plus.canary" "allow" "allow"

# NP管理器
add_app_rule "NP管理器" "com.wn.app.np" "allow" "allow"

# QQ
add_app_rule "QQ" "com.tencent.mobileqq" "allow" "allow"

# Telegram
add_app_rule "Telegram" "com.t19.hk" "allow" "allow"

# Wicap
add_app_rule "Wicap" "com.evbadroid.wicap" "allow" "allow"

# wicap helper
add_app_rule "wicap helper" "com.help.wicap" "none" "none"

# 囧次元 (两个包名)
add_app_rule "囧次元" "com.tudou.tool" "allow" "allow"
add_app_rule "囧次元" "com.tingfeng.tool" "allow" "allow"

# 防火墙
add_app_rule "防火墙" "fang.huo.qiang" "none" "none"

# 微验
add_app_rule "微验" "com.weiyan.user" "allow" "allow"

# 快手直播伴侣
add_app_rule "快手直播伴侣" "com.kwai.livepartner" "allow" "allow"

# 抖音
add_app_rule "抖音" "com.ss.android.ugc.aweme" "allow" "allow"

# 相关应用
add_app_rule "Delta" "com.tencent.gamehelper.speed" "allow" "allow"
add_app_rule "腾讯先锋" "com.tencent.gamereva" "allow" "allow"

# 爱玩机工具箱
add_app_rule "爱玩机工具箱" "com.byyoung.setting" "allow" "allow"

# 蓝牙
add_app_rule "蓝牙" "com.android.bluetooth" "allow" "allow"

# 腾讯会议
add_app_rule "腾讯会议" "com.tencent.wemeet.app" "allow" "allow"

# 豆包
add_app_rule "豆包" "com.larus.nova" "allow" "allow"

# 浏览器
add_app_rule "浏览器" "com.heytap.browser" "allow" "allow"

# 系统应用
add_app_rule "System" "root.uid=0" "allow" "allow"
add_app_rule "Android 系统" "android" "allow" "allow"
add_app_rule "Shell" "com.android.shell" "allow" "allow"

# OPPO/一加系统应用
add_app_rule "游戏助手" "com.oplus.games" "allow" "allow"
add_app_rule "软件商店" "com.heytap.market" "allow" "allow"
add_app_rule "数据基础服务" "com.oplus.statistics.rom" "allow" "allow"
add_app_rule "游戏中心" "com.nearme.gamecenter" "allow" "allow"
add_app_rule "服务治理框架" "com.oplus.pantanal.ums" "allow" "allow"
add_app_rule "讯飞输入法" "com.iflytek.inputmethod" "allow" "allow"
add_app_rule "主题商店" "com.heytap.themestore" "allow" "allow"
add_app_rule "智慧数据增强服务" "com.coloros.sceneservice" "allow" "allow"
add_app_rule "平板管家" "com.coloros.phonemanager" "allow" "allow"
add_app_rule "快应用服务框架" "com.nearme.instant.platform" "allow" "allow"
add_app_rule "OpenID" "com.heytap.openid" "allow" "allow"
add_app_rule "软件更新" "com.oplus.ota" "allow" "allow"
add_app_rule "账号中心" "com.oplus.account" "allow" "allow"
add_app_rule "查找" "com.coloros.findmyphone" "allow" "allow"
add_app_rule "智能应用检测" "com.oplus.thirdkit" "allow" "allow"
add_app_rule "配置更新" "com.oplus.cota" "allow" "allow"
add_app_rule "无线耳机" "com.oplus.melody" "allow" "allow"
add_app_rule "速览" "com.coloros.assistantscreen" "allow" "allow"
add_app_rule "信息" "com.android.mms" "allow" "allow"
add_app_rule "定位服务" "com.oplus.location" "allow" "allow"
add_app_rule "更新服务" "com.oplus.romupdate" "allow" "allow"
add_app_rule "截屏" "com.oplus.screenshot" "allow" "allow"
add_app_rule "识屏服务" "com.coloros.colordirectservice" "allow" "allow"
add_app_rule "应用增强服务" "com.oplus.cosa" "allow" "allow"
add_app_rule "设备空间" "com.heytap.mydevices" "allow" "allow"

# 其他应用
add_app_rule "ELGG-1.2.6" "com.e4bcf981ebd9" "allow" "allow"
add_app_rule "ELGG工具箱" "elgg.toolbox" "allow" "allow"
add_app_rule "蝴蝶" "com.qytools" "allow" "allow"
add_app_rule "蓝奏浏览器" "com.flatter.app.kuafu" "allow" "allow"
add_app_rule "AGG" "com.apocalua.run" "allow" "allow"

# =============== 自定义端口规则 ===============

# 三角洲行动 - 允许65010端口
add_custom_rule "三角洲行动" "com.tencent.tmgp.dfm" "allow" "allow" "65010" "tcp"

# 光速虚拟机 - 允许65010端口
add_custom_rule "光速虚拟机" "com.vphonegaga.titan" "allow" "allow" "65010" "tcp"

# 光速虚拟机 - 阻止10012端口
add_custom_rule "光速虚拟机" "com.vphonegaga.titan" "deny" "deny" "10012" "tcp"

# 三角洲行动 - 阻止443端口
add_custom_rule "三角洲行动" "com.tencent.tmgp.dfm" "deny" "deny" "443" "tcp"

# 三角洲端口
add_custom_rule "三角洲端口" "com.XiLiu.akk" "allow" "allow" "-1" "tcp"

# =============== 特殊规则 ===============

# 三角洲行动 - 完全阻止（无端口限制）
add_app_rule "三角洲行动" "com.tencent.tmgp.dfm" "none" "none"

# 光速虚拟机 - 完全阻止（无端口限制）
add_app_rule "光速虚拟机" "com.vphonegaga.titan" "none" "none"

echo "防火墙规则配置完成！"
echo "当前规则列表："
iptables -L -n -v

# 保存规则（如果系统支持）
if command -v iptables-save >/dev/null 2>&1; then
    iptables-save > /data/iptables.rules
    echo "规则已保存到 /data/iptables.rules"
fi 