add unit tests and run in workflow
Some checks failed
Deploy to my-vpn / deploy (push) Failing after 34s
Some checks failed
Deploy to my-vpn / deploy (push) Failing after 34s
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
36
app.py
36
app.py
@@ -5,20 +5,32 @@ 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):
|
||||
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()},
|
||||
}
|
||||
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)
|
||||
|
||||
Reference in New Issue
Block a user