mirror of https://github.com/calzoneman/sync.git
Refactoring
This commit is contained in:
parent
65d7a8a455
commit
b686deb16f
2
acp.js
2
acp.js
|
@ -11,7 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
||||||
|
|
||||||
module.exports = function (Server) {
|
module.exports = function (Server) {
|
||||||
var db = Server.db;
|
var db = Server.db;
|
||||||
var ActionLog = require("./actionlog")(Server);
|
var ActionLog = Server.actionlog;
|
||||||
return {
|
return {
|
||||||
init: function(user) {
|
init: function(user) {
|
||||||
ActionLog.record(user.ip, user.name, "acp-init");
|
ActionLog.record(user.ip, user.name, "acp-init");
|
||||||
|
|
2
api.js
2
api.js
|
@ -13,7 +13,7 @@ var Logger = require("./logger");
|
||||||
var fs = require("fs");
|
var fs = require("fs");
|
||||||
|
|
||||||
module.exports = function (Server) {
|
module.exports = function (Server) {
|
||||||
var ActionLog = require("./actionlog")(Server);
|
var ActionLog = Server.actionlog;
|
||||||
function getIP(req) {
|
function getIP(req) {
|
||||||
var raw = req.connection.remoteAddress;
|
var raw = req.connection.remoteAddress;
|
||||||
var forward = req.header("x-forwarded-for");
|
var forward = req.header("x-forwarded-for");
|
||||||
|
|
|
@ -18,7 +18,6 @@ var Logger = require("./logger.js");
|
||||||
var Rank = require("./rank.js");
|
var Rank = require("./rank.js");
|
||||||
var ChatCommand = require("./chatcommand.js");
|
var ChatCommand = require("./chatcommand.js");
|
||||||
var Filter = require("./filter.js").Filter;
|
var Filter = require("./filter.js").Filter;
|
||||||
var ActionLog = require("./actionlog");
|
|
||||||
var Playlist = require("./playlist");
|
var Playlist = require("./playlist");
|
||||||
var sanitize = require("validator").sanitize;
|
var sanitize = require("validator").sanitize;
|
||||||
|
|
||||||
|
@ -341,7 +340,7 @@ Channel.prototype.tryReadLog = function (user) {
|
||||||
Channel.prototype.tryRegister = function (user) {
|
Channel.prototype.tryRegister = function (user) {
|
||||||
var self = this;
|
var self = this;
|
||||||
if(self.registered) {
|
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"]);
|
[self.name, "Channel already registered"]);
|
||||||
user.socket.emit("registerChannel", {
|
user.socket.emit("registerChannel", {
|
||||||
success: false,
|
success: false,
|
||||||
|
@ -349,7 +348,7 @@ Channel.prototype.tryRegister = function (user) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else if(!user.loggedIn) {
|
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"]);
|
[self.name, "Not logged in"]);
|
||||||
user.socket.emit("registerChannel", {
|
user.socket.emit("registerChannel", {
|
||||||
success: false,
|
success: false,
|
||||||
|
@ -358,7 +357,7 @@ Channel.prototype.tryRegister = function (user) {
|
||||||
|
|
||||||
}
|
}
|
||||||
else if(!Rank.hasPermission(user, "registerChannel")) {
|
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"]);
|
[self.name, "Insufficient permissions"]);
|
||||||
user.socket.emit("registerChannel", {
|
user.socket.emit("registerChannel", {
|
||||||
success: false,
|
success: false,
|
||||||
|
@ -376,7 +375,7 @@ Channel.prototype.tryRegister = function (user) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
ActionLog.record(user.ip, user.name,
|
self.server.actionlog.record(user.ip, user.name,
|
||||||
"channel-register-success", self.name);
|
"channel-register-success", self.name);
|
||||||
self.registered = true;
|
self.registered = true;
|
||||||
self.initialized = true;
|
self.initialized = true;
|
||||||
|
|
19
database.js
19
database.js
|
@ -1353,6 +1353,25 @@ Database.prototype.listActions = function (types, callback) {
|
||||||
|
|
||||||
/* REGION stats */
|
/* 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) {
|
Database.prototype.listStats = function (callback) {
|
||||||
var self = this;
|
var self = this;
|
||||||
if(typeof callback !== "function")
|
if(typeof callback !== "function")
|
||||||
|
|
23
server.js
23
server.js
|
@ -73,6 +73,7 @@ var Server = {
|
||||||
ips: {},
|
ips: {},
|
||||||
acp: null,
|
acp: null,
|
||||||
httpaccess: null,
|
httpaccess: null,
|
||||||
|
actionlog: null,
|
||||||
logHTTP: function (req, status) {
|
logHTTP: function (req, status) {
|
||||||
if(status === undefined)
|
if(status === undefined)
|
||||||
status = 200;
|
status = 200;
|
||||||
|
@ -88,6 +89,7 @@ var Server = {
|
||||||
this.httpaccess.log([ipstr, req.method, url, status, req.headers["user-agent"]].join(" "));
|
this.httpaccess.log([ipstr, req.method, url, status, req.headers["user-agent"]].join(" "));
|
||||||
},
|
},
|
||||||
init: function () {
|
init: function () {
|
||||||
|
this.actionlog = require("./actionlog")(this);
|
||||||
this.httpaccess = new Logger.Logger("httpaccess.log");
|
this.httpaccess = new Logger.Logger("httpaccess.log");
|
||||||
this.app = express();
|
this.app = express();
|
||||||
this.app.use(express.bodyParser());
|
this.app.use(express.bodyParser());
|
||||||
|
@ -169,14 +171,15 @@ var Server = {
|
||||||
this.io.sockets.on("connection", function (socket) {
|
this.io.sockets.on("connection", function (socket) {
|
||||||
var ip = getSocketIP(socket);
|
var ip = getSocketIP(socket);
|
||||||
socket._ip = ip;
|
socket._ip = ip;
|
||||||
if(this.db.checkGlobalBan(ip)) {
|
this.db.isGlobalIPBanned(ip, function (err, bant) {
|
||||||
Logger.syslog.log("Disconnecting " + ip + " - gbanned");
|
if(bant) {
|
||||||
socket.emit("kick", {
|
Logger.syslog.log("Disconnecting " + ip + " - gbanned");
|
||||||
reason: "You're globally banned."
|
socket.emit("kick", {
|
||||||
});
|
reason: "You're globally banned."
|
||||||
socket.disconnect(true);
|
});
|
||||||
return;
|
socket.disconnect(true);
|
||||||
}
|
}
|
||||||
|
});
|
||||||
|
|
||||||
socket.on("disconnect", function () {
|
socket.on("disconnect", function () {
|
||||||
this.ips[ip]--;
|
this.ips[ip]--;
|
||||||
|
@ -200,8 +203,8 @@ var Server = {
|
||||||
}.bind(this));
|
}.bind(this));
|
||||||
|
|
||||||
// init database
|
// init database
|
||||||
this.db = require("./database");
|
var Database = require("./database");
|
||||||
this.db.setup(Server.cfg);
|
this.db = new Database(this.cfg);
|
||||||
this.db.init();
|
this.db.init();
|
||||||
|
|
||||||
// init ACP
|
// init ACP
|
||||||
|
|
28
stats.js
28
stats.js
|
@ -15,6 +15,7 @@ const STAT_INTERVAL = 60 * 60 * 1000;
|
||||||
const STAT_EXPIRE = 24 * STAT_INTERVAL;
|
const STAT_EXPIRE = 24 * STAT_INTERVAL;
|
||||||
|
|
||||||
module.exports = function (Server) {
|
module.exports = function (Server) {
|
||||||
|
var db = Server.db;
|
||||||
setInterval(function () {
|
setInterval(function () {
|
||||||
var chancount = Server.channels.length;
|
var chancount = Server.channels.length;
|
||||||
var usercount = 0;
|
var usercount = 0;
|
||||||
|
@ -24,29 +25,8 @@ module.exports = function (Server) {
|
||||||
|
|
||||||
var mem = process.memoryUsage().rss;
|
var mem = process.memoryUsage().rss;
|
||||||
|
|
||||||
var db = Server.db.getConnection();
|
db.addStatPoint(Date.now(), usercount, chancount, mem, function () {
|
||||||
if(!db)
|
db.pruneStats(Date.now() - STAT_EXPIRE);
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}, STAT_INTERVAL);
|
}, STAT_INTERVAL);
|
||||||
}
|
}
|
||||||
|
|
7
user.js
7
user.js
|
@ -10,11 +10,10 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var Rank = require("./rank.js");
|
var Rank = require("./rank.js");
|
||||||
var Auth = require("./auth.js");
|
|
||||||
var Channel = require("./channel.js").Channel;
|
var Channel = require("./channel.js").Channel;
|
||||||
var formatTime = require("./media.js").formatTime;
|
var formatTime = require("./media.js").formatTime;
|
||||||
var Logger = require("./logger.js");
|
var Logger = require("./logger.js");
|
||||||
var ActionLog = require("./actionlog");
|
var self.server.actionlog = require("./actionlog");
|
||||||
|
|
||||||
// Represents a client connected via socket.io
|
// Represents a client connected via socket.io
|
||||||
var User = function(socket, Server) {
|
var User = function(socket, Server) {
|
||||||
|
@ -605,7 +604,7 @@ User.prototype.login = function(name, pw, session) {
|
||||||
} else {
|
} else {
|
||||||
self.server.db.userLogin(name, pw, session, function (err, row) {
|
self.server.db.userLogin(name, pw, session, function (err, row) {
|
||||||
if(err) {
|
if(err) {
|
||||||
ActionLog.record(self.ip, self.name, "login-failure");
|
self.server.actionlog.record(self.ip, self.name, "login-failure");
|
||||||
self.socket.emit("login", {
|
self.socket.emit("login", {
|
||||||
success: false,
|
success: false,
|
||||||
error: err
|
error: err
|
||||||
|
@ -620,7 +619,7 @@ User.prototype.login = function(name, pw, session) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(self.global_rank >= 255)
|
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.loggedIn = true;
|
||||||
self.socket.emit("login", {
|
self.socket.emit("login", {
|
||||||
success: true,
|
success: true,
|
||||||
|
|
Loading…
Reference in New Issue