From fc934029c5e04c32606bf89de4201ad9c70141e9 Mon Sep 17 00:00:00 2001 From: GrassCarp_ Date: Sat, 23 Aug 2025 15:48:27 +0800 Subject: [PATCH 1/2] add ipv6 support --- Dockerfile | 2 +- app/turnify.py | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index a399c96..b941f97 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ ENV PYTHONUNBUFFERED=1 ENV FLASK_ENV=production ENV LOG_LEVEL=info -CMD ["sh", "-c", "gunicorn -b 0.0.0.0:4499 turnify:app \ +CMD ["sh", "-c", "gunicorn -b [::]:4499 turnify:app \ --log-level $(echo $LOG_LEVEL | tr '[:upper:]' '[:lower:]') \ --access-logfile ${GUNICORN_ACCESS_LOG:-'-'} \ --error-logfile ${GUNICORN_ERROR_LOG:-'-'}"] \ No newline at end of file diff --git a/app/turnify.py b/app/turnify.py index 558f049..f1f7cb4 100644 --- a/app/turnify.py +++ b/app/turnify.py @@ -23,7 +23,7 @@ @app.before_request def log_request(): app.logger.debug(f"Received {request.method} request to {request.path} from {request.remote_addr}") - + @app.route('//voip/turnServer', methods=['GET']) def proxy_request(subpath): app.logger.debug(f"Handling {request.method} request for {request.path}") @@ -126,10 +126,10 @@ def generate(): status=response.status_code, headers={key: value for key, value in response.headers.items() if key.lower() != 'transfer-encoding'} ) - + except requests.exceptions.RequestException as e: app.logger.error(f"{ip_addr} -- Error forwarding request to Synapse: {e}") return jsonify({"error": "Failed to connect to Synapse"}), 502 if __name__ == "__main__": - app.run(host="0.0.0.0", port=4499) + app.run(host="::", port=4499) From e3decb0969bcd8ac3f6c089cc57eefc2e499c71c Mon Sep 17 00:00:00 2001 From: GrassCarp_ Date: Sat, 23 Aug 2025 17:24:16 +0800 Subject: [PATCH 2/2] auto detect ipv4-mapped ipv6 --- Dockerfile | 8 ++++---- app/turnify.py | 2 +- entrypoint.sh | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 entrypoint.sh diff --git a/Dockerfile b/Dockerfile index b941f97..6af3e65 100644 --- a/Dockerfile +++ b/Dockerfile @@ -10,7 +10,7 @@ ENV PYTHONUNBUFFERED=1 ENV FLASK_ENV=production ENV LOG_LEVEL=info -CMD ["sh", "-c", "gunicorn -b [::]:4499 turnify:app \ - --log-level $(echo $LOG_LEVEL | tr '[:upper:]' '[:lower:]') \ - --access-logfile ${GUNICORN_ACCESS_LOG:-'-'} \ - --error-logfile ${GUNICORN_ERROR_LOG:-'-'}"] \ No newline at end of file +COPY entrypoint.sh /app/entrypoint.sh +RUN chmod +x /app/entrypoint.sh + +CMD ["/app/entrypoint.sh"] \ No newline at end of file diff --git a/app/turnify.py b/app/turnify.py index f1f7cb4..894f0ec 100644 --- a/app/turnify.py +++ b/app/turnify.py @@ -132,4 +132,4 @@ def generate(): return jsonify({"error": "Failed to connect to Synapse"}), 502 if __name__ == "__main__": - app.run(host="::", port=4499) + app.run(host="0.0.0.0", port=4499) diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..0dd1880 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,37 @@ +#!/bin/sh + +python3 - <<'EOF' +import socket, sys +try: + s = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) + s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) + s.bind(("::", 4499)) + s.listen(1) + + test = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + test.connect(("127.0.0.1", 4499)) + sys.exit(0) + except Exception: + sys.exit(1) + finally: + test.close() +except Exception: + sys.exit(1) +finally: + s.close() +EOF + +if [ $? -eq 0 ]; then + echo "Use IPv4-mapped IPv6" + exec gunicorn -w 4 -b [::]:4499 turnify:app \ + --log-level "$(echo $LOG_LEVEL | tr '[:upper:]' '[:lower:]')" \ + --access-logfile "${GUNICORN_ACCESS_LOG:--}" \ + --error-logfile "${GUNICORN_ERROR_LOG:--}" +else + echo "Use manual IPv4 and IPv6 binding" + exec gunicorn -w 4 -b 0.0.0.0:4499 -b [::]:4499 turnify:app \ + --log-level "$(echo $LOG_LEVEL | tr '[:upper:]' '[:lower:]')" \ + --access-logfile "${GUNICORN_ACCESS_LOG:--}" \ + --error-logfile "${GUNICORN_ERROR_LOG:--}" +fi \ No newline at end of file