Skip to content

Commit

Permalink
Allow passing hostname in JF_WEBRTC_TURN_LISTEN_IP (fishjam-dev#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgfn authored May 9, 2024
1 parent 1d7db2a commit 5a96434
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
38 changes: 20 additions & 18 deletions lib/jellyfish/config_reader.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ defmodule Jellyfish.ConfigReader do
end
end

def read_and_resolve_hostname(env) do
if value = System.get_env(env) do
# resolve_hostname will raise if address is invalid/unresolvable
{:ok, resolved_ip} = value |> resolve_hostname() |> to_charlist() |> :inet.parse_address()

resolved_ip
end
end

def read_port(env) do
if value = System.get_env(env) do
case Integer.parse(value) do
Expand Down Expand Up @@ -104,7 +113,7 @@ defmodule Jellyfish.ConfigReader do
[
webrtc_used?: true,
turn_ip: read_ip("JF_WEBRTC_TURN_IP") || {127, 0, 0, 1},
turn_listen_ip: read_ip("JF_WEBRTC_TURN_LISTEN_IP") || {127, 0, 0, 1},
turn_listen_ip: read_and_resolve_hostname("JF_WEBRTC_TURN_LISTEN_IP") || {127, 0, 0, 1},
turn_port_range: read_port_range("JF_WEBRTC_TURN_PORT_RANGE") || {50_000, 59_999},
turn_tcp_port: read_port("JF_WEBRTC_TURN_TCP_PORT")
]
Expand Down Expand Up @@ -256,7 +265,8 @@ defmodule Jellyfish.ConfigReader do
end

defp do_read_dns_config(node_name_value, cookie, mode) do
node_name = parse_node_name(node_name_value)
# Verify the node name is formatted correctly
_node_name = parse_node_name(node_name_value)

query_value = System.get_env("JF_DIST_QUERY")

Expand All @@ -265,21 +275,8 @@ defmodule Jellyfish.ConfigReader do
end

[node_basename, hostname | []] = String.split(node_name_value, "@")

node_name =
if ip_address?(hostname) do
node_name
else
Logger.info(
"Resolving hostname part of JF node name as DNS cluster strategy requires IP address."
)

resolved_hostname = resolve_hostname(hostname)

Logger.info("Resolved #{hostname} as #{resolved_hostname}")

String.to_atom("#{node_basename}@#{resolved_hostname}")
end
resolved_hostname = resolve_hostname(hostname)
node_name = String.to_atom("#{node_basename}@#{resolved_hostname}")

polling_interval = parse_polling_interval()

Expand Down Expand Up @@ -344,7 +341,12 @@ defmodule Jellyfish.ConfigReader do
# Assert there is at least one ip address.
# In other case, this is fatal error
[h | _] = h_addr_list
"#{:inet.ntoa(h)}"
resolved_hostname = "#{:inet.ntoa(h)}"

if resolved_hostname != hostname,
do: Logger.info("Resolved #{hostname} as #{resolved_hostname}")

resolved_hostname

{:error, reason} ->
raise """
Expand Down
23 changes: 23 additions & 0 deletions test/jellyfish/config_reader_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ defmodule Jellyfish.ConfigReaderTest do
end
end

test "read_and_resolve_hostname/1" do
env_name = "JF_CONF_READER_TEST_HOSTNAME"

with_env env_name do
System.put_env(env_name, "127.0.0.1")
assert ConfigReader.read_and_resolve_hostname(env_name) == {127, 0, 0, 1}

# On most systems, both of these hostnames will resolve to {127, 0, 0, 1}
# However, since we can't expect this to always be true,
# let's settle for asserting that these calls return and not raise an error
System.put_env(env_name, "localhost")
assert ConfigReader.read_and_resolve_hostname(env_name)
{:ok, hostname} = :inet.gethostname()
System.put_env(env_name, "#{hostname}")
assert ConfigReader.read_and_resolve_hostname(env_name)

System.delete_env(env_name)
assert ConfigReader.read_and_resolve_hostname(env_name) == nil
System.put_env(env_name, "unresolvable-hostname")
assert_raise RuntimeError, fn -> ConfigReader.read_and_resolve_hostname(env_name) end
end
end

test "read_port/1" do
env_name = "JF_CONF_READER_TEST_PORT"

Expand Down

0 comments on commit 5a96434

Please sign in to comment.