diff options
| -rw-r--r-- | .github/workflows/docker-image.yml | 6 | ||||
| -rw-r--r-- | Dockerfile | 3 | ||||
| -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 |
6 files changed, 35 insertions, 5 deletions
diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 8945bfb..3f83351 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml | |||
| @@ -16,7 +16,7 @@ jobs: | |||
| 16 | uses: actions/checkout@v2 | 16 | uses: actions/checkout@v2 |
| 17 | - name: Docker meta | 17 | - name: Docker meta |
| 18 | id: meta | 18 | id: meta |
| 19 | uses: docker/metadata-action@v3 | 19 | uses: docker/metadata-action@v4 |
| 20 | with: | 20 | with: |
| 21 | images: ghcr.io/tomvanderlee/ttun-server | 21 | images: ghcr.io/tomvanderlee/ttun-server |
| 22 | tags: | | 22 | tags: | |
| @@ -31,9 +31,11 @@ jobs: | |||
| 31 | username: ${{ github.actor }} | 31 | username: ${{ github.actor }} |
| 32 | password: ${{ secrets.GITHUB_TOKEN }} | 32 | password: ${{ secrets.GITHUB_TOKEN }} |
| 33 | - name: Build and push | 33 | - name: Build and push |
| 34 | uses: docker/build-push-action@v2 | 34 | uses: docker/build-push-action@v4 |
| 35 | with: | 35 | with: |
| 36 | context: . | 36 | context: . |
| 37 | push: ${{ github.event_name != 'pull_request' }} | 37 | push: ${{ github.event_name != 'pull_request' }} |
| 38 | tags: ${{ steps.meta.outputs.tags }} | 38 | tags: ${{ steps.meta.outputs.tags }} |
| 39 | labels: ${{ steps.meta.outputs.labels }} | 39 | labels: ${{ steps.meta.outputs.labels }} |
| 40 | build-args: | | ||
| 41 | DOCKER_METADATA_OUTPUT_VERSION | ||
| @@ -15,7 +15,10 @@ RUN pip install -r requirements.txt --root /buildroot | |||
| 15 | FROM base | 15 | FROM base |
| 16 | 16 | ||
| 17 | COPY --from=build /buildroot / | 17 | COPY --from=build /buildroot / |
| 18 | |||
| 18 | COPY . . | 19 | COPY . . |
| 20 | ARG DOCKER_METADATA_OUTPUT_VERSION | ||
| 21 | RUN echo "version='$DOCKER_METADATA_OUTPUT_VERSION'" > ttun_server/_version.py | ||
| 19 | 22 | ||
| 20 | ENV TUNNEL_DOMAIN= | 23 | ENV TUNNEL_DOMAIN= |
| 21 | ENV SECURE True | 24 | ENV SECURE True |
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): |
