diff --git a/config.template.yaml b/config.template.yaml index 25ff7a09..daac867b 100644 --- a/config.template.yaml +++ b/config.template.yaml @@ -9,6 +9,7 @@ mysql: database: 'cytube3' user: 'cytube3' password: '' + pool-size: 10 # Define IPs/ports to listen on # Each entry MUST define ip and port (ip can be '' to bind all available addresses) diff --git a/package.json b/package.json index da2b9d6c..64d0ac21 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Calvin Montgomery", "name": "CyTube", "description": "Online media synchronizer and chat", - "version": "3.28.2", + "version": "3.29.0", "repository": { "url": "http://github.com/calzoneman/sync" }, diff --git a/src/channel/kickban.js b/src/channel/kickban.js index 0d11e0c9..aedf02c9 100644 --- a/src/channel/kickban.js +++ b/src/channel/kickban.js @@ -44,25 +44,28 @@ function checkNameBan(cname, name, cb) { }); } +function checkBan(cname, ip, name, cb) { + db.channels.isBanned(cname, ip, name, function (err, banned) { + if (err) { + cb(false); + } else { + cb(banned); + } + }); +} + KickBanModule.prototype.onUserPreJoin = function (user, data, cb) { if (!this.channel.is(Flags.C_REGISTERED)) { return cb(null, ChannelModule.PASSTHROUGH); } var cname = this.channel.name; - checkIPBan(cname, user.realip, function (banned) { + checkBan(cname, user.realip, user.getName(), function (banned) { if (banned) { cb(null, ChannelModule.DENY); - user.kick("Your IP address is banned from this channel."); + user.kick("You are banned from this channel."); } else { - checkNameBan(cname, user.getName(), function (banned) { - if (banned) { - cb(null, ChannelModule.DENY); - user.kick("Your username is banned from this channel."); - } else { - cb(null, ChannelModule.PASSTHROUGH); - } - }); + cb(null, ChannelModule.PASSTHROUGH); } }); diff --git a/src/config.js b/src/config.js index 80dc58c6..7fa285a3 100644 --- a/src/config.js +++ b/src/config.js @@ -12,6 +12,7 @@ var defaults = { database: "cytube3", user: "cytube3", password: "", + "pool-size": 10 }, listen: [ { diff --git a/src/database.js b/src/database.js index 3906f453..f155ee79 100644 --- a/src/database.js +++ b/src/database.js @@ -19,7 +19,8 @@ module.exports.init = function () { password: Config.get("mysql.password"), database: Config.get("mysql.database"), multipleStatements: true, - charset: "UTF8MB4_GENERAL_CI" // Needed for emoji and other non-BMP unicode + charset: "UTF8MB4_GENERAL_CI", // Needed for emoji and other non-BMP unicode + connectionLimit: Config.get("mysql.pool-size") }); // Test the connection @@ -40,6 +41,10 @@ module.exports.init = function () { } }); + pool.on("enqueue", function () { + Metrics.incCounter("db:queryQueued", 1); + }); + global_ipbans = {}; module.exports.users = require("./database/accounts"); module.exports.channels = require("./database/channels"); @@ -66,6 +71,7 @@ module.exports.query = function (query, sub, callback) { callback("Database failure", null); } else { function cback(err, res) { + conn.release(); if (err) { Logger.errlog.log("! DB query failed: " + query); if (sub) { @@ -76,7 +82,6 @@ module.exports.query = function (query, sub, callback) { } else { callback(null, res); } - conn.release(); Metrics.stopTimer(timer); } @@ -84,10 +89,16 @@ module.exports.query = function (query, sub, callback) { console.log(query); } - if (sub) { - conn.query(query, sub, cback); - } else { - conn.query(query, cback); + try { + if (sub) { + conn.query(query, sub, cback); + } else { + conn.query(query, cback); + } + } catch (error) { + Logger.errlog.log("Broken query: " + error.stack); + callback("Broken query", null); + conn.release(); } } }); diff --git a/src/database/channels.js b/src/database/channels.js index f7dce3d2..224a1e39 100644 --- a/src/database/channels.js +++ b/src/database/channels.js @@ -571,6 +571,29 @@ module.exports = { }); }, + /** + * Check if a user's name or IP is banned + */ + isBanned: function (chan, ip, name, callback) { + if (typeof callback !== "function") { + return; + } + + if (!valid(chan)) { + callback("Invalid channel name", null); + return; + } + + var range = util.getIPRange(ip); + var wrange = util.getWideIPRange(ip); + + db.query("SELECT COUNT(1) AS count FROM `channel_bans` WHERE (ip IN (?, ?, ?) OR name=?) AND channel=?", + [ip, range, wrange, name, chan], + function (err, rows) { + callback(err, err ? false : rows.length > 0 && rows[0].count > 0); + }); + }, + /** * Lists all bans */