From 15ba0508fe548b151ee125e947379883c951d34d Mon Sep 17 00:00:00 2001 From: Daniel Berteaud Date: Fri, 30 May 2014 12:01:34 +0200 Subject: [PATCH] Rebase signal master to current GIT --- signalmaster/.jshintrc | 3 +- signalmaster/Procfile | 1 + signalmaster/README.md | 4 ++- signalmaster/dev_config.json.sample | 9 ++++++ signalmaster/package.json | 26 +++++++---------- signalmaster/server.js | 57 ++++++++++++++++++++++++++++++------- 6 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 signalmaster/Procfile diff --git a/signalmaster/.jshintrc b/signalmaster/.jshintrc index e6f298d..23ddad6 100644 --- a/signalmaster/.jshintrc +++ b/signalmaster/.jshintrc @@ -6,8 +6,9 @@ "evil": true, "white": true, "undef": true, - "node": true, + "browser": true, "predef": [ + "app", "$", "FormBot", "socket", diff --git a/signalmaster/Procfile b/signalmaster/Procfile new file mode 100644 index 0000000..6f86b16 --- /dev/null +++ b/signalmaster/Procfile @@ -0,0 +1 @@ +web: node server.js \ No newline at end of file diff --git a/signalmaster/README.md b/signalmaster/README.md index 7647de2..2026fef 100644 --- a/signalmaster/README.md +++ b/signalmaster/README.md @@ -4,7 +4,9 @@ A simple signaling server for clients to connect and do signaling for WebRTC. Specifically created as a default connection point for [SimpleWebRTC.js](https://github.com/HenrikJoreteg/SimpleWebRTC) +It also supports vending STUN/TURN servers with the shared secret mechanism as described in [this draft](http://tools.ietf.org/html/draft-uberti-behave-turn-rest-00). + Read more: - [Introducing SimpleWebRTC and conversat.io](http://blog.andyet.com/2013/feb/22/introducing-simplewebrtcjs-and-conversatio/) - [SimpleWebRTC.com](http://simplewebrtc.com) - - [conversat.io](http://conversat.io) + - [talky.io](https://talky.io) diff --git a/signalmaster/dev_config.json.sample b/signalmaster/dev_config.json.sample index 9e40453..9e454ab 100644 --- a/signalmaster/dev_config.json.sample +++ b/signalmaster/dev_config.json.sample @@ -1,8 +1,17 @@ { "isDev": false, + "logLevel": 3, "server": { "port": 8888 }, + /*"stunservers" : [ + {"url": "stun:stun.l.google.com:19302"} + ], + "turnservers" : [ + { "url": "turn:your.turn.server.here", + "secret": "turnserversharedsecret" + "expiry": 86400 } + ]*/ "mysql": { "server": "localhost", "database": "vroom", diff --git a/signalmaster/package.json b/signalmaster/package.json index 766aa3d..2a39d02 100644 --- a/signalmaster/package.json +++ b/signalmaster/package.json @@ -1,21 +1,17 @@ { "name": "signal-master", - "version": "0.0.1", - "repository": { - "type": "git", - "url": "git@github.com:andyet/signal-master.git" - }, - "description": "Mind-meldification for teams", + "description": "Simple signaling server for SimpleWebRTC", + "version": "0.1.0", "dependencies": { - "async": "0.1.9", + "getconfig": "0.3.0", "node-uuid": "1.2.0", - "redis": "", - "underscore": "", - "precommit-hook": "", - "getconfig": "", - "socket.io": "", - "mysql":"", - "cookie":"" + "socket.io": "0.9.16", + "mysql": "", + "cookie": "" }, - "main": "server.js" + "main": "server.js", + "repository": { + "type": "git", + "url": "git@github.com:andyet/signal-master.git" + } } diff --git a/signalmaster/server.js b/signalmaster/server.js index 8b90998..e1b7872 100644 --- a/signalmaster/server.js +++ b/signalmaster/server.js @@ -3,7 +3,9 @@ var config = require('getconfig'), uuid = require('node-uuid'), mysql = require('mysql'), cookie_reader = require('cookie'), - io = require('socket.io').listen(config.server.port); + crypto = require('crypto'), + port = parseInt(process.env.PORT || config.server.port, 10), + io = require('socket.io').listen(port); var sql = mysql.createConnection({ host : config.mysql.server, @@ -11,6 +13,11 @@ var sql = mysql.createConnection({ user : config.mysql.user, password : config.mysql.password}); +if (config.logLevel) { + // https://github.com/Automattic/socket.io/wiki/Configuring-Socket.IO + io.set('log level', config.logLevel); +} + function describeRoom(name) { var clients = io.sockets.clients(name); var result = { @@ -94,24 +101,30 @@ io.sockets.on('connection', function (client) { client.on('unshareScreen', function (type) { client.resources.screen = false; - if (client.room) removeFeed('screen'); + removeFeed('screen'); }); client.on('join', join); function removeFeed(type) { - io.sockets.in(client.room).emit('remove', { - id: client.id, - type: type - }); + if (client.room) { + io.sockets.in(client.room).emit('remove', { + id: client.id, + type: type + }); + if (!type) { + client.leave(client.room); + client.room = undefined; + } + } } function join(name, cb) { // sanity check if (typeof name !== 'string') return; // leave any existing rooms - if (client.room) removeFeed(); - safeCb(cb)(null, describeRoom(name)) + removeFeed(); + safeCb(cb)(null, describeRoom(name)); client.join(name); client.room = name; } @@ -121,7 +134,9 @@ io.sockets.on('connection', function (client) { client.on('disconnect', function () { removeFeed(); }); - client.on('leave', removeFeed); + client.on('leave', function () { + removeFeed(); + }); client.on('create', function (name, cb) { if (arguments.length == 2) { @@ -139,7 +154,29 @@ io.sockets.on('connection', function (client) { safeCb(cb)(null, name); } }); + + // tell client about stun and turn servers and generate nonces + if (config.stunservers) { + client.emit('stunservers', config.stunservers); + } + if (config.turnservers) { + // create shared secret nonces for TURN authentication + // the process is described in draft-uberti-behave-turn-rest + var credentials = []; + 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) + (server.expiry || 86400) + ""; + hmac.update(username); + credentials.push({ + username: username, + credential: hmac.digest('base64'), + url: server.url + }); + }); + client.emit('turnservers', credentials); + } }); if (config.uid) process.setuid(config.uid); -console.log('signal master is running at: http://localhost:' + config.server.port); +console.log('signal master is running at: http://localhost:' + port);