Skip to content

Commit df87fa9

Browse files
committed
fix stun
1 parent 120d028 commit df87fa9

2 files changed

Lines changed: 15 additions & 11 deletions

File tree

ex/config/runtime.exs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ config :ama, :offline, (!!System.get_env("OFFLINE") || nil)
2020
config :ama, :http_ipv4, ((System.get_env("HTTP_IPV4") || "0.0.0.0") |> :unicode.characters_to_list() |> :inet.parse_ipv4_address() |> (case do {:ok, addr}-> addr end))
2121
config :ama, :http_port, (System.get_env("HTTP_PORT") || "80") |> :erlang.binary_to_integer()
2222

23-
config :ama, :udp_ipv4_tuple, ((System.get_env("UDP_IPV4") || "0.0.0.0") |> :unicode.characters_to_list() |> :inet.parse_ipv4_address() |> (case do {:ok, addr}-> addr end))
23+
udp_ipv4_iface = ((System.get_env("UDP_IPV4") || "0.0.0.0") |> :unicode.characters_to_list() |> :inet.parse_ipv4_address() |> (case do {:ok, addr}-> addr end))
24+
config :ama, :udp_ipv4_tuple, udp_ipv4_iface
2425
config :ama, :udp_port, 36969
2526

2627
#Nodes
@@ -53,7 +54,7 @@ config :ama, :autoupdate, System.get_env("AUTOUPDATE") in ["true", "y", "yes"]
5354
config :ama, :computor_type, (case System.get_env("COMPUTOR") do nil -> nil; "trainer" -> :trainer; _ -> :default end)
5455
config :ama, :snapshot_height, (System.get_env("SNAPSHOT_HEIGHT") || "24875547") |> :erlang.binary_to_integer()
5556

56-
pub_ipv4 = STUN.get_current_ip4()
57+
pub_ipv4 = STUN.get_current_ip4(udp_ipv4_iface)
5758
config :ama, :public_udp_ipv4, pub_ipv4
5859
config :ama, :anr, NodeANR.build(sk, pk, pub_ipv4, version)
5960
config :ama, :max_peers, (System.get_env("MAX_PEERS") || "300") |> :erlang.binary_to_integer()

ex/lib/misc/stun.ex

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ defmodule STUN do
33
alias ExSTUN.Message.Type
44
alias ExSTUN.Message.Attribute.XORMappedAddress
55

6-
def get_my_public_ipv4() do
7-
{:ok, socket} = :gen_udp.open(0, [{:active, false}, :binary])
6+
def get_my_public_ipv4(iface \\ nil) do
7+
iface = if !iface do Application.fetch_env!(:ama, :udp_ipv4_tuple) else iface end
8+
{:ok, socket} = :gen_udp.open(0, [{:ifaddr, iface}, {:active, false}, :binary])
89

910
req =
1011
%Type{class: :request, method: :binding}
@@ -19,24 +20,26 @@ defmodule STUN do
1920
"#{ip1}.#{ip2}.#{ip3}.#{ip4}"
2021
end
2122

22-
def get_my_public_ipv4_http() do
23+
def get_my_public_ipv4_http(iface \\ nil) do
2324
url = "http://api.myip.la/en?json"
24-
{:ok, %{status_code: 200, body: body}} = :comsat_http.get(url, %{}, %{timeout: 6000})
25+
26+
iface = if !iface do Application.fetch_env!(:ama, :udp_ipv4_tuple) else iface end
27+
{:ok, %{status_code: 200, body: body}} = :comsat_http.get(url, %{}, %{timeout: 6000, inet_options: [{:ifaddr, iface}]})
2528
JSX.decode!(body, labels: :atom).ip
2629
end
2730

28-
def get_current_ip4() do
31+
def get_current_ip4(iface \\ nil) do
2932
pub_ipv4 = case System.get_env("PUBLIC_UDP_IPV4") do
30-
nil -> get_current_ip4_2()
33+
nil -> get_current_ip4_2(iface)
3134
ipv4 -> ipv4
3235
end
3336
end
34-
defp get_current_ip4_2() do
37+
defp get_current_ip4_2(iface) do
3538
IO.puts "trying to get ip4 via STUN.."
36-
ip4 = try do get_my_public_ipv4() catch _,_ -> nil end
39+
ip4 = try do get_my_public_ipv4(iface) catch _,_ -> nil end
3740
if ip4 do ip4 else
3841
IO.puts "trying to get ip4 via HTTP.."
39-
ip4 = try do get_my_public_ipv4_http() catch _,_ -> nil end
42+
ip4 = try do get_my_public_ipv4_http(iface) catch _,_ -> nil end
4043
if ip4 do ip4 else
4144
IO.put "failed to find your nodes public ip. Hardcode it via PUBLIC_UDP_IPV4="
4245
end

0 commit comments

Comments
 (0)