diff options
| author | 2023-06-08 23:05:26 +0200 | |
|---|---|---|
| committer | 2023-06-08 23:05:26 +0200 | |
| commit | 53a8f300859a50d9f99f1821c35bca999fced6d8 (patch) | |
| tree | 0243959c8241de1b278609a65d4b337e04a597ac /ttun_server | |
| parent | 7ac28203290a211a6e17ae0b91bc2b609f110514 (diff) | |
| download | server-2.0.0.tar.gz server-2.0.0.tar.bz2 server-2.0.0.zip | |
v2v2.0.0
Diffstat (limited to 'ttun_server')
| -rw-r--r-- | ttun_server/__init__.py | 6 | ||||
| -rw-r--r-- | ttun_server/endpoints.py | 22 | ||||
| -rw-r--r-- | ttun_server/redis.py | 2 | ||||
| -rw-r--r-- | ttun_server/types.py | 1 |
4 files changed, 28 insertions, 3 deletions
diff --git a/ttun_server/__init__.py b/ttun_server/__init__.py index 17a8e7a..81f8cd4 100644 --- a/ttun_server/__init__.py +++ b/ttun_server/__init__.py | |||
| @@ -20,3 +20,9 @@ server = Starlette( | |||
| 20 | Route('/{path:path}', Proxy), | 20 | Route('/{path:path}', Proxy), |
| 21 | ] | 21 | ] |
| 22 | ) | 22 | ) |
| 23 | |||
| 24 | try: | ||
| 25 | from ._version import version | ||
| 26 | __version__ = version | ||
| 27 | except ImportError: | ||
| 28 | __version__ = 'development' | ||
diff --git a/ttun_server/endpoints.py b/ttun_server/endpoints.py index 6728c31..3e263da 100644 --- a/ttun_server/endpoints.py +++ b/ttun_server/endpoints.py | |||
| @@ -1,6 +1,7 @@ | |||
| 1 | import asyncio | 1 | import asyncio |
| 2 | import logging | 2 | import logging |
| 3 | import os | 3 | import os |
| 4 | from asyncio import create_task | ||
| 4 | from base64 import b64decode, b64encode | 5 | from base64 import b64decode, b64encode |
| 5 | from typing import Optional, Any | 6 | from typing import Optional, Any |
| 6 | from uuid import uuid4 | 7 | from uuid import uuid4 |
| @@ -11,6 +12,7 @@ from starlette.responses import Response | |||
| 11 | from starlette.types import Scope, Receive, Send | 12 | from starlette.types import Scope, Receive, Send |
| 12 | from starlette.websockets import WebSocket | 13 | from starlette.websockets import WebSocket |
| 13 | 14 | ||
| 15 | import ttun_server | ||
| 14 | from ttun_server.proxy_queue import ProxyQueue | 16 | from ttun_server.proxy_queue import ProxyQueue |
| 15 | from ttun_server.types import RequestData, Config, Message, MessageType | 17 | from ttun_server.types import RequestData, Config, Message, MessageType |
| 16 | 18 | ||
| @@ -40,9 +42,10 @@ class Proxy(HTTPEndpoint): | |||
| 40 | 42 | ||
| 41 | request_queue = await ProxyQueue.get_for_identifier(subdomain) | 43 | request_queue = await ProxyQueue.get_for_identifier(subdomain) |
| 42 | 44 | ||
| 45 | logger.debug('PROXY %s%s ', subdomain, request.url) | ||
| 43 | await request_queue.enqueue( | 46 | await request_queue.enqueue( |
| 44 | Message( | 47 | Message( |
| 45 | type=MessageType.request, | 48 | type=MessageType.request.value, |
| 46 | identifier=identifier, | 49 | identifier=identifier, |
| 47 | payload= | 50 | payload= |
| 48 | RequestData( | 51 | RequestData( |
| @@ -85,16 +88,31 @@ class Tunnel(WebSocketEndpoint): | |||
| 85 | 88 | ||
| 86 | async def handle_requests(self, websocket: WebSocket): | 89 | async def handle_requests(self, websocket: WebSocket): |
| 87 | while request := await self.proxy_queue.dequeue(): | 90 | while request := await self.proxy_queue.dequeue(): |
| 88 | await websocket.send_json(request) | 91 | create_task(websocket.send_json(request)) |
| 89 | 92 | ||
| 90 | async def on_connect(self, websocket: WebSocket) -> None: | 93 | async def on_connect(self, websocket: WebSocket) -> None: |
| 91 | await websocket.accept() | 94 | await websocket.accept() |
| 92 | self.config = await websocket.receive_json() | 95 | self.config = await websocket.receive_json() |
| 93 | 96 | ||
| 97 | client_version = self.config.get('version', '1.0.0') | ||
| 98 | logger.debug('client_version %s', client_version) | ||
| 99 | |||
| 100 | if 'git' not in client_version and ttun_server.__version__ != 'development': | ||
| 101 | [client_major, *_] = [int(i) for i in client_version.split('.')[:3]] | ||
| 102 | [server_major, *_] = [int(i) for i in ttun_server.__version__.split('.')] | ||
| 103 | |||
| 104 | if client_major < server_major: | ||
| 105 | await websocket.close(4000, 'Your client is too old') | ||
| 106 | |||
| 107 | if client_major > server_major: | ||
| 108 | await websocket.close(4001, 'Your client is too new') | ||
| 109 | |||
| 110 | |||
| 94 | if self.config['subdomain'] is None \ | 111 | if self.config['subdomain'] is None \ |
| 95 | or await ProxyQueue.has_connection(self.config['subdomain']): | 112 | or await ProxyQueue.has_connection(self.config['subdomain']): |
| 96 | self.config['subdomain'] = uuid4().hex | 113 | self.config['subdomain'] = uuid4().hex |
| 97 | 114 | ||
| 115 | |||
| 98 | self.proxy_queue = await ProxyQueue.create_for_identifier(self.config['subdomain']) | 116 | self.proxy_queue = await ProxyQueue.create_for_identifier(self.config['subdomain']) |
| 99 | 117 | ||
| 100 | hostname = os.environ.get("TUNNEL_DOMAIN") | 118 | hostname = os.environ.get("TUNNEL_DOMAIN") |
diff --git a/ttun_server/redis.py b/ttun_server/redis.py index 344c107..3065dec 100644 --- a/ttun_server/redis.py +++ b/ttun_server/redis.py | |||
| @@ -3,7 +3,7 @@ import os | |||
| 3 | from aioredis import ConnectionPool, Redis | 3 | from aioredis import ConnectionPool, Redis |
| 4 | 4 | ||
| 5 | 5 | ||
| 6 | class RedisConnectionPool(): | 6 | class RedisConnectionPool: |
| 7 | instance: 'RedisConnectionPool' = None | 7 | instance: 'RedisConnectionPool' = None |
| 8 | 8 | ||
| 9 | def __init__(self): | 9 | def __init__(self): |
diff --git a/ttun_server/types.py b/ttun_server/types.py index 2f1959f..8a4d929 100644 --- a/ttun_server/types.py +++ b/ttun_server/types.py | |||
| @@ -10,6 +10,7 @@ class MessageType(Enum): | |||
| 10 | 10 | ||
| 11 | class Config(TypedDict): | 11 | class Config(TypedDict): |
| 12 | subdomain: str | 12 | subdomain: str |
| 13 | client_version: str | ||
| 13 | 14 | ||
| 14 | 15 | ||
| 15 | class RequestData(TypedDict): | 16 | class RequestData(TypedDict): |
