-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Samuel
committed
Jun 14, 2022
1 parent
0ac4cf8
commit fb15f07
Showing
13 changed files
with
77 additions
and
1,385 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,24 @@ | ||
defmodule Archethic.Networking.IPLookup.NATDiscovery do | ||
@moduledoc """ | ||
Provide abstraction over :natupnp_v1, :natupnp_v2, :natpmp | ||
Provide implementation to discover ip address using NAT | ||
""" | ||
|
||
alias Archethic.Networking.IPLookup.Impl | ||
|
||
alias __MODULE__.UPnPv1 | ||
alias __MODULE__.UPnPv2 | ||
alias __MODULE__.PMP | ||
|
||
require Logger | ||
|
||
@behaviour Impl | ||
def get_node_ip() do | ||
provider = provider() | ||
do_get_node_ip(provider) | ||
end | ||
|
||
defp do_get_node_ip(provider) do | ||
case provider.get_node_ip() do | ||
{:ok, ip} -> | ||
{:ok, ip} | ||
|
||
{:error, reason} -> | ||
Logger.error( | ||
"Cannot use the provider #{provider} for IP Lookup - reason: #{inspect(reason)}" | ||
) | ||
|
||
fallback(provider, reason) | ||
end | ||
end | ||
|
||
defp fallback(UPnPv1, _reason) do | ||
do_get_node_ip(UPnPv2) | ||
end | ||
|
||
defp fallback(UPnPv2, _reason) do | ||
do_get_node_ip(PMP) | ||
end | ||
|
||
defp fallback(PMP, reason) do | ||
{:error, reason} | ||
@spec get_node_ip() :: {:ok, :inet.ip_address()} | {:error, any()} | ||
def get_node_ip do | ||
provider().get_node_ip() | ||
end | ||
|
||
defp fallback(_provider, reason) do | ||
{:error, reason} | ||
@spec open_port(non_neg_integer()) :: :ok | :error | ||
def open_port(port) do | ||
provider().open_port(port) | ||
end | ||
|
||
defp provider() do | ||
defp provider do | ||
:archethic | ||
|> Application.get_env(__MODULE__, []) | ||
|> Keyword.get(:provider, UPnPv1) | ||
|> Keyword.get(:provider, __MODULE__.MiniUPNP) | ||
end | ||
end |
65 changes: 65 additions & 0 deletions
65
lib/archethic/networking/ip_lookup/nat_discovery/miniupnp.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
defmodule Archethic.Networking.IPLookup.NATDiscovery.MiniUPNP do | ||
@moduledoc false | ||
|
||
require Logger | ||
|
||
@upnpc Application.app_dir(:archethic, "priv/c_dist/upnpc") | ||
|
||
@spec get_node_ip() :: {:ok, :inet.ip_address()} | {:error, any()} | ||
def get_node_ip do | ||
case System.cmd(@upnpc, ["-s"]) do | ||
{output, 0} -> | ||
[[_, ip]] = Regex.scan(~r/ExternalIPAddress = ([0-9.]*)/, output, capture: :all) | ||
|
||
ip | ||
|> to_charlist() | ||
|> :inet.parse_address() | ||
|
||
{_, status} -> | ||
{:error, status} | ||
end | ||
end | ||
|
||
@spec open_port(non_neg_integer()) :: :ok | :error | ||
def open_port(port) do | ||
# {:ok, [{local_ip, _, _} | _]} = :inet.getif() | ||
|
||
local_ip = | ||
case System.cmd(@upnpc, ["-s"]) do | ||
{output, 0} -> | ||
[[_, ip]] = Regex.scan(~r/Local LAN ip address : ([0-9.]*)/, output, capture: :all) | ||
|
||
ip | ||
|> to_charlist() | ||
|> :inet.parse_address() | ||
|> elem(1) | ||
|
||
{_, status} -> | ||
{:error, status} | ||
end | ||
|
||
opts = [ | ||
# Add redirection | ||
"-a", | ||
# Local ip | ||
local_ip |> :inet.ntoa() |> to_string(), | ||
# Local opened port | ||
"#{port}", | ||
# Remote port to open | ||
"#{port}", | ||
# Protocol | ||
"tcp", | ||
# Lifetime | ||
"0" | ||
] | ||
|
||
case System.cmd(@upnpc, opts) do | ||
{_, 0} -> | ||
{:ok, port} | ||
|
||
{reason, _status} -> | ||
Logger.debug(reason) | ||
:error | ||
end | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
lib/archethic/networking/ip_lookup/nat_discovery/upnp_v1.ex
This file was deleted.
Oops, something went wrong.
14 changes: 0 additions & 14 deletions
14
lib/archethic/networking/ip_lookup/nat_discovery/upnp_v2.ex
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.