Current section

Files

Jump to
phoenix_ddos lib phoenix_ddos zzz ip.ex_
Raw

lib/phoenix_ddos/zzz/ip.ex_

defmodule PhoenixDDoS.Ip do
@moduledoc """
helper with ip/subnet match
## Examples
iex> IO.puts(1)
:ok
ip erlang
references
https://www.ibm.com/docs/en/ts3500-tape-library?topic=formats-subnet-masks-ipv4-prefixes-ipv6
"""
use Bitwise, only_operators: true
# {{sa, sb, sc, sd}, netmask}
# ipv4
def ip_in_subnet?({a, b, c, d}, subnet_mask) do
end
# ipv6
def ip_in_subnet?({a, b, c, d, _, _, _, _}, {a, b, c, d}) do
end
def ip_in_subnet?({_, _, _, _, _, _, _, _}, {_, _, _, _}), do: false
def in_safelist?(ip) do
# safelist
end
# "1.2.3.4/24"
def netmask!(subnet) do
[ip, range] = subnet |> String.split("/")
with [ip, range] <- subnet |> String.split("/"),
{range, ""} <- Integer.parse(range),
true <- range in 0..32 do
case ip do
# v4
{a, b, c, d} ->
a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d &&& 1
# v6
{a, b, c, d, e, f, g, h} ->
nil
end
else
raise "invalid subnet #{subnet}"
end
end
def in_range(ip, ip, range) do
end
def in_blocklist?(ip) do
# safelist
end
# <<f::binary-size(1), _::binary>>
end
# {0..65535,
# 0..65535,
# 0..65535,
# 0..65535,
# 0..65535,
# 0..65535,
# 0..65535,
# 0..65535}
# moduledoc toussa