From db86d3918fa8a828be160207b9e391bf241dc398 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Tue, 19 Aug 2014 22:57:28 -0500 Subject: [PATCH 01/14] 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 02/14] 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 03/14] 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 04/14] 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 05/14] 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 06/14] 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 07/14] 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" } } From 6e0735f3feabbcd04f683fd625ced92941f0ba7d Mon Sep 17 00:00:00 2001 From: calzoneman Date: Tue, 19 Aug 2014 22:57:28 -0500 Subject: [PATCH 08/14] 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 5a2af30e..fed1e703 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 }; @@ -159,10 +161,10 @@ function handleConnection(sock) { addTypecheckedFunctions(sock); var user = new User(sock); - if (sockUser) { + if (sock.user) { user.setFlag(Flags.U_REGISTERED); user.clearFlag(Flags.U_READY); - user.refreshAccount({ name: sockUser.name }, + user.refreshAccount({ name: sock.user.name }, function (err, account) { if (err) { user.clearFlag(Flags.U_REGISTERED); @@ -190,44 +192,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 291e9a4f..14a510d3 100644 --- a/www/js/callbacks.js +++ b/www/js/callbacks.js @@ -1106,17 +1106,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 50dd0982a426dae8b1e4403c72236a0d34a1340b Mon Sep 17 00:00:00 2001 From: calzoneman Date: Wed, 20 Aug 2014 11:44:37 -0500 Subject: [PATCH 09/14] Fix IP extraction for socket.io --- lib/io/ioserver.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/io/ioserver.js b/lib/io/ioserver.js index fed1e703..72ef6ff8 100644 --- a/lib/io/ioserver.js +++ b/lib/io/ioserver.js @@ -126,8 +126,7 @@ function addTypecheckedFunctions(sock) { * Called after a connection is accepted */ function handleConnection(sock) { - var ip = sock.handshake.address.address; - var sockUser = sock.handshake.user; + var ip = sock.request.connection.remoteAddress; if (net.isIPv6(ip)) { ip = util.expandIPv6(ip); } From b4bcb7637b146782cdf898b09c2c719c41899b71 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Wed, 20 Aug 2014 12:09:38 -0500 Subject: [PATCH 10/14] 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 289807535ac11bdea7d9969777712edd57653991 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Wed, 20 Aug 2014 12:11:46 -0500 Subject: [PATCH 11/14] 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 4967e463435d70358787da5018a5b95e021acb56 Mon Sep 17 00:00:00 2001 From: calzoneman Date: Sun, 24 Aug 2014 13:18:15 -0500 Subject: [PATCH 12/14] 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 3f62cd7dfb8973abd599d8df10936ab29be2f8cf Mon Sep 17 00:00:00 2001 From: calzoneman Date: Sun, 24 Aug 2014 13:38:34 -0500 Subject: [PATCH 13/14] 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" } } From 4c5d441931f28adcf01461ef4d96df8b7fc714bd Mon Sep 17 00:00:00 2001 From: calzoneman Date: Fri, 24 Oct 2014 10:30:23 -0500 Subject: [PATCH 14/14] Minor update --- package.json | 2 +- www/js/callbacks.js | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index d698b68d..60e2e597 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": "Automattic/socket.io#5863903", + "socket.io": "^1.1.0", "yamljs": "^0.1.5" } } diff --git a/www/js/callbacks.js b/www/js/callbacks.js index 14a510d3..3ee748b8 100644 --- a/www/js/callbacks.js +++ b/www/js/callbacks.js @@ -1106,11 +1106,12 @@ try { throw false; } + var opts = { transports: ["websocket", "polling"] }; if (IO_URL === IO_URLS["ipv4-ssl"] || IO_URL === IO_URLS["ipv6-ssl"]) { + opts.secure = true; socket = io(IO_URL, { secure: true }); - } else { - socket = io(IO_URL); } + socket = io(IO_URL, opts); setupCallbacks(); } catch (e) { if (e) {