diff --git a/Dockerfile b/Dockerfile index a399c96..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 0.0.0.0: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 558f049..894f0ec 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,7 +126,7 @@ 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 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