Skip to content

Commit

Permalink
Return proper TURN address when requesting credentials from AuthProvider
Browse files Browse the repository at this point in the history
  • Loading branch information
LVala committed Jul 27, 2023
1 parent 7466ead commit 00237a0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
13 changes: 13 additions & 0 deletions lib/ex_turn/auth_provider.ex
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ defmodule ExTURN.AuthProvider do
ip_addr = Application.fetch_env!(:ex_turn, :listen_ip)
port = Application.fetch_env!(:ex_turn, :listen_port)

ip_addr = if(ip_addr == {0, 0, 0, 0}, do: get_public_ip_addr(), else: ip_addr)

response =
Jason.encode!(%{
"username" => username,
Expand Down Expand Up @@ -51,4 +53,15 @@ defmodule ExTURN.AuthProvider do
match _ do
send_resp(conn, 400, "invalid request")
end

defp get_public_ip_addr() do
with {:ok, opts} <- :inet.getifaddrs(),
addrs <- Enum.flat_map(opts, fn {_if, opt} -> Keyword.get_values(opt, :addr) end),
addr <- Enum.find(addrs, &match?({a, _, _, _} when a != 127, &1)),
false <- is_nil(addr) do
addr
else
_other -> "AuthProvider listens on 0.0.0.0, but failed to find public IP interface"
end
end
end
4 changes: 0 additions & 4 deletions lib/ex_turn/listener.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ defmodule ExTURN.Listener do
]
)

# in case 0.0.0.0 was used, AuthProvider requires actuall interface address
{:ok, {server_ip, _port}} = :inet.sockname(socket)
Application.put_env(:ex_turn, :listen_ip, server_ip)

spawn(ExTURN.Monitor, :start, [self(), socket])

recv_loop(socket, %{
Expand Down

0 comments on commit 00237a0

Please sign in to comment.