diff --git a/server/DatabaseHelper.js b/server/DatabaseHelper.js index c732e2b..a2f2b2f 100644 --- a/server/DatabaseHelper.js +++ b/server/DatabaseHelper.js @@ -1,31 +1,36 @@ -const tcpx = require('tcp-netx'), - aes256 = require('aes256'); +const mysql = require("mysql"); module.exports = class { - constructor(databaseAddress, databasePort, databaseKey) { - this.databaseKey = databaseKey; - - // Create a new instance of tcp-netx and connect to the database - this.databaseServer = new tcpx.server(databaseAddress, databasePort); - this.databaseServer.connect(); - - - // First response will always be broken - this.databaseServer.write({data: aes256.encrypt(this.databaseKey, `p|`)}); - this.databaseServer.read(); + constructor(databaseAddress, databasePort = 3306, databaseUsername, databasePassword, databaseName) { + this.connectionPool = mysql.createPool({ + connectionLimit: 128, + host: databaseAddress, + port: databasePort, + user: databaseUsername, + password: databasePassword, + database: databaseName + }); } - executeInDB(query) { - const result = this.databaseServer.write({data: aes256.encrypt(this.databaseKey, `r|${query}`)}); - - if (result.ok == 1) return this.databaseServer.read(); - else throw "Database error" - } - - 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"; + async query(sqlQuery) { + return new Promise((resolve, reject) => { + this.connectionPool.getConnection((err, connection) => { + if (err) { + 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(); + } + }); + } + }); + }); } } \ No newline at end of file diff --git a/server/Packets/AddFriend.js b/server/Packets/AddFriend.js index 75673e1..e7389f8 100644 --- a/server/Packets/AddFriend.js +++ b/server/Packets/AddFriend.js @@ -1,3 +1,3 @@ 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});`); } \ No newline at end of file diff --git a/server/Packets/RemoveFriend.js b/server/Packets/RemoveFriend.js index 9faee74..8e1137f 100644 --- a/server/Packets/RemoveFriend.js +++ b/server/Packets/RemoveFriend.js @@ -1,3 +1,3 @@ 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`); } \ No newline at end of file diff --git a/server/User.js b/server/User.js index 040335e..41c4a7f 100644 --- a/server/User.js +++ b/server/User.js @@ -55,9 +55,9 @@ module.exports = class { } // Gets the user's score information from the database and caches it - getNewUserInformationFromDatabase() { - const userScoreDB = global.DatabaseHelper.getFromDB(`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`); + async getNewUserInformationFromDatabase() { + 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 = 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"; diff --git a/server/loginHandler.js b/server/loginHandler.js index 96d11ca..05abbcf 100644 --- a/server/loginHandler.js +++ b/server/loginHandler.js @@ -11,12 +11,12 @@ const osu = require("osu-packet"), UserPresence = require("./Packets/UserPresence.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 const loginStartTime = new Date().getTime(); // Check login - const loginCheck = loginHelper.checkLogin(loginInfo); + const loginCheck = await loginHelper.checkLogin(loginInfo); if (loginCheck != null) { res.writeHead(200, loginCheck[1]); return res.end(loginCheck[0]); @@ -45,7 +45,7 @@ module.exports = function(req, res, loginInfo) { } // 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 const newClientToken = uuid(); @@ -128,7 +128,7 @@ module.exports = function(req, res, loginInfo) { } // 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 = []; for (let i = 0; i < userFriends.length; i++) { friendsArray.push(userFriends[i].friendsWith); diff --git a/server/loginHelper.js b/server/loginHelper.js index 263bf9b..ff471ea 100644 --- a/server/loginHelper.js +++ b/server/loginHelper.js @@ -1,13 +1,13 @@ const osu = require("osu-packet"); module.exports = { - checkLogin:function(loginInfo) { + checkLogin:async function(loginInfo) { // Queue up incorrect login response const incorrectDetailsResponse = incorrectLoginResponse(); // Check if there is any login information provided 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 if (Object.keys(userDBData).length < 1) return incorrectDetailsResponse; diff --git a/server/serverHandler.js b/server/serverHandler.js index 1049841..cc6c709 100644 --- a/server/serverHandler.js +++ b/server/serverHandler.js @@ -19,7 +19,7 @@ global.users = [ global.users[0].location[0] = 50; 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 @@ -136,7 +136,7 @@ module.exports = async function(req, res) { // Client doesn't have a token yet, let's auth them! const userData = parseUserData(requestData); global.consoleHelper.printBancho(`New client connection. [User: ${userData.username}]`); - loginHandler(req, res, userData); + await loginHandler(req, res, userData); } else { // Client has a token, let's see what they want. try {