Skip to content

Commit

Permalink
Merge pull request #17 from toverainc/rk/ports
Browse files Browse the repository at this point in the history
Ephemeral port range control
  • Loading branch information
kristiankielhofner authored Mar 2, 2023
2 parents 9030419 + 2a58b9d commit f174628
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
34 changes: 33 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
from fastapi.encoders import jsonable_encoder
from fastapi.responses import JSONResponse, FileResponse
from fastapi.staticfiles import StaticFiles
from typing import List, Optional
from typing import List, Optional, Tuple
import types
import random
import datetime
import numpy as np
from PIL import Image
Expand Down Expand Up @@ -51,6 +53,36 @@ async def new_handle_rtcp_packet(self, packet):
RTCRtpReceiver._handle_rtcp_packet = new_handle_rtcp_packet
#logging.basicConfig(level=logging.DEBUG) #very useful debugging aiortc issues

# Monkey patch aiortc to control ephemeral ports
local_ports = list(range(60000, 60000+100)) # Allowed ephemeral port range
loop = asyncio.get_event_loop()
old_create_datagram_endpoint = loop.create_datagram_endpoint
async def create_datagram_endpoint(self, protocol_factory,
local_addr: Tuple[str, int] = None,
**kwargs,
):
#if port is specified just use it
if local_addr and local_addr[1]:
return await old_create_datagram_endpoint(protocol_factory, local_addr=local_addr, **kwargs)
if local_addr is None:
return await old_create_datagram_endpoint(protocol_factory, local_addr=None, **kwargs)
#if port is not specified make it use our range
ports = list(local_ports)
random.shuffle(ports)
for port in ports:
try:
ret = await old_create_datagram_endpoint(
protocol_factory, local_addr=(local_addr[0], port), **kwargs
)
print('create_datagram_endpoint chose port', port)
return ret
except OSError as exc:
if port == ports[-1]:
# this was the last port, give up
raise exc
raise ValueError("local_ports must not be empty")
loop.create_datagram_endpoint = types.MethodType(create_datagram_endpoint, loop)

# default return language
return_language = "en"

Expand Down
4 changes: 2 additions & 2 deletions run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ else
export CUDA_VISIBLE_DEVICES="0"
fi

# -p 19000:8000 -p 8081:8080
# -p "$PORT":8000 -p 60000-60100:60000-60100/udp
docker run --rm -it --gpus all --shm-size=1g --ipc=host \
-v $PWD:/app -v $PWD/cache:/root/.cache -e CUDA_VISIBLE_DEVICES -e WEB_CONCURRENCY \
--name air-infer-api \
--net host air-infer-api:latest \
-p "$PORT":"$PORT" -p 60000-60100:60000-60100/udp air-infer-api:latest \
uvicorn main:app --host 0.0.0.0 --port "$PORT" --reload --ssl-keyfile="/app/key.pem" --ssl-certfile="/app/cert.pem"

0 comments on commit f174628

Please sign in to comment.