FROM kalilinux/kali-rolling

ENV DEBIAN_FRONTEND=noninteractive

# ── Core tools ────────────────────────────────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
    python3 python3-pip python3-dev \
    curl wget gnupg lsb-release ca-certificates \
    net-tools iputils-ping dnsutils whois \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# ── Pentest tools ─────────────────────────────────────────────────────────────
RUN apt-get update && apt-get install -y --no-install-recommends \
    nmap nikto sqlmap \
    postgresql postgresql-client \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# ── Metasploit (apt, non-fatal) ───────────────────────────────────────────────
# Kali mirrors can be flaky during Docker build; mark as || true so the image
# still builds even if the mirror is temporarily unreachable.
RUN apt-get update \
    && apt-get install -y --no-install-recommends metasploit-framework \
    && apt-get clean && rm -rf /var/lib/apt/lists/* \
    || (echo "[!] Kali mirror unavailable — attempting Rapid7 installer …" \
        && curl -sSf https://raw.githubusercontent.com/rapid7/metasploit-omnibus/master/config/templates/metasploit-framework-wrappers/msfupdate.erb \
           -o /tmp/msfinstall \
        && chmod 755 /tmp/msfinstall \
        && /tmp/msfinstall \
        || echo "[!] Metasploit not installed — MSF features will be disabled at runtime")

# ── Optional pentest tools (failures are non-fatal) ──────────────────────────
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
       hydra dirb smbclient exploitdb sshpass snmp telnet \
    && apt-get clean && rm -rf /var/lib/apt/lists/* \
    || echo "[!] Some optional tools skipped"

RUN apt-get update \
    && (apt-get install -y --no-install-recommends john      || true) \
    && (apt-get install -y --no-install-recommends gobuster  || true) \
    && (apt-get install -y --no-install-recommends enum4linux || true) \
    && (apt-get install -y --no-install-recommends wpscan    || true) \
    && apt-get clean && rm -rf /var/lib/apt/lists/*

# ── App directory ─────────────────────────────────────────────────────────────
RUN mkdir -p /app/templates /app/results

WORKDIR /app

# Copy requirements first (for layer caching)
COPY requirements.txt /app/requirements.txt

# Install Python packages
RUN pip3 install --break-system-packages --ignore-installed -r /app/requirements.txt

# Copy application files
COPY app.py       /app/app.py
COPY mcp_agent.py /app/mcp_agent.py
COPY mcp_server.py /app/mcp_server.py
COPY webapp_kb.py  /app/webapp_kb.py

# Copy templates
COPY templates/ /app/templates/

# Initialise MSF database (non-fatal — runs again at startup)
RUN service postgresql start && msfdb init || true

EXPOSE 4444

CMD bash -c "\
  service postgresql start && sleep 2 && \
  msfdb init 2>/dev/null && \
  msfrpcd -P msf -U msf -S -a 0.0.0.0 -p 55553 && \
  sleep 3 && \
  cd /app && python3 app.py"
