From db86d3918fa8a828be160207b9e391bf241dc398 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Tue, 19 Aug 2014 22:57:28 -0500 Subject: [PATCH 1/7] Update to socket.io 1.0 --- lib/channel/channel.js | 4 +--- lib/io/ioserver.js | 46 ++++++++++++++++-------------------------- package.json | 2 +- www/js/callbacks.js | 11 ++-------- 4 files changed, 21 insertions(+), 42 deletions(-) diff --git a/lib/channel/channel.js b/lib/channel/channel.js index 5f0e6fed..f13ef131 100644 --- a/lib/channel/channel.js +++ b/lib/channel/channel.js @@ -609,9 +609,7 @@ Channel.prototype.handleReadLog = function (user) { }; Channel.prototype._broadcast = function (msg, data, ns) { - sio.ioServers.forEach(function (io) { - io.sockets.in(ns).emit(msg, data); - }); + sio.instance.in(ns).emit(msg, data); }; Channel.prototype.broadcastAll = function (msg, data) { diff --git a/lib/io/ioserver.js b/lib/io/ioserver.js index 09cb12b4..bde1e93b 100644 --- a/lib/io/ioserver.js +++ b/lib/io/ioserver.js @@ -26,14 +26,16 @@ var ipCount = {}; /** * Called before an incoming socket.io connection is accepted. */ -function handleAuth(data, accept) { - data.user = false; +function handleAuth(socket, accept) { + var data = socket.request; + + socket.user = false; if (data.headers.cookie) { cookieParser(data, null, function () { var auth = data.cookies.auth; db.users.verifyAuth(auth, function (err, user) { if (!err) { - data.user = { + socket.user = { name: user.name, global_rank: user.global_rank }; @@ -158,10 +160,10 @@ function handleConnection(sock) { addTypecheckedFunctions(sock); var user = new User(sock); - if (sock.handshake.user) { + if (sock.user) { user.setFlag(Flags.U_REGISTERED); user.clearFlag(Flags.U_READY); - user.refreshAccount({ name: sock.handshake.user.name }, + user.refreshAccount({ name: sock.user.name }, function (err, account) { if (err) { user.clearFlag(Flags.U_REGISTERED); @@ -189,44 +191,30 @@ function handleConnection(sock) { module.exports = { init: function (srv) { + var bound = {}; + var io = sio.instance = sio(); + + io.use(handleAuth); + io.on("connection", handleConnection); + Config.get("listen").forEach(function (bind) { if (!bind.io) { return; } var id = bind.ip + ":" + bind.port; - if (id in srv.ioServers) { + if (id in bound) { Logger.syslog.log("[WARN] Ignoring duplicate listen address " + id); return; } - var io = null; if (id in srv.servers) { - io = srv.ioServers[id] = sio.listen(srv.servers[id]); + io.attach(srv.servers[id]); } else { - if (net.isIPv6(bind.ip) || bind.ip === "::") { - /** - * Socket.IO won't bind to a v6 address natively. - * Instead, we have to create a node HTTP server, bind it - * to the desired address, then have socket.io listen on it - */ - io = srv.ioServers[id] = sio.listen( - require("http").createServer().listen(bind.port, bind.ip) - ); - } else { - io = srv.ioServers[id] = sio.listen(bind.port, bind.ip); - } + io.attach(require("http").createServer().listen(bind.port, bind.ip)); } - if (io) { - io.set("log level", 1); - io.set("authorization", handleAuth); - io.on("connection", handleConnection); - } + bound[id] = null; }); - - sio.ioServers = Object.keys(srv.ioServers) - .filter(Object.hasOwnProperty.bind(srv.ioServers)) - .map(function (k) { return srv.ioServers[k] }); } }; diff --git a/package.json b/package.json index 2ede3b54..5627b6bb 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "oauth": "^0.9.12", "q": "^1.0.1", "serve-static": "^1.5.3", - "socket.io": "~0.9.16", + "socket.io": "^1.0.6", "yamljs": "^0.1.5" } } diff --git a/www/js/callbacks.js b/www/js/callbacks.js index e9608a1c..f8946ae4 100644 --- a/www/js/callbacks.js +++ b/www/js/callbacks.js @@ -1095,17 +1095,10 @@ try { throw false; } - if (NO_WEBSOCKETS || USEROPTS.altsocket) { - var i = io.transports.indexOf("websocket"); - if (i >= 0) { - io.transports.splice(i, 1); - } - } - if (IO_URL === IO_URLS["ipv4-ssl"] || IO_URL === IO_URLS["ipv6-ssl"]) { - socket = io.connect(IO_URL, { secure: true }); + socket = io(IO_URL, { secure: true }); } else { - socket = io.connect(IO_URL); + socket = io(IO_URL); } setupCallbacks(); } catch (e) { From 3e53b3030512f9b97fda11fcf14773afff3bc7ca Mon Sep 17 00:00:00 2001 From: calzoneman Date: Wed, 20 Aug 2014 11:44:37 -0500 Subject: [PATCH 2/7] Fix IP extraction for socket.io --- lib/io/ioserver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/io/ioserver.js b/lib/io/ioserver.js index bde1e93b..72ef6ff8 100644 --- a/lib/io/ioserver.js +++ b/lib/io/ioserver.js @@ -126,7 +126,7 @@ function addTypecheckedFunctions(sock) { * Called after a connection is accepted */ function handleConnection(sock) { - var ip = sock.handshake.address.address; + var ip = sock.request.connection.remoteAddress; if (net.isIPv6(ip)) { ip = util.expandIPv6(ip); } From ab49eaab763326be677595f539439b77148be86b Mon Sep 17 00:00:00 2001 From: calzoneman Date: Wed, 20 Aug 2014 12:09:38 -0500 Subject: [PATCH 3/7] Reject sockets with no IP --- lib/io/ioserver.js | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/io/ioserver.js b/lib/io/ioserver.js index 72ef6ff8..a00e7eec 100644 --- a/lib/io/ioserver.js +++ b/lib/io/ioserver.js @@ -127,6 +127,13 @@ function addTypecheckedFunctions(sock) { */ function handleConnection(sock) { var ip = sock.request.connection.remoteAddress; + if (!ip) { + socket.emit("kick", { + reason: "Your IP address could not be determined from the socket connection. See https://github.com/Automattic/socket.io/issues/1387#issuecomment-48425088 for details" + }); + return; + } + if (net.isIPv6(ip)) { ip = util.expandIPv6(ip); } From c757f62fc1db676641a7d411fbd481fbe3f8ef4c Mon Sep 17 00:00:00 2001 From: calzoneman Date: Wed, 20 Aug 2014 12:11:46 -0500 Subject: [PATCH 4/7] Typo --- lib/io/ioserver.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/io/ioserver.js b/lib/io/ioserver.js index a00e7eec..ad852539 100644 --- a/lib/io/ioserver.js +++ b/lib/io/ioserver.js @@ -128,7 +128,7 @@ function addTypecheckedFunctions(sock) { function handleConnection(sock) { var ip = sock.request.connection.remoteAddress; if (!ip) { - socket.emit("kick", { + sock.emit("kick", { reason: "Your IP address could not be determined from the socket connection. See https://github.com/Automattic/socket.io/issues/1387#issuecomment-48425088 for details" }); return; From ec9ee1d37f53d189c9e91407703fee16d02f4652 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Thu, 21 Aug 2014 20:30:24 -0500 Subject: [PATCH 5/7] Wrap socket callbacks in try-catch, fix mediaUpdate remove video --- www/js/callbacks.js | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/www/js/callbacks.js b/www/js/callbacks.js index f8946ae4..eef13e18 100644 --- a/www/js/callbacks.js +++ b/www/js/callbacks.js @@ -893,7 +893,7 @@ Callbacks = { }, mediaUpdate: function(data) { - if ($("body").hasClass("chatOnly")) { + if ($("body").hasClass("chatOnly") || $("#videowrap").length === 0) { return; } @@ -1071,16 +1071,22 @@ Callbacks = { } } -var SOCKET_DEBUG = true; +var SOCKET_DEBUG = false; setupCallbacks = function() { for(var key in Callbacks) { (function(key) { - socket.on(key, function(data) { - if (SOCKET_DEBUG) { - console.log(key, data); - } - Callbacks[key](data); - }); + socket.on(key, function(data) { + if (SOCKET_DEBUG) { + console.log(key, data); + } + try { + Callbacks[key](data); + } catch (e) { + if (SOCKET_DEBUG) { + console.log("EXCEPTION: " + e.stack); + } + } + }); })(key); } } From 9be993a6792cf7748426e6d5255e91560fe9deef Mon Sep 17 00:00:00 2001 From: calzoneman Date: Sun, 24 Aug 2014 13:18:15 -0500 Subject: [PATCH 6/7] Fix socket.io issue; fix ACP announcements --- lib/io/ioserver.js | 4 ++-- lib/server.js | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/lib/io/ioserver.js b/lib/io/ioserver.js index ad852539..93f0fab7 100644 --- a/lib/io/ioserver.js +++ b/lib/io/ioserver.js @@ -126,10 +126,10 @@ function addTypecheckedFunctions(sock) { * Called after a connection is accepted */ function handleConnection(sock) { - var ip = sock.request.connection.remoteAddress; + var ip = sock.client.conn.remoteAddress; if (!ip) { sock.emit("kick", { - reason: "Your IP address could not be determined from the socket connection. See https://github.com/Automattic/socket.io/issues/1387#issuecomment-48425088 for details" + reason: "Your IP address could not be determined from the socket connection. See https://github.com/Automattic/socket.io/issues/1737 for details" }); return; } diff --git a/lib/server.js b/lib/server.js index 86a44dcb..4fcfd29e 100644 --- a/lib/server.js +++ b/lib/server.js @@ -45,6 +45,7 @@ var User = require("./user"); var $util = require("./utilities"); var db = require("./database"); var Flags = require("./flags"); +var sio = require("socket.io"); var Server = function () { var self = this; @@ -55,7 +56,6 @@ var Server = function () { self.announcement = null; self.infogetter = null; self.servers = {}; - self.ioServers = {}; // database init ------------------------------------------------------ var Database = require("./database"); @@ -207,9 +207,7 @@ Server.prototype.announce = function (data) { } else { this.announcement = data; db.setAnnouncement(data); - for (var id in this.ioServers) { - this.ioServers[id].sockets.emit("announcement", data); - } + sio.instance.emit("announcement", data); } }; From ddb4be0c5991f0b2415e65d6de626803cddb90c6 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Sun, 24 Aug 2014 13:38:34 -0500 Subject: [PATCH 7/7] Socket.io pls --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 5627b6bb..d698b68d 100644 --- a/package.json +++ b/package.json @@ -20,7 +20,7 @@ "oauth": "^0.9.12", "q": "^1.0.1", "serve-static": "^1.5.3", - "socket.io": "^1.0.6", + "socket.io": "Automattic/socket.io#5863903", "yamljs": "^0.1.5" } }