#!/usr/bin/env python3
"""LLM Team UI - Web interface to configure and run multi-model teams."""
import json
import os
import time
import threading
import secrets
import hashlib
import logging
import re
import requests
import random
import psycopg2
import psycopg2.extras
import bcrypt
from concurrent.futures import ThreadPoolExecutor, as_completed
from flask import Flask, render_template_string, request, jsonify, Response, redirect, url_for, session
from functools import wraps
app = Flask(__name__)
app.secret_key = os.environ.get("FLASK_SECRET", secrets.token_hex(32))
# ─── SECURITY LOGGING ─────────────────────────────────────────
# Dedicated security log for fail2ban and audit trail
_sec_handler = logging.FileHandler("/var/log/llm-team-security.log")
_sec_handler.setFormatter(logging.Formatter("%(asctime)s %(message)s"))
sec_log = logging.getLogger("security")
sec_log.addHandler(_sec_handler)
sec_log.setLevel(logging.WARNING)
# ─── EMAIL ALERTS ──────────────────────────────────────────────
SMTP_HOST = os.environ.get("SMTP_HOST", "127.0.0.1")
SMTP_PORT = int(os.environ.get("SMTP_PORT", "1025"))
ALERT_FROM = os.environ.get("ALERT_FROM", "security@island37.com")
ALERT_TO = os.environ.get("ALERT_TO", "admin@island37.com")
def send_security_alert(subject, body):
"""Send security alert email (non-blocking)."""
def _send():
try:
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg["Subject"] = f"[LLM Team Security] {subject}"
msg["From"] = ALERT_FROM
msg["To"] = ALERT_TO
msg.set_content(body)
with smtplib.SMTP(SMTP_HOST, SMTP_PORT, timeout=5) as s:
s.send_message(msg)
except Exception as e:
sec_log.error("EMAIL_FAILED subject=%s error=%s", subject, str(e))
threading.Thread(target=_send, daemon=True).start()
# Known exploit paths that scanners probe
EXPLOIT_PATTERNS = re.compile(
r"(\.env|wp-admin|wp-login|phpmyadmin|\.git|/admin\.php|/config\.|"
r"\.asp|\.aspx|/cgi-bin|/shell|/eval|/exec|/passwd|/etc/shadow|"
r"\.\./|%2e%2e|