From 8578485a84709d22a46f292c5530f3396144cdea Mon Sep 17 00:00:00 2001 From: Noe Date: Wed, 12 Jul 2017 15:24:40 -0700 Subject: [PATCH 1/2] Move logic to request ice servers into factory class --- .gitignore | 1 + config/development.json | 1 + config/production.json | 1 + iceServers.js | 31 +++++++++++++++++++++++++++++++ sockets.js | 29 ++++++++--------------------- 5 files changed, 42 insertions(+), 21 deletions(-) create mode 100644 iceServers.js diff --git a/.gitignore b/.gitignore index 4176ef0f..db0b11a0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +.idea .DS_Store node_modules *.pem diff --git a/config/development.json b/config/development.json index 0c79b8e3..6222da3b 100644 --- a/config/development.json +++ b/config/development.json @@ -12,6 +12,7 @@ "/* maxClients */": "/* maximum number of clients per room. 0 = no limit */", "maxClients": 0 }, + "iceController": "./iceServers", "stunservers": [ { "url": "stun:stun.l.google.com:19302" diff --git a/config/production.json b/config/production.json index 69c9e4be..56f638c7 100644 --- a/config/production.json +++ b/config/production.json @@ -12,6 +12,7 @@ "/* maxClients */": "/* maximum number of clients per room. 0 = no limit */", "maxClients": 0 }, + "iceController": "./iceServer", "stunservers": [ { "url": "stun:stun.l.google.com:19302" diff --git a/iceServers.js b/iceServers.js new file mode 100644 index 00000000..339f5fe5 --- /dev/null +++ b/iceServers.js @@ -0,0 +1,31 @@ +var crypto = require('crypto'); +var config = require('getconfig'); +module.exports = { + getIceServers: function (client, cb) { + try { + // create shared secret nonces for TURN authentication + // the process is described in draft-uberti-behave-turn-rest + var credentials = []; + // allow selectively vending turn credentials based on origin. + var origin = client.handshake.headers.origin; + + if (!config.turnorigins || config.turnorigins.indexOf(origin) !== -1) { + config.turnservers.forEach(function (server) { + var hmac = crypto.createHmac('sha1', server.secret); + // default to 86400 seconds timeout unless specified + var username = Math.floor(new Date().getTime() / 1000) + (parseInt(server.expiry || 86400, 10)) + ""; + hmac.update(username); + credentials.push({ + username: username, + credential: hmac.digest('base64'), + urls: server.urls || server.url + }); + }); + } + cb(null, {turnservers: credentials, stunservers: config.stunservers || []}); + } catch (e) { + cb(e, {}); + } + } + +}; \ No newline at end of file diff --git a/sockets.js b/sockets.js index 0dd461a6..f25207e9 100644 --- a/sockets.js +++ b/sockets.js @@ -100,27 +100,14 @@ module.exports = function (server, config) { // tell client about stun and turn servers and generate nonces - client.emit('stunservers', config.stunservers || []); - - // create shared secret nonces for TURN authentication - // the process is described in draft-uberti-behave-turn-rest - var credentials = []; - // allow selectively vending turn credentials based on origin. - var origin = client.handshake.headers.origin; - if (!config.turnorigins || config.turnorigins.indexOf(origin) !== -1) { - config.turnservers.forEach(function (server) { - var hmac = crypto.createHmac('sha1', server.secret); - // default to 86400 seconds timeout unless specified - var username = Math.floor(new Date().getTime() / 1000) + (parseInt(server.expiry || 86400, 10)) + ""; - hmac.update(username); - credentials.push({ - username: username, - credential: hmac.digest('base64'), - urls: server.urls || server.url - }); - }); - } - client.emit('turnservers', credentials); + var iceController = require(config.iceController); + iceController.getIceServers(client, safeCb(function (error, response) { + if (error) { + console.log('err', error); + } + client.emit('stunservers', response.stunservers || []); + client.emit('turnservers', response.turnservers || []); + })); }); From d38105370eae41974f328069b376f515808d186c Mon Sep 17 00:00:00 2001 From: nicholi Date: Wed, 12 Jul 2017 18:01:54 -0700 Subject: [PATCH 2/2] Typo --- config/production.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config/production.json b/config/production.json index 56f638c7..796e557d 100644 --- a/config/production.json +++ b/config/production.json @@ -12,7 +12,7 @@ "/* maxClients */": "/* maximum number of clients per room. 0 = no limit */", "maxClients": 0 }, - "iceController": "./iceServer", + "iceController": "./iceServers", "stunservers": [ { "url": "stun:stun.l.google.com:19302"