From 8f08fbb8725afbb18af19cce594b35a8ef50b93c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Wala?= Date: Mon, 21 Aug 2023 17:12:35 +0200 Subject: [PATCH] Allow to set allocation port range via env variable --- README.md | 8 ++++++++ config/config.exs | 4 ---- config/runtime.exs | 4 +++- lib/rel/listener.ex | 6 ++++-- 4 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 29f9dc2..16c7c7a 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,13 @@ RELAY_IP=0.0.0.0 EXTERNAL_RELAY_IP=167.235.241.140 ``` +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`. + +```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. @@ -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 ``` diff --git a/config/config.exs b/config/config.exs index e3e2ffa..7b54f26 100644 --- a/config/config.exs +++ b/config/config.exs @@ -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, diff --git a/config/runtime.exs b/config/runtime.exs index d23f761..2235cd0 100644 --- a/config/runtime.exs +++ b/config/runtime.exs @@ -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, diff --git a/lib/rel/listener.ex b/lib/rel/listener.ex index 75d804a..f8d9b91 100644 --- a/lib/rel/listener.ex +++ b/lib/rel/listener.ex @@ -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 @@ -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}