Add multiple fallback IP lookup services

parent b37c0195
...@@ -163,14 +163,25 @@ update_range(Range, Tab) -> ...@@ -163,14 +163,25 @@ update_range(Range, Tab) ->
true = ets:insert(Tab, {id_range, Range}). true = ets:insert(Tab, {id_range, Range}).
update_ip() -> update_ip() ->
case application:get_env(?APP, ip_lookup_service) of case application:get_env(?APP, ip_lookup_services) of
undefined -> false; undefined -> false;
{ok, URL} -> {ok, URLs} ->
{ok, {{_, 200, _}, _, Body}} = httpc:request(URL), update_ip(URLs)
end.
update_ip([Url | Fallbacks]) ->
try
{ok, {{_, 200, _}, _, Body}} = httpc:request(Url),
IpStr= string:trim(Body), IpStr= string:trim(Body),
{ok, _} = inet:parse_ipv4strict_address(IpStr), %assert {ok, _} = inet:parse_ipv4strict_address(IpStr), %assert
application:set_env(?APP, external_ip, IpStr) application:set_env(?APP, external_ip, IpStr)
end. catch Class:Reason ->
lager:error("Failed to update IP with ~s service: ~s",
[Url, lager:pr_stacktrace(erlang:get_stacktrace(), {Class, Reason})]),
update_ip(Fallbacks)
end;
update_ip([]) ->
error(ip_lookup_failed).
-ifdef(TEST). -ifdef(TEST).
......
{application, mtproto_proxy, {application, mtproto_proxy,
[{description, "An OTP application"}, [{description, "An OTP application"},
{vsn, "0.1.0"}, {vsn, "0.2.0"},
{registered, []}, {registered, []},
{mod, { mtproto_proxy_app, []}}, {mod, { mtproto_proxy_app, []}},
{applications, {applications,
...@@ -21,12 +21,17 @@ ...@@ -21,12 +21,17 @@
%% Telegram server uses your external IP address as part of encryption %% Telegram server uses your external IP address as part of encryption
%% key, so, you should know it. %% key, so, you should know it.
%% You can configure IP lookup service by `ip_lookup_service' (should %% You can configure IP lookup services by `ip_lookup_services' (should
%% return my IP address as one line from this URL) or set IP address %% return my IP address as one line from this URL) or set IP address
%% statically by `external_ip' (not both). %% statically by `external_ip' (not both).
%% If both are unset, we will try to guess IP address %% If both are unset, proxy will try to guess IP address
%% from getsockname(). %% from getsockname().
{ip_lookup_service, "https://digitalresistance.dog/myIp"}, %% `ip_lookup_services' will be tried one-by-one: if 1st is not responding,
%% 2nd one will be tried and so on
{ip_lookup_services,
["http://ip.seriyps.ru/",
"https://digitalresistance.dog/myIp",
"http://ipv4.myexternalip.com/raw"]},
%% {external_ip, "YOUR.SERVER.EXTERNAL.IP"}, %% {external_ip, "YOUR.SERVER.EXTERNAL.IP"},
%% Interface to listen for incoming connections %% Interface to listen for incoming connections
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment