aboutsummaryrefslogtreecommitdiffstats
path: root/proxy/endpoints.py
diff options
context:
space:
mode:
Diffstat (limited to 'proxy/endpoints.py')
-rw-r--r--proxy/endpoints.py25
1 files changed, 12 insertions, 13 deletions
diff --git a/proxy/endpoints.py b/proxy/endpoints.py
index 0a23e7b..f514226 100644
--- a/proxy/endpoints.py
+++ b/proxy/endpoints.py
@@ -1,13 +1,13 @@
1import logging 1import logging
2from base64 import b64encode, b64decode 2from base64 import b64decode, b64encode
3from uuid import uuid4 3from uuid import uuid4
4 4
5from starlette.endpoints import HTTPEndpoint 5from starlette.endpoints import HTTPEndpoint
6from starlette.requests import Request 6from starlette.requests import Request
7from starlette.responses import Response 7from starlette.responses import Response
8 8
9from proxy.utils import get_path_with_query_string
10from proxy.queue import ProxyQueue 9from proxy.queue import ProxyQueue
10from proxy.utils import get_path_with_query_string
11from ttun_server.types import HttpMessage, HttpMessageType, HttpRequestData 11from ttun_server.types import HttpMessage, HttpMessageType, HttpRequestData
12 12
13logger = logging.getLogger(__name__) 13logger = logging.getLogger(__name__)
@@ -18,16 +18,15 @@ class HeaderMapping:
18 self._headers = headers 18 self._headers = headers
19 19
20 def items(self): 20 def items(self):
21 for header in self._headers: 21 yield from self._headers
22 yield header
23 22
24 23
25class Proxy(HTTPEndpoint): 24class Proxy(HTTPEndpoint):
26 async def dispatch(self) -> None: 25 async def dispatch(self) -> None:
27 request = Request(self.scope, self.receive) 26 request = Request(self.scope, self.receive)
28 27
29 subdomain = request.path_params['subdomain'] 28 subdomain = request.path_params["subdomain"]
30 response = Response(content='Not Found', status_code=404) 29 response = Response(content="Not Found", status_code=404)
31 30
32 identifier = str(uuid4()) 31 identifier = str(uuid4())
33 response_queue = await ProxyQueue.create_for_identifier(identifier) 32 response_queue = await ProxyQueue.create_for_identifier(identifier)
@@ -35,7 +34,7 @@ class Proxy(HTTPEndpoint):
35 try: 34 try:
36 request_queue = await ProxyQueue.get_for_identifier(subdomain) 35 request_queue = await ProxyQueue.get_for_identifier(subdomain)
37 36
38 logger.debug('PROXY %s%s ', subdomain, request.url) 37 logger.debug("PROXY %s%s ", subdomain, request.url)
39 await request_queue.enqueue( 38 await request_queue.enqueue(
40 HttpMessage( 39 HttpMessage(
41 type=HttpMessageType.request.value, 40 type=HttpMessageType.request.value,
@@ -44,17 +43,17 @@ class Proxy(HTTPEndpoint):
44 method=request.method, 43 method=request.method,
45 path=get_path_with_query_string(request), 44 path=get_path_with_query_string(request),
46 headers=list(request.headers.items()), 45 headers=list(request.headers.items()),
47 body=b64encode(await request.body()).decode() 46 body=b64encode(await request.body()).decode(),
48 ) 47 ),
49 ) 48 )
50 ) 49 )
51 50
52 _response = await response_queue.dequeue() 51 _response = await response_queue.dequeue()
53 payload = _response['payload'] 52 payload = _response["payload"]
54 response = Response( 53 response = Response(
55 status_code=payload['status'], 54 status_code=payload["status"],
56 headers=HeaderMapping(payload['headers']), 55 headers=HeaderMapping(payload["headers"]),
57 content=b64decode(payload['body'].encode()) 56 content=b64decode(payload["body"].encode()),
58 ) 57 )
59 except AssertionError: 58 except AssertionError:
60 pass 59 pass