Skip to content

Commit 8f08fbb

Browse files
committed
Allow to set allocation port range via env variable
1 parent 94925ea commit 8f08fbb

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,13 @@ RELAY_IP=0.0.0.0
8888
EXTERNAL_RELAY_IP=167.235.241.140
8989
```
9090

91+
Rel will try to open relay addresses in `49_152 - 65_535` port range, but this can be changed. `RELAY_PORT_END` must be greater than `RELAY_PORT_START`.
92+
93+
```console
94+
RELAY_PORT_START=35000
95+
RELAY_PORT_END=45000
96+
```
97+
9198
Remember to use the `REALM` variable specific to your deployment. It's used in `REALM` STUN attributes. See
9299
[this section of RFC 2617](https://datatracker.ietf.org/doc/html/rfc2617#section-3.2.1) to learn about appropriate values for `REALM` attribute.
93100

@@ -96,6 +103,7 @@ REALM=my-amazing-turn.com
96103
```
97104

98105
You can configure the number of running `listener` processes. By default, it is equal to number of running Erlang VM schedulers:
106+
99107
```console
100108
LISTENER_COUNT=8
101109
```

config/config.exs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import Config
22

3-
config :rel,
4-
alloc_port_range: 49_152..65_535
5-
6-
# various lifetimes
73
config :rel,
84
# 1 day in seconds, see https://datatracker.ietf.org/doc/html/draft-uberti-rtcweb-turn-rest-00#section-2.2
95
credentials_lifetime: 24 * 60 * 60,

config/runtime.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,9 @@ config :rel,
133133
relay_ip: relay_ip,
134134
external_relay_ip: external_relay_ip,
135135
listen_port: System.get_env("LISTEN_PORT", "3478") |> ConfigUtils.parse_port(),
136-
realm: System.get_env("REALM", "example.com")
136+
realm: System.get_env("REALM", "example.com"),
137+
relay_port_start: System.get_env("RELAY_PORT_START", "49152") |> ConfigUtils.parse_port(),
138+
relay_port_end: System.get_env("RELAY_PORT_END", "65535") |> ConfigUtils.parse_port()
137139

138140
# Metrics endpoint configuration
139141
config :rel,

lib/rel/listener.ex

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ defmodule Rel.Listener do
2222
alias ExSTUN.Message.Attribute.{Username, XORMappedAddress}
2323

2424
@buf_size 2 * 1024 * 1024
25-
@default_alloc_ports MapSet.new(Application.compile_env!(:rel, :alloc_port_range))
2625

2726
@spec start_link(term()) :: {:ok, pid()}
2827
def start_link(args) do
@@ -322,7 +321,10 @@ defmodule Rel.Listener do
322321
|> Enum.map(fn alloc_origin_state -> Map.fetch!(alloc_origin_state, :alloc_port) end)
323322
|> MapSet.new()
324323

325-
available_alloc_ports = MapSet.difference(@default_alloc_ports, used_alloc_ports)
324+
relay_port_start = Application.fetch_env!(:rel, :relay_port_start)
325+
relay_port_end = Application.fetch_env!(:rel, :relay_port_end)
326+
default_alloc_ports = MapSet.new(relay_port_start..relay_port_end)
327+
available_alloc_ports = MapSet.difference(default_alloc_ports, used_alloc_ports)
326328

327329
if MapSet.size(available_alloc_ports) == 0 do
328330
{:error, :out_of_ports}

0 commit comments

Comments
 (0)