Skip to content

Commit

Permalink
Add API to force Janus to use TURN (#2774)
Browse files Browse the repository at this point in the history
  • Loading branch information
lminiero authored Sep 27, 2021
1 parent 63cbedd commit 7587975
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 2 deletions.
14 changes: 12 additions & 2 deletions conf/janus.jcfg.sample.in
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,19 @@ nat: {
# You can configure a TURN server in two different ways: specifying a
# statically configured TURN server, and thus provide the address of the
# TURN server, the transport (udp/tcp/tls) to use, and a set of valid
# credentials to authenticate...
# credentials to authenticate. Notice that you should NEVER configure
# a TURN server for Janus unless it's really what you want! If you want
# *users* to use TURN, then you need to configure that on the client
# side, and NOT in Janus. The following TURN configuration should ONLY
# be enabled when Janus itself is sitting behind a restrictive firewall
# (e.g., it's part of a service installed on a box in a private home).
#turn_server = "myturnserver.com"
#turn_port = 3478
#turn_type = "udp"
#turn_user = "myuser"
#turn_pwd = "mypassword"

# ... or you can make use of the TURN REST API to get info on one or more
# You can also make use of the TURN REST API to get info on one or more
# TURN services dynamically. This makes use of the proposed standard of
# such an API (https://tools.ietf.org/html/draft-uberti-behave-turn-rest-00)
# which is currently available in both rfc5766-turn-server and coturn.
Expand All @@ -327,6 +332,11 @@ nat: {
#turn_rest_api_method = "GET"
#turn_rest_api_timeout = 10

# In case a TURN server is provided, you can allow applications to force
# Janus to use TURN (https://github.com/meetecho/janus-gateway/pull/2774).
# This is NOT allowed by default: only enable it if you know what you're doing.
#allow_force_relay = true

# You can also choose which interfaces should be explicitly used by the
# gateway for the purpose of ICE candidates gathering, thus excluding
# others that may be available. To do so, use the 'ice_enforce_list'
Expand Down
2 changes: 2 additions & 0 deletions html/janus.js
Original file line number Diff line number Diff line change
Expand Up @@ -1390,6 +1390,8 @@ function Janus(gatewayCallbacks) {
request.jsep.e2ee = true;
if(jsep.rid_order === "hml" || jsep.rid_order === "lmh")
request.jsep.rid_order = jsep.rid_order;
if(jsep.force_relay)
request.jsep.force_relay = true;
}
Janus.debug("Sending message to plugin (handle=" + handleId + "):");
Janus.debug(request);
Expand Down
8 changes: 8 additions & 0 deletions ice.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ char *janus_ice_get_turn_rest_api(void) {
#endif
}

/* Force relay settings */
static gboolean force_relay_allowed = FALSE;
void janus_ice_allow_force_relay(void) {
force_relay_allowed = TRUE;
}
gboolean janus_ice_is_force_relay_allowed(void) {
return force_relay_allowed;
}

/* ICE-Lite status */
static gboolean janus_ice_lite_enabled;
Expand Down
5 changes: 5 additions & 0 deletions ice.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,11 @@ uint16_t janus_ice_get_turn_port(void);
/*! \brief Method to get the specified TURN REST API backend, if any
* @returns The currently specified TURN REST API backend, if available, or NULL if not */
char *janus_ice_get_turn_rest_api(void);
/*! \brief Method to enable applications to force Janus to use TURN */
void janus_ice_allow_force_relay(void);
/*! \brief Method to check whether applications are allowed to force Janus to use TURN
* @returns TRUE if they're allowed, FALSE otherwise */
gboolean janus_ice_is_force_relay_allowed(void);
/*! \brief Helper method to force Janus to overwrite all host candidates with the public IP
* @param[in] keep_private_host Whether we should keep the original private host as a separate candidate, or replace it */
void janus_ice_enable_nat_1_1(gboolean keep_private_host);
Expand Down
22 changes: 22 additions & 0 deletions janus.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ static struct janus_json_parameter jsep_parameters[] = {
{"sdp", JSON_STRING, JANUS_JSON_PARAM_REQUIRED},
{"trickle", JANUS_JSON_BOOL, 0},
{"rid_order", JSON_STRING, 0},
{"force_relay", JANUS_JSON_BOOL, 0},
{"e2ee", JANUS_JSON_BOOL, 0}
};
static struct janus_json_parameter add_token_parameters[] = {
Expand Down Expand Up @@ -376,6 +377,8 @@ static json_t *janus_info(const char *transaction) {
g_snprintf(server, 255, "%s:%"SCNu16, janus_ice_get_turn_server(), janus_ice_get_turn_port());
json_object_set_new(info, "turn-server", json_string(server));
}
if(janus_ice_is_force_relay_allowed())
json_object_set_new(info, "allow-force-relay", json_true());
json_object_set_new(info, "static-event-loops", json_integer(janus_ice_get_static_event_loops()));
if(janus_ice_get_static_event_loops())
json_object_set_new(info, "loop-indication", janus_ice_is_loop_indication_allowed() ? json_true() : json_false());
Expand Down Expand Up @@ -1480,6 +1483,20 @@ int janus_process_incoming_request(janus_request *request) {
goto jsondone;
}
}
/* If for some reason the user is asking us to force using a relay, do that. Notice
* that this only works with libnice >= 0.1.14, and will cause the PeerConnection
* to fail if Janus itself is not configured to use a TURN server. Don't use this
* feature if you don't know what you're doing! You will almost always NOT want
* Janus itself to use TURN: https://janus.conf.meetecho.com/docs/FAQ.html#turn */
if(json_is_true(json_object_get(jsep, "force_relay"))) {
if(!janus_ice_is_force_relay_allowed()) {
JANUS_LOG(LOG_WARN, "[%"SCNu64"] Forcing Janus to use a TURN server is not allowed\n", handle->handle_id);
} else {
JANUS_LOG(LOG_VERB, "[%"SCNu64"] Forcing Janus to use a TURN server\n", handle->handle_id);
g_object_set(G_OBJECT(handle->agent), "force-relay", TRUE, NULL);
}
}
/* Process the remote SDP */
if(janus_sdp_process(handle, parsed_sdp, rids_hml, FALSE) < 0) {
JANUS_LOG(LOG_ERR, "Error processing SDP\n");
janus_sdp_destroy(parsed_sdp);
Expand Down Expand Up @@ -4879,6 +4896,11 @@ gint main(int argc, char *argv[])
}
}
#endif
item = janus_config_get(config, config_nat, janus_config_type_item, "allow_force_relay");
if(item && item->value && janus_is_true(item->value)) {
JANUS_LOG(LOG_WARN, "Note: applications/users will be allowed to force Janus to use TURN. Make sure you know what you're doing!\n");
janus_ice_allow_force_relay();
}
/* 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");
if(item && item->value) {
Expand Down

0 comments on commit 7587975

Please sign in to comment.