#!/usr/bin/python3 from starlette.applications import Starlette from starlette.responses import Response from starlette.routing import Route import httpx async def topic(request): host = request.headers.get("host") path = request.path_params["path"] target_url = f"https://{host}/{path}" pubKey = request.headers.get("crypto-key").encode() salt = request.headers.get("encryption").encode() body = await request.body() + b"\n" + pubKey + b"\n" + salt + b"\naesgcm" headers = dict(request.headers) del headers["content-encoding"] del headers["content-length"] # Forward the request to the target URL async with httpx.AsyncClient() as client: upstream_response = await client.request( method=request.method, url=target_url, headers=headers, data=body, params=request.query_params, ) return Response( content=upstream_response.content, status_code=upstream_response.status_code ) app = Starlette( debug=True, routes=[ Route("/{path:path}", topic, methods=["GET", "POST", "PUT"]), ], )