#!/bin/bash
# ==============================================================================
# NEW BARWARE - IMSI-catcher (Stingray) detection + AOSP evasion, non-root.
# Runs in Termux over local Wireless ADB (127.0.0.1), paired via mDNS.
#
#   curl -fsSL https://tinyradr.lovable.app/agent/new_barware.sh -o ~/new_barware.sh
#   chmod +x ~/new_barware.sh && ~/new_barware.sh
# ==============================================================================

# --- CONFIGURATION ---
TRUSTED_TACS=(65535 12345 54321) # Add your known good TACs here
WAIT_TIME=12                     # AOSP 10s power-down buffer + 2s

LAST_TAC=""

echo "[+] Initializing New Barware System..."

# 1. Hold CPU Wake Lock
termux-wake-lock
echo "[+] CPU Wake Lock acquired."

# 2. Automated mDNS Port Discovery & ADB Connection
echo "[*] Scanning for Wireless Debugging port via mDNS..."

ADB_PORT=$(adb mdns services 2>/dev/null | grep "_adb-tls-connect._tcp" | awk -F':' '{print $NF}' | head -n 1)

if [ -z "$ADB_PORT" ]; then
    echo "[!] Could not auto-detect port via mDNS."
    echo "[!] Please ensure Wireless Debugging is ENABLED in Developer Options."
    echo -n "[?] Enter Wireless Debugging Port manually: "
    read -r ADB_PORT
fi

echo "[*] Connecting ADB to localhost:${ADB_PORT}..."
adb connect "127.0.0.1:${ADB_PORT}"
sleep 2

# Verify ADB Link
if ! adb devices | grep -q "device$"; then
    echo "[!] Error: ADB connection failed. Verify pairing in Developer Options."
    exit 1
fi

echo "[+] ADB Link Established successfully!"
echo "[+] Trusted TAC Whitelist: ${TRUSTED_TACS[*]}"

# Clear Log Buffer
adb logcat -b radio -c

# --- EVASION MODULE ---
evasion_cycle() {
    local rogue_tac="$1"
    echo -e "\n[!] ========================================================="
    echo "[!] ALERT: UNTRUSTED TAC DETECTED ($rogue_tac)"
    echo "[!] INITIATING EVASION CYCLE. Press Ctrl+C to terminate."
    echo "[!] ========================================================="

    while true; do
        echo "[*] Triggering Detach (Airplane Mode ON)..."
        adb shell cmd connectivity airplane-mode enable

        sleep $WAIT_TIME

        echo "[*] Triggering Attach (Airplane Mode OFF)..."
        adb shell cmd connectivity airplane-mode disable

        sleep $WAIT_TIME
    done
}

# --- DETECTION MODULE ---
evaluate_tac() {
    local detected_tac="$1"

    # Ignore zero, max-integer placeholders, or duplicate captures
    if [[ "$detected_tac" == "0" || "$detected_tac" == "2147483647" || "$detected_tac" == "$LAST_TAC" ]]; then
        return
    fi

    LAST_TAC="$detected_tac"
    TIMESTAMP=$(date +"%H:%M:%S")

    local IS_APPROVED=0
    for trusted in "${TRUSTED_TACS[@]}"; do
        if [[ "$detected_tac" == "$trusted" ]]; then
            IS_APPROVED=1
            break
        fi
    done

    if [[ $IS_APPROVED -eq 1 ]]; then
        echo "[${TIMESTAMP}] [ACCEPT] TAC: ${detected_tac} (Verified)"
    else
        evasion_cycle "$detected_tac"
    fi
}

echo "[+] Listener Active. Streaming RIL radio buffer..."

# Main Event Stream
adb logcat -b radio -v threadtime | while read -r line; do
    if [[ "$line" =~ [tT][aA][cC][=:[:space:]]+([0-9]+) ]]; then
        evaluate_tac "${BASH_REMATCH[1]}"
    fi
done
