Switch to new DatabaseHelper
This commit is contained in:
parent
bc7624e001
commit
83d9e3aa75
7 changed files with 43 additions and 38 deletions
|
@ -1,31 +1,36 @@
|
||||||
const tcpx = require('tcp-netx'),
|
const mysql = require("mysql");
|
||||||
aes256 = require('aes256');
|
|
||||||
|
|
||||||
module.exports = class {
|
module.exports = class {
|
||||||
constructor(databaseAddress, databasePort, databaseKey) {
|
constructor(databaseAddress, databasePort = 3306, databaseUsername, databasePassword, databaseName) {
|
||||||
this.databaseKey = databaseKey;
|
this.connectionPool = mysql.createPool({
|
||||||
|
connectionLimit: 128,
|
||||||
// Create a new instance of tcp-netx and connect to the database
|
host: databaseAddress,
|
||||||
this.databaseServer = new tcpx.server(databaseAddress, databasePort);
|
port: databasePort,
|
||||||
this.databaseServer.connect();
|
user: databaseUsername,
|
||||||
|
password: databasePassword,
|
||||||
|
database: databaseName
|
||||||
// First response will always be broken
|
});
|
||||||
this.databaseServer.write({data: aes256.encrypt(this.databaseKey, `p|`)});
|
|
||||||
this.databaseServer.read();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
executeInDB(query) {
|
async query(sqlQuery) {
|
||||||
const result = this.databaseServer.write({data: aes256.encrypt(this.databaseKey, `r|${query}`)});
|
return new Promise((resolve, reject) => {
|
||||||
|
this.connectionPool.getConnection((err, connection) => {
|
||||||
if (result.ok == 1) return this.databaseServer.read();
|
if (err) {
|
||||||
else throw "Database error"
|
reject(err);
|
||||||
|
connection.release();
|
||||||
|
} else {
|
||||||
|
connection.query(sqlQuery, (err, data) => {
|
||||||
|
if (err) {
|
||||||
|
reject(err);
|
||||||
|
connection.release();
|
||||||
|
} else {
|
||||||
|
if (sqlQuery.includes("LIMIT 1")) resolve(data[0]);
|
||||||
|
else resolve(data);
|
||||||
|
connection.release();
|
||||||
}
|
}
|
||||||
|
});
|
||||||
getFromDB(query) {
|
}
|
||||||
const result = this.databaseServer.write({data: aes256.encrypt(this.databaseKey, `g|${query}`)});
|
});
|
||||||
|
});
|
||||||
if (result.ok == 1) return JSON.parse(aes256.decrypt(this.databaseKey, this.databaseServer.read()["data"]));
|
|
||||||
else throw "Database error";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,3 +1,3 @@
|
||||||
module.exports = function(CurrentUser, FriendToAdd) {
|
module.exports = function(CurrentUser, FriendToAdd) {
|
||||||
global.DatabaseHelper.executeInDB(`INSERT INTO friends (user, friendsWith) VALUES (${CurrentUser.id}, ${FriendToAdd});`);
|
global.DatabaseHelper.query(`INSERT INTO friends (user, friendsWith) VALUES (${CurrentUser.id}, ${FriendToAdd});`);
|
||||||
}
|
}
|
|
@ -1,3 +1,3 @@
|
||||||
module.exports = function(CurrentUser, FriendToRemove) {
|
module.exports = function(CurrentUser, FriendToRemove) {
|
||||||
global.DatabaseHelper.executeInDB(`DELETE FROM friends WHERE user = ${CurrentUser.id} AND friendsWith = ${FriendToRemove} LIMIT 1`);
|
global.DatabaseHelper.query(`DELETE FROM friends WHERE user = ${CurrentUser.id} AND friendsWith = ${FriendToRemove} LIMIT 1`);
|
||||||
}
|
}
|
|
@ -55,9 +55,9 @@ module.exports = class {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Gets the user's score information from the database and caches it
|
// Gets the user's score information from the database and caches it
|
||||||
getNewUserInformationFromDatabase() {
|
async getNewUserInformationFromDatabase() {
|
||||||
const userScoreDB = global.DatabaseHelper.getFromDB(`SELECT * FROM users_modes_info WHERE user_id = ${this.id} AND mode_id = ${this.playMode} LIMIT 1`);
|
const userScoreDB = await global.DatabaseHelper.query(`SELECT * FROM users_modes_info WHERE user_id = ${this.id} AND mode_id = ${this.playMode} LIMIT 1`);
|
||||||
const userRankDB = global.DatabaseHelper.getFromDB(`SELECT user_id, pp_raw FROM users_modes_info WHERE mode_id = ${this.playMode} ORDER BY pp_raw DESC`);
|
const userRankDB = await global.DatabaseHelper.query(`SELECT user_id, pp_raw FROM users_modes_info WHERE mode_id = ${this.playMode} ORDER BY pp_raw DESC`);
|
||||||
|
|
||||||
if (userScoreDB == null || userRankDB == null) throw "fuck";
|
if (userScoreDB == null || userRankDB == null) throw "fuck";
|
||||||
|
|
||||||
|
|
|
@ -11,12 +11,12 @@ const osu = require("osu-packet"),
|
||||||
UserPresence = require("./Packets/UserPresence.js"),
|
UserPresence = require("./Packets/UserPresence.js"),
|
||||||
StatusUpdate = require("./Packets/StatusUpdate.js");
|
StatusUpdate = require("./Packets/StatusUpdate.js");
|
||||||
|
|
||||||
module.exports = function(req, res, loginInfo) {
|
module.exports = async function(req, res, loginInfo) {
|
||||||
// Get time at the start of login
|
// Get time at the start of login
|
||||||
const loginStartTime = new Date().getTime();
|
const loginStartTime = new Date().getTime();
|
||||||
|
|
||||||
// Check login
|
// Check login
|
||||||
const loginCheck = loginHelper.checkLogin(loginInfo);
|
const loginCheck = await loginHelper.checkLogin(loginInfo);
|
||||||
if (loginCheck != null) {
|
if (loginCheck != null) {
|
||||||
res.writeHead(200, loginCheck[1]);
|
res.writeHead(200, loginCheck[1]);
|
||||||
return res.end(loginCheck[0]);
|
return res.end(loginCheck[0]);
|
||||||
|
@ -45,7 +45,7 @@ module.exports = function(req, res, loginInfo) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get information about the user from the database
|
// Get information about the user from the database
|
||||||
const userDB = global.DatabaseHelper.getFromDB(`SELECT id FROM users_info WHERE username = "${loginInfo.username}" LIMIT 1`);
|
const userDB = await global.DatabaseHelper.query(`SELECT id FROM users_info WHERE username = "${loginInfo.username}" LIMIT 1`);
|
||||||
|
|
||||||
// Create a token for the client
|
// Create a token for the client
|
||||||
const newClientToken = uuid();
|
const newClientToken = uuid();
|
||||||
|
@ -128,7 +128,7 @@ module.exports = function(req, res, loginInfo) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Construct user's friends list
|
// Construct user's friends list
|
||||||
const userFriends = global.DatabaseHelper.getFromDB(`SELECT friendsWith FROM friends WHERE user = ${NewUser.id}`);
|
const userFriends = await global.DatabaseHelper.query(`SELECT friendsWith FROM friends WHERE user = ${NewUser.id}`);
|
||||||
let friendsArray = [];
|
let friendsArray = [];
|
||||||
for (let i = 0; i < userFriends.length; i++) {
|
for (let i = 0; i < userFriends.length; i++) {
|
||||||
friendsArray.push(userFriends[i].friendsWith);
|
friendsArray.push(userFriends[i].friendsWith);
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
const osu = require("osu-packet");
|
const osu = require("osu-packet");
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
checkLogin:function(loginInfo) {
|
checkLogin:async function(loginInfo) {
|
||||||
// Queue up incorrect login response
|
// Queue up incorrect login response
|
||||||
const incorrectDetailsResponse = incorrectLoginResponse();
|
const incorrectDetailsResponse = incorrectLoginResponse();
|
||||||
// Check if there is any login information provided
|
// Check if there is any login information provided
|
||||||
if (loginInfo == null) return incorrectDetailsResponse;
|
if (loginInfo == null) return incorrectDetailsResponse;
|
||||||
|
|
||||||
const userDBData = global.DatabaseHelper.getFromDB(`SELECT * FROM users_info WHERE username = "${loginInfo.username}" LIMIT 1`);
|
const userDBData = await global.DatabaseHelper.query(`SELECT * FROM users_info WHERE username = "${loginInfo.username}" LIMIT 1`);
|
||||||
|
|
||||||
// Make sure a user was found in the database
|
// Make sure a user was found in the database
|
||||||
if (Object.keys(userDBData).length < 1) return incorrectDetailsResponse;
|
if (Object.keys(userDBData).length < 1) return incorrectDetailsResponse;
|
||||||
|
|
|
@ -19,7 +19,7 @@ global.users = [
|
||||||
global.users[0].location[0] = 50;
|
global.users[0].location[0] = 50;
|
||||||
global.users[0].location[1] = -32;
|
global.users[0].location[1] = -32;
|
||||||
|
|
||||||
global.DatabaseHelper = new DatabaseHelperClass(config.databaseAddress, config.databasePort, config.databaseKey);
|
global.DatabaseHelper = new DatabaseHelperClass(config.databaseAddress, config.databasePort, config.databaseUsername, config.databasePassword, config.databaseName);
|
||||||
|
|
||||||
|
|
||||||
// Start a loop that gets new data for users from the database for use on the user panel
|
// Start a loop that gets new data for users from the database for use on the user panel
|
||||||
|
@ -136,7 +136,7 @@ module.exports = async function(req, res) {
|
||||||
// Client doesn't have a token yet, let's auth them!
|
// Client doesn't have a token yet, let's auth them!
|
||||||
const userData = parseUserData(requestData);
|
const userData = parseUserData(requestData);
|
||||||
global.consoleHelper.printBancho(`New client connection. [User: ${userData.username}]`);
|
global.consoleHelper.printBancho(`New client connection. [User: ${userData.username}]`);
|
||||||
loginHandler(req, res, userData);
|
await loginHandler(req, res, userData);
|
||||||
} else {
|
} else {
|
||||||
// Client has a token, let's see what they want.
|
// Client has a token, let's see what they want.
|
||||||
try {
|
try {
|
||||||
|
|
Loading…
Reference in a new issue