From dfe454e18f546332421a62a9171f76a58eb7055c Mon Sep 17 00:00:00 2001 From: "J. Ryan Stinnett" Date: Tue, 13 Aug 2019 15:32:07 +0100 Subject: [PATCH] Change ICE server fallback to opt-in This changes the ICE server fallback to be disabled by default. The SDK consumer will receive a new event in case the homeserver has no ICE servers of its own, and can prompt the user to agree to the fallback if desired. Part of https://github.com/vector-im/riot-web/issues/10173 --- src/client.js | 33 +++++++++++++++++++++++++++++---- src/webrtc/call.js | 12 +++++++----- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/client.js b/src/client.js index 08cee844b..804e19ef7 100644 --- a/src/client.js +++ b/src/client.js @@ -221,6 +221,7 @@ function MatrixClient(opts) { this._verificationMethods = opts.verificationMethods; this._forceTURN = opts.forceTURN || false; + this._fallbackICEServerAllowed = false; // List of which rooms have encryption enabled: separate from crypto because // we still want to know which rooms are encrypted even if crypto is disabled: @@ -3883,6 +3884,28 @@ MatrixClient.prototype.getTurnServers = function() { return this._turnServers || []; }; +/** + * Set whether to allow a fallback ICE server should be used for negotiating a + * WebRTC connection if the homeserver doesn't provide any servers. Defaults to + * false. + * + * @param {boolean} allow + */ +MatrixClient.prototype.setFallbackICEServerAllowed = function(allow) { + this._fallbackICEServerAllowed = allow; +}; + +/** + * Get whether to allow a fallback ICE server should be used for negotiating a + * WebRTC connection if the homeserver doesn't provide any servers. Defaults to + * false. + * + * @returns {boolean} + */ +MatrixClient.prototype.isFallbackICEServerAllowed = function() { + return this._fallbackICEServerAllowed; +}; + // Higher level APIs // ================= @@ -4330,13 +4353,15 @@ function checkTurnServers(client) { client._checkTurnServersTimeoutID = setTimeout(() => { checkTurnServers(client); }, (res.ttl || (60 * 60)) * 1000 * 0.9); + } else { + logger.warn("No TURN URIs from homeserver"); + client.emit("Call.noTURNServers"); } }, function(err) { logger.error("Failed to get TURN URIs"); - client._checkTurnServersTimeoutID = - setTimeout(function() { - checkTurnServers(client); -}, 60000); + client._checkTurnServersTimeoutID = setTimeout(function() { + checkTurnServers(client); + }, 60000); }); } diff --git a/src/webrtc/call.js b/src/webrtc/call.js index 501548df4..8aa4d2841 100644 --- a/src/webrtc/call.js +++ b/src/webrtc/call.js @@ -61,9 +61,9 @@ function MatrixCall(opts) { this.URL = opts.URL; // Array of Objects with urls, username, credential keys this.turnServers = opts.turnServers || []; - if (this.turnServers.length === 0) { + if (this.turnServers.length === 0 && this.client.isFallbackICEServerAllowed()) { this.turnServers.push({ - urls: [MatrixCall.FALLBACK_STUN_SERVER], + urls: [MatrixCall.FALLBACK_ICE_SERVER], }); } utils.forEach(this.turnServers, function(server) { @@ -92,8 +92,8 @@ function MatrixCall(opts) { } /** The length of time a call can be ringing for. */ MatrixCall.CALL_TIMEOUT_MS = 60000; -/** The fallback server to use for STUN. */ -MatrixCall.FALLBACK_STUN_SERVER = 'stun:turn.matrix.org'; +/** The fallback ICE server to use for STUN or TURN protocols. */ +MatrixCall.FALLBACK_ICE_SERVER = 'stun:turn.matrix.org'; /** An error code when the local client failed to create an offer. */ MatrixCall.ERR_LOCAL_OFFER_FAILED = "local_offer_failed"; /** @@ -1337,7 +1337,9 @@ module.exports.setVideoInput = function(deviceId) { videoInput = deviceId; }; * @param {MatrixClient} client The client instance to use. * @param {string} roomId The room the call is in. * @param {Object?} options DEPRECATED optional options map. - * @param {boolean} options.forceTURN DEPRECATED whether relay through TURN should be forced. This option is deprecated - use opts.forceTURN when creating the matrix client since it's only possible to set this option on outbound calls. + * @param {boolean} options.forceTURN DEPRECATED whether relay through TURN should be + * forced. This option is deprecated - use opts.forceTURN when creating the matrix client + * since it's only possible to set this option on outbound calls. * @return {MatrixCall} the call or null if the browser doesn't support calling. */ module.exports.createNewMatrixCall = function(client, roomId, options) {