Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move logic to request ice servers into factory class #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.idea
.DS_Store
node_modules
*.pem
1 change: 1 addition & 0 deletions config/development.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions config/production.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 31 additions & 0 deletions iceServers.js
Original file line number Diff line number Diff line change
@@ -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, {});
}
}

};
29 changes: 8 additions & 21 deletions sockets.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 || []);
}));
});


Expand Down