defmodule SNet.Addr do
use Agent
require Logger
def start_link(_) do
Agent.start_link(__MODULE__, :init, [], name: __MODULE__)
end
def init() do
Application.ensure_all_started(:inets)
Application.ensure_all_started(:ssl)
case :httpc.request('http://api.ipify.org') do
{:ok, {_, _, body}} ->
{:ok, addr} = :inet.parse_address body
Logger.info "Public IP address: #{body}"
[addr]
_ ->
Logger.info "Could not get public IP address"
[]
end
end
def get_if_inet4 do
{:ok, ifs} = :inet.getifaddrs
for {_, opts} <- ifs,
{:addr, addr} <- opts,
tuple_size(addr) == 4,
addr != {127,0,0,1}
do
addr
end
end
def get_pub_inet4 do
Agent.get(__MODULE__, &(&1))
end
def get_all_inet4 do
addrset = for x <- get_if_inet4() ++ get_pub_inet4(), into: %MapSet{}, do: x
MapSet.to_list addrset
end
def is_local?({:inet, ip, port}) do
port == Application.get_env(:shard, :port) and (ip == {127,0,0,1} or ip in get_all_inet4())
end
end