#!/bin/bash
# DYNR Module: Security Stance v2.3.6

run_security_stance() {
    BLUE='\033[38;5;45m'; PURPLE='\033[38;5;127m'; GOLD='\033[38;5;178m'; RED='\033[38;5;196m'; NC='\033[0m'
    
    # --- Dynamic Dependency Check ---
    if ! command -v ss &> /dev/null; then
        echo -e "${RED}⚠️  The 'iproute2' package is required for port auditing.${NC}"
        read -p "Manifest this tool now? (y/n): " choice
        if [[ "$choice" == "y" ]]; then
            sudo apt update -qq && sudo apt install -y -qq iproute2
        else
            echo -e "${RED}Stance aborted.${NC}"
            dynr; return
        fi
    fi

    clear
    echo -e "${PURPLE}=================================================${NC}"
    echo -e "${BLUE}    🐉  DYNASTY REVOLUTION: SECURITY STANCE  🐉    ${NC}"
    echo -e "${PURPLE}=================================================${NC}"

    # 1. Active Logins
    echo -e "${GOLD}👥 ACTIVE WARRIORS (Logged In Users):${NC}"
    who | sed 's/^/  ⚔️  /' || echo "  No active sessions found."
    echo -e "${PURPLE}-------------------------------------------------${NC}"

    # 2. Listening Ports (Simplified)
    echo -e "${GOLD}🔓 OPEN GATES (Listening Ports):${NC}"
    # This filters for established listening ports and cleans up the view
    ss -tunl | grep "LISTEN" | awk '{print $5}' | cut -d':' -f2 | sort -nu | while read port; do
        service=$(grep -w "$port/tcp" /etc/services | awk '{print $1}' | head -n 1)
        [[ -z "$service" ]] && service="unknown"
        echo -e "  ${BLUE}Port $port${NC} ($service)"
    done

    # 3. Sudoers Check
    echo -e "${PURPLE}-------------------------------------------------${NC}"
    echo -e "${GOLD}👑 DYNASTY ELDERS (Sudo Privileges):${NC}"
    grep '^sudo:.*' /etc/group | cut -d: -f4 | sed 's/,/, /g' | sed 's/^/  /' || echo "  Root only."

    echo -e "${PURPLE}=================================================${NC}"
    
    read -p "Press Enter to return to the Dynasty..." < /dev/tty
    dynr
}