#!/bin/bash

# MACVerify Quick Network Scanner for Linux/Mac
# Bash script for quick network scanning

# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# Generate session ID
SESSION_ID="scan_$(date +%s)_$$"

echo -e "${CYAN}MACVerify Network Scanner - Quick Scan${NC}"
echo -e "${YELLOW}Session ID: $SESSION_ID${NC}"
echo ""

# Get local IP
if [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS
    LOCAL_IP=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | head -1 | awk '{print $2}')
    OS_INFO="macOS Bash"
else
    # Linux
    LOCAL_IP=$(hostname -I | awk '{print $1}')
    OS_INFO="Linux Bash"
fi

echo -e "${GREEN}Scanning network...${NC}"

# Get ARP table and format as JSON
DEVICES="["
FIRST=true

# Different commands for different OS
if [[ "$OSTYPE" == "darwin"* ]]; then
    # macOS
    ARP_DATA=$(arp -an | grep -E "([0-9]{1,3}\.){3}[0-9]{1,3}")
else
    # Linux
    ARP_DATA=$(ip neighbor 2>/dev/null || arp -n)
fi

while IFS= read -r line; do
    # Extract IP and MAC
    IP=$(echo "$line" | grep -oE "([0-9]{1,3}\.){3}[0-9]{1,3}" | head -1)
    MAC=$(echo "$line" | grep -oE "([0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}" | head -1)
    
    if [ ! -z "$IP" ] && [ ! -z "$MAC" ]; then
        # Format MAC address
        MAC=$(echo "$MAC" | tr '[:lower:]' '[:upper:]' | sed 's/-/:/g')
        
        # Skip broadcast and invalid MACs
        if [[ "$MAC" != "FF:FF:FF:FF:FF:FF" ]] && [[ "$MAC" != *"00:00:00"* ]]; then
            # Try to get hostname
            HOSTNAME=$(nslookup "$IP" 2>/dev/null | grep "name =" | cut -d= -f2 | tr -d ' ' | tr -d '.' | head -1)
            
            if [ "$FIRST" = false ]; then
                DEVICES="$DEVICES,"
            fi
            
            DEVICES="$DEVICES{\"ip\":\"$IP\",\"mac\":\"$MAC\",\"type\":\"dynamic\",\"hostname\":\"$HOSTNAME\"}"
            FIRST=false
        fi
    fi
done <<< "$ARP_DATA"

DEVICES="$DEVICES]"

# Count devices
DEVICE_COUNT=$(echo "$DEVICES" | grep -o "\"ip\"" | wc -l)
echo -e "${GREEN}Found $DEVICE_COUNT devices!${NC}"

# Prepare JSON data
JSON_DATA=$(cat <<EOF
{
    "session_id": "$SESSION_ID",
    "devices": $DEVICES,
    "scan_time": "$(date '+%Y-%m-%d %H:%M:%S')",
    "total_found": $DEVICE_COUNT,
    "local_ip": "$LOCAL_IP",
    "os_info": "$OS_INFO",
    "scan_method": "Unix ARP Quick Scan"
}
EOF
)

# Send to server
echo -e "${YELLOW}Sending results to server...${NC}"
RESPONSE=$(curl -s -X POST \
    -H "Content-Type: application/json" \
    -d "$JSON_DATA" \
    "https://macverify.com/scan/api/submit.php")

# Check response
if echo "$RESPONSE" | grep -q "\"success\":true"; then
    RESULT_URL="https://macverify.com/scan/results.php?session=$SESSION_ID"
    echo -e "${GREEN}Success! Opening results...${NC}"
    
    # Open browser based on OS
    if [[ "$OSTYPE" == "darwin"* ]]; then
        open "$RESULT_URL"
    elif command -v xdg-open > /dev/null; then
        xdg-open "$RESULT_URL"
    elif command -v gnome-open > /dev/null; then
        gnome-open "$RESULT_URL"
    fi
    
    echo ""
    echo -e "${CYAN}Results URL: $RESULT_URL${NC}"
else
    echo -e "${RED}Error: Failed to send results${NC}"
    echo "$RESPONSE"
fi

echo ""
echo -e "${GREEN}Scan complete!${NC}"