Aggiunto un log migliore
This commit is contained in:
@@ -230,27 +230,19 @@ def build_http_redirect(location: str) -> bytes:
|
||||
return "\r\n".join(headers).encode("utf-8")
|
||||
|
||||
|
||||
def build_user_host(host: str, user_id: str) -> Optional[str]:
|
||||
if not host:
|
||||
return None
|
||||
def rewrite_http_request_target(data: bytes, target: str) -> bytes:
|
||||
header_end = data.find(b"\r\n")
|
||||
if header_end == -1:
|
||||
return data
|
||||
|
||||
hostname, separator, port = host.partition(":")
|
||||
hostname = hostname.strip().lower()
|
||||
if not hostname:
|
||||
return None
|
||||
request_line = data[:header_end].decode("iso-8859-1", errors="replace")
|
||||
parts = request_line.split(" ", 2)
|
||||
if len(parts) != 3:
|
||||
return data
|
||||
|
||||
if hostname == "localhost":
|
||||
target_host = f"{user_id}.localhost"
|
||||
else:
|
||||
labels = [label for label in hostname.split(".") if label]
|
||||
if not labels:
|
||||
return None
|
||||
if labels[0] == user_id:
|
||||
target_host = hostname
|
||||
else:
|
||||
target_host = ".".join([user_id, *labels])
|
||||
|
||||
return f"{target_host}:{port}" if separator else target_host
|
||||
method, _, version = parts
|
||||
rewritten_line = f"{method} {target} {version}".encode("iso-8859-1")
|
||||
return rewritten_line + data[header_end:]
|
||||
|
||||
|
||||
def extract_user_from_http_request(data: bytes, known_users: Dict[str, Dict[str, Any]]):
|
||||
@@ -287,13 +279,10 @@ def extract_user_from_http_request(data: bytes, known_users: Dict[str, Dict[str,
|
||||
if segments:
|
||||
candidate = segments[0]
|
||||
if candidate in known_users:
|
||||
user_host = build_user_host(headers.get("host", ""), candidate)
|
||||
if user_host:
|
||||
redirect_path = "/" + "/".join(segments[1:]) if len(segments) > 1 else "/"
|
||||
if separator:
|
||||
redirect_path = f"{redirect_path}?{query}"
|
||||
redirect_target = f"http://{user_host}{redirect_path}"
|
||||
return None, data, redirect_target
|
||||
rewritten_path = "/" + "/".join(segments[1:]) if len(segments) > 1 else "/"
|
||||
rewritten_target = f"{rewritten_path}?{query}" if separator else rewritten_path
|
||||
rewritten_data = rewrite_http_request_target(data, rewritten_target)
|
||||
return known_users[candidate], rewritten_data, None
|
||||
|
||||
return None, data, None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user