Refactoring

This commit is contained in:
calzoneman 2013-08-17 18:44:48 -05:00
parent 65d7a8a455
commit b686deb16f
7 changed files with 45 additions and 45 deletions

2
acp.js
View File

@ -11,7 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
module.exports = function (Server) {
var db = Server.db;
var ActionLog = require("./actionlog")(Server);
var ActionLog = Server.actionlog;
return {
init: function(user) {
ActionLog.record(user.ip, user.name, "acp-init");

2
api.js
View File

@ -13,7 +13,7 @@ var Logger = require("./logger");
var fs = require("fs");
module.exports = function (Server) {
var ActionLog = require("./actionlog")(Server);
var ActionLog = Server.actionlog;
function getIP(req) {
var raw = req.connection.remoteAddress;
var forward = req.header("x-forwarded-for");

View File

@ -18,7 +18,6 @@ var Logger = require("./logger.js");
var Rank = require("./rank.js");
var ChatCommand = require("./chatcommand.js");
var Filter = require("./filter.js").Filter;
var ActionLog = require("./actionlog");
var Playlist = require("./playlist");
var sanitize = require("validator").sanitize;
@ -341,7 +340,7 @@ Channel.prototype.tryReadLog = function (user) {
Channel.prototype.tryRegister = function (user) {
var self = this;
if(self.registered) {
ActionLog.record(user.ip, user.name, "channel-register-failure",
self.server.actionlog.record(user.ip, user.name, "channel-register-failure",
[self.name, "Channel already registered"]);
user.socket.emit("registerChannel", {
success: false,
@ -349,7 +348,7 @@ Channel.prototype.tryRegister = function (user) {
});
}
else if(!user.loggedIn) {
ActionLog.record(user.ip, user.name, "channel-register-failure",
self.server.actionlog.record(user.ip, user.name, "channel-register-failure",
[self.name, "Not logged in"]);
user.socket.emit("registerChannel", {
success: false,
@ -358,7 +357,7 @@ Channel.prototype.tryRegister = function (user) {
}
else if(!Rank.hasPermission(user, "registerChannel")) {
ActionLog.record(user.ip, user.name, "channel-register-failure",
self.server.actionlog.record(user.ip, user.name, "channel-register-failure",
[self.name, "Insufficient permissions"]);
user.socket.emit("registerChannel", {
success: false,
@ -376,7 +375,7 @@ Channel.prototype.tryRegister = function (user) {
return;
}
ActionLog.record(user.ip, user.name,
self.server.actionlog.record(user.ip, user.name,
"channel-register-success", self.name);
self.registered = true;
self.initialized = true;

View File

@ -1353,6 +1353,25 @@ Database.prototype.listActions = function (types, callback) {
/* REGION stats */
Database.prototype.addStatPoint = function (time, ucount, ccount, mem,
callback) {
var self = this;
if(typeof callback !== "function")
callback = blackHole;
var query = "INSERT INTO stats VALUES (?, ?, ?, ?)";
self.query(query, [time, ucount, ccount, mem], callback);
};
Database.prototype.pruneStats = function (before, callback) {
var self = this;
if(typeof callback !== "function")
callback = blackHole;
var query = "DELETE FROM stats WHERE time < ?";
self.query(query, [before], callback);
};
Database.prototype.listStats = function (callback) {
var self = this;
if(typeof callback !== "function")

View File

@ -73,6 +73,7 @@ var Server = {
ips: {},
acp: null,
httpaccess: null,
actionlog: null,
logHTTP: function (req, status) {
if(status === undefined)
status = 200;
@ -88,6 +89,7 @@ var Server = {
this.httpaccess.log([ipstr, req.method, url, status, req.headers["user-agent"]].join(" "));
},
init: function () {
this.actionlog = require("./actionlog")(this);
this.httpaccess = new Logger.Logger("httpaccess.log");
this.app = express();
this.app.use(express.bodyParser());
@ -169,14 +171,15 @@ var Server = {
this.io.sockets.on("connection", function (socket) {
var ip = getSocketIP(socket);
socket._ip = ip;
if(this.db.checkGlobalBan(ip)) {
Logger.syslog.log("Disconnecting " + ip + " - gbanned");
socket.emit("kick", {
reason: "You're globally banned."
});
socket.disconnect(true);
return;
}
this.db.isGlobalIPBanned(ip, function (err, bant) {
if(bant) {
Logger.syslog.log("Disconnecting " + ip + " - gbanned");
socket.emit("kick", {
reason: "You're globally banned."
});
socket.disconnect(true);
}
});
socket.on("disconnect", function () {
this.ips[ip]--;
@ -200,8 +203,8 @@ var Server = {
}.bind(this));
// init database
this.db = require("./database");
this.db.setup(Server.cfg);
var Database = require("./database");
this.db = new Database(this.cfg);
this.db.init();
// init ACP

View File

@ -15,6 +15,7 @@ const STAT_INTERVAL = 60 * 60 * 1000;
const STAT_EXPIRE = 24 * STAT_INTERVAL;
module.exports = function (Server) {
var db = Server.db;
setInterval(function () {
var chancount = Server.channels.length;
var usercount = 0;
@ -24,29 +25,8 @@ module.exports = function (Server) {
var mem = process.memoryUsage().rss;
var db = Server.db.getConnection();
if(!db)
return;
var query = Server.db.createQuery(
"INSERT INTO stats VALUES (?, ?, ?, ?)",
[Date.now(), usercount, chancount, mem]
);
if(!db.querySync(query)) {
Logger.errlog.log("! Failed to record stats");
Logger.errlog.log(query);
}
query = Server.db.createQuery(
"DELETE FROM stats WHERE time<?",
[Date.now() - STAT_EXPIRE]
);
if(!db.querySync(query)) {
Logger.errlog.log("! Failed to prune stats");
Logger.errlog.log(query);
}
db.addStatPoint(Date.now(), usercount, chancount, mem, function () {
db.pruneStats(Date.now() - STAT_EXPIRE);
});
}, STAT_INTERVAL);
}

View File

@ -10,11 +10,10 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
*/
var Rank = require("./rank.js");
var Auth = require("./auth.js");
var Channel = require("./channel.js").Channel;
var formatTime = require("./media.js").formatTime;
var Logger = require("./logger.js");
var ActionLog = require("./actionlog");
var self.server.actionlog = require("./actionlog");
// Represents a client connected via socket.io
var User = function(socket, Server) {
@ -605,7 +604,7 @@ User.prototype.login = function(name, pw, session) {
} else {
self.server.db.userLogin(name, pw, session, function (err, row) {
if(err) {
ActionLog.record(self.ip, self.name, "login-failure");
self.server.actionlog.record(self.ip, self.name, "login-failure");
self.socket.emit("login", {
success: false,
error: err
@ -620,7 +619,7 @@ User.prototype.login = function(name, pw, session) {
}
}
if(self.global_rank >= 255)
ActionLog.record(self.ip, name, "login-success");
self.server.actionlog.record(self.ip, name, "login-success");
self.loggedIn = true;
self.socket.emit("login", {
success: true,