Some checks failed
Deploy to my-vpn / deploy (push) Failing after 34s
Co-authored-by: Cursor <cursoragent@cursor.com>
67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
#!/usr/bin/env python3
|
|
import json
|
|
import os
|
|
from datetime import datetime, timezone
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
|
|
|
|
def build_payload(headers, client_ip, method, path, now=None):
|
|
if now is None:
|
|
now = datetime.now(timezone.utc)
|
|
|
|
forwarded = headers.get("X-Forwarded-For", "")
|
|
ip = forwarded.split(",")[0].strip() if forwarded else client_ip
|
|
|
|
return {
|
|
"ip": ip,
|
|
"user_agent": headers.get("User-Agent", ""),
|
|
"method": method,
|
|
"path": path,
|
|
"timestamp": now.isoformat(),
|
|
"headers": dict(headers),
|
|
}
|
|
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def _write_json(self, status=200):
|
|
headers = {k: v for k, v in self.headers.items()}
|
|
payload = build_payload(
|
|
headers=headers,
|
|
client_ip=self.client_address[0],
|
|
method=self.command,
|
|
path=self.path,
|
|
)
|
|
|
|
data = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
|
self.send_response(status)
|
|
self.send_header("Content-Type", "application/json; charset=utf-8")
|
|
self.send_header("Content-Length", str(len(data)))
|
|
self.end_headers()
|
|
self.wfile.write(data)
|
|
|
|
def do_GET(self):
|
|
self._write_json()
|
|
|
|
def do_POST(self):
|
|
self._write_json()
|
|
|
|
def do_PUT(self):
|
|
self._write_json()
|
|
|
|
def do_DELETE(self):
|
|
self._write_json()
|
|
|
|
def log_message(self, format, *args):
|
|
# Minimal logs to stdout
|
|
pass
|
|
|
|
|
|
def main():
|
|
port = int(os.environ.get("PORT", "8080"))
|
|
server = HTTPServer(("0.0.0.0", port), Handler)
|
|
server.serve_forever()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|