diff options
Diffstat (limited to 'ttun_server')
| -rw-r--r-- | ttun_server/__init__.py | 9 | ||||
| -rw-r--r-- | ttun_server/websockets.py | 3 |
2 files changed, 11 insertions, 1 deletions
diff --git a/ttun_server/__init__.py b/ttun_server/__init__.py index 4af6ca0..f5d6b45 100644 --- a/ttun_server/__init__.py +++ b/ttun_server/__init__.py | |||
| @@ -2,8 +2,10 @@ import logging | |||
| 2 | import os | 2 | import os |
| 3 | 3 | ||
| 4 | from fastapi import FastAPI | 4 | from fastapi import FastAPI |
| 5 | from starlette.middleware.authentication import AuthenticationMiddleware | ||
| 5 | from starlette.routing import Host, Route, WebSocketRoute, Mount | 6 | from starlette.routing import Host, Route, WebSocketRoute, Mount |
| 6 | 7 | ||
| 8 | from authentication.backend import BearerTokenAuthBackend | ||
| 7 | from authentication.endpoints import authentication, oauth | 9 | from authentication.endpoints import authentication, oauth |
| 8 | from proxy import app as proxy_app | 10 | from proxy import app as proxy_app |
| 9 | from ttun_server.endpoints import health | 11 | from ttun_server.endpoints import health |
| @@ -16,12 +18,17 @@ app = FastAPI( | |||
| 16 | routes=[ | 18 | routes=[ |
| 17 | Mount('/auth/', app=authentication), | 19 | Mount('/auth/', app=authentication), |
| 18 | Mount('/oauth/', app=oauth), | 20 | Mount('/oauth/', app=oauth), |
| 19 | WebSocketRoute('/tunnel/', endpoint=tunnel), | 21 | WebSocketRoute( |
| 22 | '/tunnel/', | ||
| 23 | endpoint=tunnel, | ||
| 24 | ), | ||
| 20 | Route('/health/', endpoint=health), | 25 | Route('/health/', endpoint=health), |
| 21 | Host(f'{{subdomain}}.{os.environ['TUNNEL_DOMAIN']}', app=proxy_app) | 26 | Host(f'{{subdomain}}.{os.environ['TUNNEL_DOMAIN']}', app=proxy_app) |
| 22 | ] | 27 | ] |
| 23 | ) | 28 | ) |
| 24 | 29 | ||
| 30 | app.add_middleware(AuthenticationMiddleware, backend=BearerTokenAuthBackend()) | ||
| 31 | |||
| 25 | try: | 32 | try: |
| 26 | from ._version import version | 33 | from ._version import version |
| 27 | __version__ = version | 34 | __version__ = version |
diff --git a/ttun_server/websockets.py b/ttun_server/websockets.py index c791625..88d6cac 100644 --- a/ttun_server/websockets.py +++ b/ttun_server/websockets.py | |||
| @@ -4,7 +4,9 @@ import os | |||
| 4 | from uuid import uuid4 | 4 | from uuid import uuid4 |
| 5 | 5 | ||
| 6 | from fastapi import WebSocket, WebSocketDisconnect | 6 | from fastapi import WebSocket, WebSocketDisconnect |
| 7 | from starlette.authentication import requires | ||
| 7 | 8 | ||
| 9 | import conf | ||
| 8 | import ttun_server | 10 | import ttun_server |
| 9 | from proxy.queue import ProxyQueue | 11 | from proxy.queue import ProxyQueue |
| 10 | from ttun_server.types import ( | 12 | from ttun_server.types import ( |
| @@ -31,6 +33,7 @@ async def assert_compatible_version(websocket: WebSocket, config: Config) -> Non | |||
| 31 | await websocket.close(4001, 'Your client is too new') | 33 | await websocket.close(4001, 'Your client is too new') |
| 32 | 34 | ||
| 33 | 35 | ||
| 36 | @requires(['authenticated', 'token'] if conf.ENABLE_AUTH else []) | ||
| 34 | async def tunnel(websocket: WebSocket) -> None: | 37 | async def tunnel(websocket: WebSocket) -> None: |
| 35 | request_tasks: dict[str, asyncio.Task] = {} | 38 | request_tasks: dict[str, asyncio.Task] = {} |
| 36 | proxy_queues: dict[str, ProxyQueue] = {} | 39 | proxy_queues: dict[str, ProxyQueue] = {} |
