Skip to content

Commit

Permalink
reserve in use tunnel interface ips
Browse files Browse the repository at this point in the history
  • Loading branch information
mobileoverlord committed Jul 3, 2024
1 parent 9dafded commit d92e5bf
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
13 changes: 12 additions & 1 deletion lib/peridio/rat/network.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
defmodule Peridio.RAT.Network do
alias Peridio.RAT.Tunnel
alias Peridio.RAT.Network.CIDR

# RFC 1918 - Private Address Space
Expand Down Expand Up @@ -46,7 +47,7 @@ defmodule Peridio.RAT.Network do
case :inet.getifaddrs() do
{:ok, addrs} ->
resp =
Enum.reduce(addrs, [], fn inets_interface, acc ->
Enum.reduce(addrs, tunnel_interface_cidrs(), fn inets_interface, acc ->
case CIDR.from_inets_interface(inets_interface) do
{:ok, cidr} -> [cidr | acc]
_e -> acc
Expand All @@ -60,6 +61,16 @@ defmodule Peridio.RAT.Network do
end
end

def tunnel_interface_cidrs() do
Peridio.RAT.DynamicSupervisor
|> DynamicSupervisor.which_children()
|> Enum.map(&elem(&1, 1))
|> Enum.map(&Tunnel.get_state/1)
|> Enum.map(& &1.interface.ip_address.address)
|> Enum.map(&CIDR.from_ip_range(&1..&1))
|> List.flatten()
end

def reserved_ports(port_start..port_end//_) do
case System.cmd("ss", [
"-tauH",
Expand Down
2 changes: 1 addition & 1 deletion lib/peridio/rat/network/cidr.ex
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ defmodule Peridio.RAT.Network.CIDR do
end

def from_ip_range(_, acc \\ [])
def from_ip_range(s_ip..s_ip//_ = range, _), do: [do_from_ip_range(range)]
def from_ip_range(s_ip..e_ip//_ = range, []) when s_ip == e_ip, do: [do_from_ip_range(range)]
def from_ip_range(s_ip..e_ip//_, acc) when s_ip >= e_ip, do: acc

def from_ip_range(_ip_start..ip_end//_ = range, acc) do
Expand Down
8 changes: 8 additions & 0 deletions lib/peridio/rat/tunnel.ex
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ defmodule Peridio.RAT.Tunnel do
}
end

def get_state(pid) do
GenServer.call(pid, :get_state)
end

def start_link(%State{} = state) do
GenServer.start_link(__MODULE__, state, name: generate_via_tuple(state.id, state.interface))
end
Expand Down Expand Up @@ -119,6 +123,10 @@ defmodule Peridio.RAT.Tunnel do
end
end

def handle_call(:get_state, _from, state) do
{:reply, state, state}
end

def handle_info(:ttl_timeout, state) do
# Just stop the process here and rely on the terminate callback to do the work.
{:stop, :normal, state}
Expand Down

0 comments on commit d92e5bf

Please sign in to comment.