From c836dc66a8a10ba88d985ba07511b89d181c6b81 Mon Sep 17 00:00:00 2001 From: Holger Weiss Date: Mon, 20 Apr 2020 08:42:32 +0200 Subject: [PATCH] ejabberd_stun: Set a default 'turn_ip' Try to resolve the local hostname, use the result as the default 'turn_ip', and only log a warning if that fails. Using the local hostname's address by default is analogous to mod_proxy65's behavior. --- src/ejabberd_stun.erl | 14 ++++++++------ src/misc.erl | 12 ++++++++++-- src/mod_proxy65_service.erl | 10 +--------- 3 files changed, 19 insertions(+), 17 deletions(-) diff --git a/src/ejabberd_stun.erl b/src/ejabberd_stun.erl index 347598e6d..89fc830dd 100644 --- a/src/ejabberd_stun.erl +++ b/src/ejabberd_stun.erl @@ -85,11 +85,13 @@ prepare_turn_opts(Opts, _UseTurn = false) -> set_certfile(Opts); prepare_turn_opts(Opts, _UseTurn = true) -> NumberOfMyHosts = length(ejabberd_option:hosts()), - case proplists:get_value(turn_ip, Opts) of - undefined -> - ?WARNING_MSG("Option 'turn_ip' is undefined, " - "most likely the TURN relay won't be working " - "properly", []); + case {proplists:get_value(turn_ip, Opts), + proplists:get_value(turn_ip, listen_options())} of + {undefined, {127, _, _, _}} -> + ?WARNING_MSG("Option 'turn_ip' is undefined and the server's " + "hostname doesn't resolve to a public IPv4 address, " + "most likely the TURN relay won't be working properly", + []); _ -> ok end, @@ -158,7 +160,7 @@ listen_opt_type(certfile) -> listen_options() -> [{shaper, none}, {use_turn, false}, - {turn_ip, undefined}, + {turn_ip, misc:get_my_ip()}, {auth_type, user}, {auth_realm, undefined}, {tls, false}, diff --git a/src/misc.erl b/src/misc.erl index 8476e7cbf..e589c2e22 100644 --- a/src/misc.erl +++ b/src/misc.erl @@ -40,8 +40,8 @@ read_css/1, read_img/1, read_js/1, read_lua/1, try_url/1, intersection/2, format_val/1, cancel_timer/1, unique_timestamp/0, is_mucsub_message/1, best_match/2, pmap/2, peach/2, format_exception/4, - parse_ip_mask/1, match_ip_mask/3, format_hosts_list/1, format_cycle/1, - delete_dir/1]). + get_my_ip/0, parse_ip_mask/1, match_ip_mask/3, format_hosts_list/1, + format_cycle/1, delete_dir/1]). %% Deprecated functions -export([decode_base64/1, encode_base64/1]). @@ -509,6 +509,14 @@ format_exception(Level, Class, Reason, Stacktrace) -> end). -endif. +-spec get_my_ip() -> inet:ip_address(). +get_my_ip() -> + {ok, MyHostName} = inet:gethostname(), + case inet:getaddr(MyHostName, inet) of + {ok, Addr} -> Addr; + {error, _} -> {127, 0, 0, 1} + end. + -spec parse_ip_mask(binary()) -> {ok, {inet:ip4_address(), 0..32}} | {ok, {inet:ip6_address(), 0..128}} | error. diff --git a/src/mod_proxy65_service.erl b/src/mod_proxy65_service.erl index fe07ca72a..5dcb79944 100644 --- a/src/mod_proxy65_service.erl +++ b/src/mod_proxy65_service.erl @@ -266,19 +266,11 @@ get_streamhost(Host, ServerHost) -> get_endpoint(Host) -> Port = mod_proxy65_opt:port(Host), IP = case mod_proxy65_opt:ip(Host) of - undefined -> get_my_ip(); + undefined -> misc:get_my_ip(); Addr -> Addr end, {Port, IP, tcp}. --spec get_my_ip() -> inet:ip_address(). -get_my_ip() -> - {ok, MyHostName} = inet:gethostname(), - case inet:getaddr(MyHostName, inet) of - {ok, Addr} -> Addr; - {error, _} -> {127, 0, 0, 1} - end. - max_connections(ServerHost) -> mod_proxy65_opt:max_connections(ServerHost).