From 0e894a78c390d1262b88e6cf3315278481783378 Mon Sep 17 00:00:00 2001 From: evorw Date: Thu, 3 Dec 2020 14:25:48 +0000 Subject: [PATCH] Make TURN REST API timeout configurable in janus.jcfg (#2470) --- conf/janus.jcfg.sample.in | 9 ++++++--- ice.c | 4 ++-- ice.h | 3 ++- janus.c | 12 +++++++++++- turnrest.c | 6 ++++-- turnrest.h | 5 +++-- 6 files changed, 28 insertions(+), 11 deletions(-) diff --git a/conf/janus.jcfg.sample.in b/conf/janus.jcfg.sample.in index a881cfb525..7b9c384062 100644 --- a/conf/janus.jcfg.sample.in +++ b/conf/janus.jcfg.sample.in @@ -294,12 +294,15 @@ nat: { # which is currently available in both rfc5766-turn-server and coturn. # You enable this by specifying the address of your TURN REST API backend, # the HTTP method to use (GET or POST) and, if required, the API key Janus - # must provide. Notice that the 'opaque_id' provided via Janus API will be - # used as the username for a specific PeerConnection by default; if that one - # is missing, the 'session_id' will be used as the username instead. + # must provide. The timeout can be configured in seconds, with a default of + # 10 seconds and a minimum of 1 second. Notice that the 'opaque_id' provided + # via Janus API will be used as the username for a specific PeerConnection + # by default; if that one is missing, the 'session_id' will be used as the + # username instead. #turn_rest_api = "http://yourbackend.com/path/to/api" #turn_rest_api_key = "anyapikeyyoumayhaveset" #turn_rest_api_method = "GET" + #turn_rest_api_timeout = 10 # You can also choose which interfaces should be explicitly used by the # gateway for the purpose of ICE candidates gathering, thus excluding diff --git a/ice.c b/ice.c index 7199ec4730..7f658f7643 100644 --- a/ice.c +++ b/ice.c @@ -1125,7 +1125,7 @@ int janus_ice_set_turn_server(gchar *turn_server, uint16_t turn_port, gchar *tur return 0; } -int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_method) { +int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_method, uint api_timeout) { #ifndef HAVE_TURNRESTAPI JANUS_LOG(LOG_ERR, "Janus has been built with no libcurl support, TURN REST API unavailable\n"); return -1; @@ -1135,7 +1135,7 @@ int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_me JANUS_LOG(LOG_ERR, "Invalid TURN REST API backend: not an HTTP address\n"); return -1; } - janus_turnrest_set_backend(api_server, api_key, api_method); + janus_turnrest_set_backend(api_server, api_key, api_method, api_timeout); JANUS_LOG(LOG_INFO, "TURN REST API backend: %s\n", api_server ? api_server : "(disabled)"); #endif return 0; diff --git a/ice.h b/ice.h index 65ce7e5db7..8fe22c5c33 100644 --- a/ice.h +++ b/ice.h @@ -70,8 +70,9 @@ int janus_ice_set_turn_server(gchar *turn_server, uint16_t turn_port, gchar *tur * @param[in] api_server TURN REST API backend (NULL to disable the API) * @param[in] api_key API key to use, if required * @param[in] api_method HTTP method to use (POST by default) + * @param[in] api_timeout total timeout for HTTP method in seconds * @returns 0 in case of success, a negative integer on errors */ -int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_method); +int janus_ice_set_turn_rest_api(gchar *api_server, gchar *api_key, gchar *api_method, uint api_timeout); /*! \brief Method to get the STUN server IP address * @returns The currently used STUN server IP address, if available, or NULL if not */ char *janus_ice_get_stun_server(void); diff --git a/janus.c b/janus.c index c885f2c3dc..bdfe68b230 100644 --- a/janus.c +++ b/janus.c @@ -4643,6 +4643,7 @@ gint main(int argc, char *argv[]) char *turn_rest_api = NULL, *turn_rest_api_key = NULL; #ifdef HAVE_TURNRESTAPI char *turn_rest_api_method = NULL; + uint turn_rest_api_timeout = 10; #endif uint16_t rtp_min_port = 0, rtp_max_port = 0; gboolean ice_lite = FALSE, ice_tcp = FALSE, full_trickle = FALSE, ipv6 = FALSE, @@ -4757,6 +4758,15 @@ gint main(int argc, char *argv[]) item = janus_config_get(config, config_nat, janus_config_type_item, "turn_rest_api_method"); if(item && item->value) turn_rest_api_method = (char *)item->value; + item = janus_config_get(config, config_nat, janus_config_type_item, "turn_rest_api_timeout"); + if(item && item->value) { + int rst = atoi(item->value); + if(rst <= 0) { /* Don't allow user to set 0 seconds i.e., infinite wait */ + JANUS_LOG(LOG_WARN, "Ignoring turn_rest_api_timeout as it's not a positive integer, leaving at default (10 seconds)\n"); + } else { + turn_rest_api_timeout = rst; + } + } #endif /* Do we need a limited number of static event loops, or is it ok to have one per handle (the default)? */ item = janus_config_get(config, config_general, janus_config_type_item, "event_loops"); @@ -4785,7 +4795,7 @@ gint main(int argc, char *argv[]) JANUS_LOG(LOG_WARN, "A TURN REST API backend specified in the settings, but libcurl support has not been built\n"); } #else - if(janus_ice_set_turn_rest_api(turn_rest_api, turn_rest_api_key, turn_rest_api_method) < 0) { + if(janus_ice_set_turn_rest_api(turn_rest_api, turn_rest_api_key, turn_rest_api_method, turn_rest_api_timeout) < 0) { JANUS_LOG(LOG_FATAL, "Invalid TURN REST API configuration: %s (%s, %s)\n", turn_rest_api, turn_rest_api_key, turn_rest_api_method); exit(1); } diff --git a/turnrest.c b/turnrest.c index cbaf8f6ed5..797ed184d3 100644 --- a/turnrest.c +++ b/turnrest.c @@ -31,6 +31,7 @@ static const char *api_server = NULL; static const char *api_key = NULL; static gboolean api_http_get = FALSE; +static uint api_timeout; static janus_mutex api_mutex = JANUS_MUTEX_INITIALIZER; @@ -69,7 +70,7 @@ void janus_turnrest_deinit(void) { janus_mutex_unlock(&api_mutex); } -void janus_turnrest_set_backend(const char *server, const char *key, const char *method) { +void janus_turnrest_set_backend(const char *server, const char *key, const char *method, const uint timeout) { janus_mutex_lock(&api_mutex); /* Get rid of the old values first */ @@ -93,6 +94,7 @@ void janus_turnrest_set_backend(const char *server, const char *key, const char api_http_get = FALSE; } } + api_timeout = timeout; } janus_mutex_unlock(&api_mutex); } @@ -163,7 +165,7 @@ janus_turnrest_response *janus_turnrest_request(const char *user) { /* FIXME Some servers don't like a POST with no data */ curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query_string); } - curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10L); /* FIXME Max 10 seconds */ + curl_easy_setopt(curl, CURLOPT_TIMEOUT, api_timeout); /* For getting data, we use an helper struct and the libcurl callback */ janus_turnrest_buffer data; data.buffer = g_malloc0(1); diff --git a/turnrest.h b/turnrest.h index 807e04db62..1e151a2101 100644 --- a/turnrest.h +++ b/turnrest.h @@ -31,8 +31,9 @@ void janus_turnrest_deinit(void); * @param server The REST API server address (pass NULL to disable the * TURN REST API entirely) * @param key The API key, if any (pass NULL if it's not required) - * @param method The HTTP method to use, POST or GET (NULL means POST) */ -void janus_turnrest_set_backend(const char *server, const char *key, const char *method); + * @param method The HTTP method to use, POST or GET (NULL means POST) + * @param timeout The timeout in seconds */ +void janus_turnrest_set_backend(const char *server, const char *key, const char *method, const uint timeout); /*! \brief Get the currently set TURN REST API backend * @returns The currently set TURN REST API backend */ const char *janus_turnrest_get_backend(void);