Add config option for mysql pool size, optimize restart login flood case

This commit is contained in:
Calvin Montgomery 2017-02-02 23:05:50 -08:00
parent 3020060627
commit 5487d15bdf
6 changed files with 56 additions and 17 deletions

View File

@ -9,6 +9,7 @@ mysql:
database: 'cytube3' database: 'cytube3'
user: 'cytube3' user: 'cytube3'
password: '' password: ''
pool-size: 10
# Define IPs/ports to listen on # Define IPs/ports to listen on
# Each entry MUST define ip and port (ip can be '' to bind all available addresses) # Each entry MUST define ip and port (ip can be '' to bind all available addresses)

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "3.28.2", "version": "3.29.0",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },

View File

@ -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) { KickBanModule.prototype.onUserPreJoin = function (user, data, cb) {
if (!this.channel.is(Flags.C_REGISTERED)) { if (!this.channel.is(Flags.C_REGISTERED)) {
return cb(null, ChannelModule.PASSTHROUGH); return cb(null, ChannelModule.PASSTHROUGH);
} }
var cname = this.channel.name; var cname = this.channel.name;
checkIPBan(cname, user.realip, function (banned) { checkBan(cname, user.realip, user.getName(), function (banned) {
if (banned) { if (banned) {
cb(null, ChannelModule.DENY); cb(null, ChannelModule.DENY);
user.kick("Your IP address is banned from this channel."); user.kick("You are banned from this channel.");
} else { } else {
checkNameBan(cname, user.getName(), function (banned) { cb(null, ChannelModule.PASSTHROUGH);
if (banned) {
cb(null, ChannelModule.DENY);
user.kick("Your username is banned from this channel.");
} else {
cb(null, ChannelModule.PASSTHROUGH);
}
});
} }
}); });

View File

@ -12,6 +12,7 @@ var defaults = {
database: "cytube3", database: "cytube3",
user: "cytube3", user: "cytube3",
password: "", password: "",
"pool-size": 10
}, },
listen: [ listen: [
{ {

View File

@ -19,7 +19,8 @@ module.exports.init = function () {
password: Config.get("mysql.password"), password: Config.get("mysql.password"),
database: Config.get("mysql.database"), database: Config.get("mysql.database"),
multipleStatements: true, 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 // Test the connection
@ -40,6 +41,10 @@ module.exports.init = function () {
} }
}); });
pool.on("enqueue", function () {
Metrics.incCounter("db:queryQueued", 1);
});
global_ipbans = {}; global_ipbans = {};
module.exports.users = require("./database/accounts"); module.exports.users = require("./database/accounts");
module.exports.channels = require("./database/channels"); module.exports.channels = require("./database/channels");
@ -66,6 +71,7 @@ module.exports.query = function (query, sub, callback) {
callback("Database failure", null); callback("Database failure", null);
} else { } else {
function cback(err, res) { function cback(err, res) {
conn.release();
if (err) { if (err) {
Logger.errlog.log("! DB query failed: " + query); Logger.errlog.log("! DB query failed: " + query);
if (sub) { if (sub) {
@ -76,7 +82,6 @@ module.exports.query = function (query, sub, callback) {
} else { } else {
callback(null, res); callback(null, res);
} }
conn.release();
Metrics.stopTimer(timer); Metrics.stopTimer(timer);
} }
@ -84,10 +89,16 @@ module.exports.query = function (query, sub, callback) {
console.log(query); console.log(query);
} }
if (sub) { try {
conn.query(query, sub, cback); if (sub) {
} else { conn.query(query, sub, cback);
conn.query(query, cback); } else {
conn.query(query, cback);
}
} catch (error) {
Logger.errlog.log("Broken query: " + error.stack);
callback("Broken query", null);
conn.release();
} }
} }
}); });

View File

@ -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 * Lists all bans
*/ */