summaryrefslogtreecommitdiffstats
path: root/ttun_server/endpoints.py
blob: 22dcb6d5a237a3daef5fadd31bd5da11f7b799af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import logging
from base64 import b64decode, b64encode
from uuid import uuid4

from starlette.background import BackgroundTask
from starlette.requests import Request
from starlette.responses import Response

from ttun_server.proxy_queue import ProxyQueue
from ttun_server.types import HttpRequestData, HttpMessageType, HttpMessage

logger = logging.getLogger(__name__)


async def proxy(request: Request) -> Response:
    [subdomain, *_] = request.headers['host'].split('.')
    identifier = str(uuid4())
    response_queue = await ProxyQueue.create_for_identifier(identifier)

    try:
        request_queue = await ProxyQueue.get_for_identifier(subdomain)

        logger.debug('PROXY %s%s ', subdomain, request.url)
        await request_queue.enqueue(
            HttpMessage(
                type=HttpMessageType.request.value,
                identifier=identifier,
                payload=HttpRequestData(
                    method=request.method,
                    path=str(request.url).replace(str(request.base_url), '/'),
                    headers=list(request.headers.items()),
                    body=b64encode(await request.body()).decode()
                )
            )
        )

        _response = await response_queue.dequeue()
        payload = _response['payload']
        return Response(
            status_code=payload['status'],
            headers=dict(payload['headers']),
            content=b64decode(payload['body'].encode()),
            background=BackgroundTask(response_queue.delete)
        )
    except AssertionError:
        return Response(
            content='Not Found',
            status_code=404,
            background=BackgroundTask(response_queue.delete)
        )


async def health(_: Request) -> Response:
    return Response(content='OK', status_code=200)