#!/bin/bash
clear
GREEN='\033[92m'
RED='\033[91m'
YELLOW='\033[93m'
BLUE='\033[94m'
MAGENTA='\033[95m'
CYAN='\033[96m'
RESET='\033[0m'

echo -n -e "${CYAN}请输入包名: ${RESET}"
read -r package_name
if [ -z "$package_name" ]; then
    echo -e "${RED}输入错误:包名不存在或为空${RESET}"
    exit 1
fi

rule_file_path="/storage/emulated/0/Android_监听数据.conf"
game_pid=$(pgrep -f "$package_name")

if [ -z "$game_pid" ]; then
    echo -e "${CYAN}应用尚未启动-等待启动${RESET}"
    while [ -z "$game_pid" ]; do
        sleep 2
        game_pid=$(pgrep -f "$package_name")
    done
    pids_oneline=$(echo "$game_pid" | tr '\n' ' ')
    echo -e "${GREEN}应用进程: $pids_oneline${RESET}"
fi

target_app="$package_name"
mkdir -p "$(dirname "$rule_file_path")"

map_state_to_chinese() {
    local state="$1"
    case "$state" in
        "LISTEN") echo "监听" ;;
        "ESTABLISHED") echo "已建立" ;;
        "SYN_SENT") echo "同步已发送" ;;
        "SYN_RECV") echo "同步已接收" ;;
        "FIN_WAIT1") echo "结束等待1" ;;
        "FIN_WAIT2") echo "结束等待2" ;;
        "TIME_WAIT") echo "时间等待" ;;
        "CLOSE_WAIT") echo "关闭等待" ;;
        "LAST_ACK") echo "最后确认" ;;
        "CLOSED") echo "已关闭" ;;
        "BOUND") echo "已绑定" ;;
        "IDLE") echo "空闲" ;;
        *) echo "$state" ;;
    esac
}

while true; do
    app_running=$(pgrep -f "$target_app")
    if [ -n "$app_running" ]; then
        connections=$(netstat -anp 2>/dev/null | grep "$target_app" 2>/dev/null)
        if [ -n "$connections" ]; then
            while read -r line; do
                proto=$(echo "$line" | awk '{print $1}')
                local_addr=$(echo "$line" | awk '{print $4}')
                remote_addr=$(echo "$line" | awk '{print $5}')
                state=$(echo "$line" | awk '{print $6}')
                if [ "$state" = "LISTEN" ]; then
                    direction="入站"
                    ip_port="$local_addr"
                elif [ -n "$remote_addr" ] && [ "$remote_addr" != "*:*" ] && ( [ "$state" = "ESTABLISHED" ] || [ "$state" = "SYN_SENT" ] || [ "$state" = "SYN_RECV" ] ); then
                    direction="出站"
                    ip_port="$remote_addr"
                else
                    direction="未知"
                    ip_port="$remote_addr"
                fi
                chinese_state=$(map_state_to_chinese "$state")
                if [ -n "$ip_port" ] && [ "$ip_port" != "*:*" ] && [ "$direction" != "未知" ]; then
                    echo -e "${MAGENTA}检测到${direction}连接: $ip_port (协议: $proto, 状态: $chinese_state)${RESET}"
                fi
            done <<< "$connections"
        else
            echo -e "${YELLOW}目标 $target_app 数据停滞中…${RESET}"
            sleep 3
        fi
    else
        echo -e "${RED}目标 $target_app 进程结束${RESET}"
        echo -n -e "${CYAN}是否保存检测到的连接？(y/N): ${RESET}"
        read -r save_choice
        if [[ "$save_choice" =~ ^[Yy]$ ]]; then
            {
                echo "# 网络连接监控报告"
                echo "# 目标应用: $target_app"
                echo "# 生成时间: $(date '+%Y-%m-%d %H:%M:%S')"
                echo ""
                while read -r line; do
                    proto=$(echo "$line" | awk '{print $1}')
                    local_addr=$(echo "$line" | awk '{print $4}')
                    remote_addr=$(echo "$line" | awk '{print $5}')
                    state=$(echo "$line" | awk '{print $6}')
                    if [ "$state" = "LISTEN" ]; then
                        ip_port="$local_addr"
                    elif [ -n "$remote_addr" ] && [ "$remote_addr" != "*:*" ] && ( [ "$state" = "ESTABLISHED" ] || [ "$state" = "SYN_SENT" ] || [ "$state" = "SYN_RECV" ] ); then
                        ip_port="$remote_addr"
                    fi
                    if [ -n "$ip_port" ] && [ "$ip_port" != "*:*" ]; then
                        echo "$ip_port"
                    fi
                done <<< "$connections"
            } > "$rule_file_path"
            echo -e "${GREEN}已保存到: $rule_file_path${RESET}"
        fi
        break
    fi
done