add ip/ua app and deploy script
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
54
app.py
Normal file
54
app.py
Normal file
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
import json
|
||||
import os
|
||||
from datetime import datetime, timezone
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
def _write_json(self, status=200):
|
||||
client_ip = self.headers.get("X-Forwarded-For", "").split(",")[0].strip()
|
||||
if not client_ip:
|
||||
client_ip = self.client_address[0]
|
||||
|
||||
payload = {
|
||||
"ip": client_ip,
|
||||
"user_agent": self.headers.get("User-Agent", ""),
|
||||
"method": self.command,
|
||||
"path": self.path,
|
||||
"timestamp": datetime.now(timezone.utc).isoformat(),
|
||||
"headers": {k: v for k, v in self.headers.items()},
|
||||
}
|
||||
|
||||
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()
|
||||
Reference in New Issue
Block a user