Skip to content

Commit

Permalink
Allow to set allocation port range via env variable
Browse files Browse the repository at this point in the history
  • Loading branch information
LVala committed Aug 21, 2023
1 parent 94925ea commit 4787e0e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ RELAY_IP=0.0.0.0
EXTERNAL_RELAY_IP=167.235.241.140
```

Rel will try to open relay addresse in `49_152 - 65_535` port range, but this can be changed. `RELAY_PORT_END` must be greater than `RELAY_PORT_START`.

```console
RELAY_PORT_START=35000
RELAY_PORT_END=45000
```

Remember to use the `REALM` variable specific to your deployment. It's used in `REALM` STUN attributes. See
[this section of RFC 2617](https://datatracker.ietf.org/doc/html/rfc2617#section-3.2.1) to learn about appropriate values for `REALM` attribute.

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

You can configure the number of running `listener` processes. By default, it is equal to number of running Erlang VM schedulers:

```console
LISTENER_COUNT=8
```
Expand Down
4 changes: 0 additions & 4 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import Config

config :rel,
alloc_port_range: 49_152..65_535

# various lifetimes
config :rel,
# 1 day in seconds, see https://datatracker.ietf.org/doc/html/draft-uberti-rtcweb-turn-rest-00#section-2.2
credentials_lifetime: 24 * 60 * 60,
Expand Down
4 changes: 3 additions & 1 deletion config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ config :rel,
relay_ip: relay_ip,
external_relay_ip: external_relay_ip,
listen_port: System.get_env("LISTEN_PORT", "3478") |> ConfigUtils.parse_port(),
realm: System.get_env("REALM", "example.com")
realm: System.get_env("REALM", "example.com"),
relay_port_start: System.get_env("RELAY_PORT_START", "49152") |> ConfigUtils.parse_port(),
relay_port_end: System.get_env("RELAY_PORT_END", "65535") |> ConfigUtils.parse_port()

# Metrics endpoint configuration
config :rel,
Expand Down
6 changes: 4 additions & 2 deletions lib/rel/listener.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ defmodule Rel.Listener do
alias ExSTUN.Message.Attribute.{Username, XORMappedAddress}

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

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

available_alloc_ports = MapSet.difference(@default_alloc_ports, used_alloc_ports)
relay_port_start = Application.fetch_env!(:rel, :relay_port_start)
relay_port_end = Application.fetch_env!(:rel, :relay_port_end)
default_alloc_ports = MapSet.new(relay_port_start..relay_port_end)
available_alloc_ports = MapSet.difference(default_alloc_ports, used_alloc_ports)

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

0 comments on commit 4787e0e

Please sign in to comment.