#Requires -Version 5.1
<#
  Login dashboard: listening ports (your "sites"), IIS, Docker, quick stats.
  Window stays open (-NoExit) so you can read; close manually.
#>
$ErrorActionPreference = 'SilentlyContinue'
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
try {
    $Host.UI.RawUI.WindowTitle = 'Active sites / listeners - ' + $env:COMPUTERNAME
    if ($Host.UI.RawUI.MaxWindowSize.Width -ge 120) {
        $Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size(120, 3000)
        $Host.UI.RawUI.WindowSize = New-Object Management.Automation.Host.Size(120, 42)
    }
} catch {}

function Write-Banner {
    param([string]$Title, [string]$Color = 'Cyan')
    Write-Host ""
    Write-Host "=== $Title ===" -ForegroundColor $Color
}

# --- Header ---
Write-Host ""
Write-Host "  LOGIN DASHBOARD  " -NoNewline -ForegroundColor Black -BackgroundColor Cyan
Write-Host "  $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')  " -ForegroundColor White -BackgroundColor DarkGray
$os = Get-CimInstance Win32_OperatingSystem
$boot = $os.LastBootUpTime
$up = (Get-Date) - $boot
Write-Host "  Machine: " -NoNewline -ForegroundColor Gray
Write-Host "$env:COMPUTERNAME" -NoNewline -ForegroundColor White
Write-Host "   User: " -NoNewline -ForegroundColor Gray
Write-Host "$env:USERNAME" -NoNewline -ForegroundColor White
Write-Host "   Uptime: " -NoNewline -ForegroundColor Gray
Write-Host ("{0}d {1}h {2}m" -f $up.Days, $up.Hours, $up.Minutes) -ForegroundColor Yellow

$cs = Get-CimInstance Win32_ComputerSystem
$totalGb = [math]::Round($cs.TotalPhysicalMemory / 1GB, 1)
$os2 = Get-CimInstance Win32_OperatingSystem
$freeGb = [math]::Round($os2.FreePhysicalMemory / 1024 / 1024, 1)
Write-Host "  RAM: " -NoNewline -ForegroundColor Gray
Write-Host ("{0} GB free / {1} GB total" -f $freeGb, $totalGb) -ForegroundColor DarkGray

$sysDrive = $env:SystemDrive + '\'
$disk = Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='$($env:SystemDrive)'"
if ($disk) {
    $freeDisk = [math]::Round($disk.FreeSpace / 1GB, 1)
    $totalDisk = [math]::Round($disk.Size / 1GB, 1)
    Write-Host "  Disk $env:SystemDrive " -NoNewline -ForegroundColor Gray
    Write-Host ("{0} GB free / {1} GB total" -f $freeDisk, $totalDisk) -ForegroundColor DarkGray
}

# --- Listening TCP ---
Write-Banner -Title 'LISTENING TCP (local sites / APIs / services)' -Color Green
try {
    $listen = @(Get-NetTCPConnection -State Listen -ErrorAction Stop)
} catch {
    $listen = @()
    Write-Host '  (Get-NetTCPConnection failed - try running as Administrator for full detail.)' -ForegroundColor Yellow
}

$rows = foreach ($conn in $listen) {
    $proc = Get-Process -Id $conn.OwningProcess -ErrorAction SilentlyContinue
    $path = ''
    try {
        if ($proc -and $proc.Path) { $path = $proc.Path }
    } catch {}
    $pathShort = $path
    if ($pathShort.Length -gt 55) { $pathShort = $pathShort.Substring(0, 52) + '...' }
    [PSCustomObject]@{
        Address = $conn.LocalAddress
        Port    = $conn.LocalPort
        PID     = $conn.OwningProcess
        Process = if ($proc) { $proc.ProcessName } else { '?' }
        Path    = $pathShort
    }
}

$common = @(80, 81, 443, 3000, 3001, 4200, 5000, 5001, 5173, 5432, 6379, 8000, 8080, 8443, 8888, 9000, 18888)
$highlight = $rows | Where-Object { $_.Port -in $common } | Sort-Object { [int]$_.Port }
$rest = $rows | Where-Object { $_.Port -notin $common } | Sort-Object { [int]$_.Port }

if ($highlight) {
    Write-Host '  Common dev / web ports:' -ForegroundColor Yellow
    $highlight | Format-Table -AutoSize Address, Port, PID, Process
}
if ($rest) {
    Write-Host '  All other listeners:' -ForegroundColor DarkGray
    $rest | Format-Table -AutoSize Address, Port, PID, Process
}
if (-not $rows -or $rows.Count -eq 0) {
    Write-Host '  No TCP listeners found (or query failed).' -ForegroundColor Yellow
}

# --- IIS ---
Write-Banner -Title 'IIS WEB SITES (if installed)' -Color Magenta
if (Get-Command Get-Website -ErrorAction SilentlyContinue) {
    try {
        Import-Module WebAdministration -ErrorAction SilentlyContinue | Out-Null
        Get-Website | Select-Object Name, State, PhysicalPath |
            Format-Table -AutoSize -Wrap
    } catch {
        Write-Host ('  IIS error: {0}' -f $_) -ForegroundColor DarkYellow
    }
} else {
    Write-Host '  IIS not installed or WebAdministration not available.' -ForegroundColor DarkGray
}

# --- Docker ---
Write-Banner -Title 'DOCKER (running containers)' -Color Blue
if (Get-Command docker -ErrorAction SilentlyContinue) {
    docker ps 2>$null
    if ($LASTEXITCODE -ne 0) {
        Write-Host '  (docker not running or daemon unreachable)' -ForegroundColor DarkGray
    }
} else {
    Write-Host '  Docker CLI not found.' -ForegroundColor DarkGray
}

if (Get-NetTCPConnection -LocalPort 18888 -State Listen -ErrorAction SilentlyContinue) {
    Write-Host '  * Port 18888 listening - likely Proxy Chain Manager (GOST).' -ForegroundColor DarkCyan
}

Write-Host ''
Write-Host '  Pin this window or move to a second monitor. Close when done.' -ForegroundColor DarkGray
Write-Host ''
