The first address for a shortname must be the one that matches

This commit is contained in:
José Valim 2021-10-29 16:29:21 +02:00
parent 0cdfe8b185
commit 8a0d218cbe

View file

@ -87,8 +87,9 @@ defmodule Livebook.Application do
{:error, :nxdomain} ->
invalid_hostname!("your hostname \"#{hostname}\" does not resolve to an IP address")
{:ok, hostent(h_addrtype: :inet, h_addr_list: addresses)} ->
unless any_loopback?(addresses) or any_if?(addresses) do
# We only try the first address, so that's the one we validate.
{:ok, hostent(h_addrtype: :inet, h_addr_list: [address | _])} ->
unless inet_loopback?(address) or inet_if?(address) do
invalid_hostname!(
"your hostname \"#{hostname}\" does not resolve to a loopback address (127.0.0.0/8)"
)
@ -100,19 +101,14 @@ defmodule Livebook.Application do
end
end
defp any_loopback?(addresses) do
Enum.any?(addresses, &match?({127, _, _, _}, &1))
defp inet_loopback?(address) do
match?({127, _, _, _}, address)
end
defp any_if?(addresses) do
defp inet_if?(address) do
case :inet.getifaddrs() do
{:ok, addrs} ->
Enum.any?(addrs, fn {_name, flags} ->
Enum.any?(flags, fn {key, value} -> key == :addr and value in addresses end)
end)
_ ->
false
{:ok, addrs} -> Enum.any?(addrs, fn {_name, flags} -> {:addr, address} in flags end)
_ -> false
end
end