@@ -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