Skip to content

Commit 00237a0

Browse files
committed
Return proper TURN address when requesting credentials from AuthProvider
1 parent 7466ead commit 00237a0

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

lib/ex_turn/auth_provider.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ defmodule ExTURN.AuthProvider do
2323
ip_addr = Application.fetch_env!(:ex_turn, :listen_ip)
2424
port = Application.fetch_env!(:ex_turn, :listen_port)
2525

26+
ip_addr = if(ip_addr == {0, 0, 0, 0}, do: get_public_ip_addr(), else: ip_addr)
27+
2628
response =
2729
Jason.encode!(%{
2830
"username" => username,
@@ -51,4 +53,15 @@ defmodule ExTURN.AuthProvider do
5153
match _ do
5254
send_resp(conn, 400, "invalid request")
5355
end
56+
57+
defp get_public_ip_addr() do
58+
with {:ok, opts} <- :inet.getifaddrs(),
59+
addrs <- Enum.flat_map(opts, fn {_if, opt} -> Keyword.get_values(opt, :addr) end),
60+
addr <- Enum.find(addrs, &match?({a, _, _, _} when a != 127, &1)),
61+
false <- is_nil(addr) do
62+
addr
63+
else
64+
_other -> "AuthProvider listens on 0.0.0.0, but failed to find public IP interface"
65+
end
66+
end
5467
end

lib/ex_turn/listener.ex

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,6 @@ defmodule ExTURN.Listener do
4040
]
4141
)
4242

43-
# in case 0.0.0.0 was used, AuthProvider requires actuall interface address
44-
{:ok, {server_ip, _port}} = :inet.sockname(socket)
45-
Application.put_env(:ex_turn, :listen_ip, server_ip)
46-
4743
spawn(ExTURN.Monitor, :start, [self(), socket])
4844

4945
recv_loop(socket, %{

0 commit comments

Comments
 (0)