diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 195a452..0000000 --- a/.gitignore +++ /dev/null @@ -1 +0,0 @@ -server/DatabaseHelper.js \ No newline at end of file diff --git a/Binato.js b/Binato.js index aadf178..3aa89c9 100644 --- a/Binato.js +++ b/Binato.js @@ -3,7 +3,8 @@ console.clear(); const app = require("express")(), fs = require("fs"), busboy = require("connect-busboy"), - osu = require("osu-packet"); + osu = require("osu-packet"), + config = require("./config.json"); const debugMode = true; diff --git a/server/DatabaseHelper.js b/server/DatabaseHelper.js new file mode 100644 index 0000000..c732e2b --- /dev/null +++ b/server/DatabaseHelper.js @@ -0,0 +1,31 @@ +const tcpx = require('tcp-netx'), + aes256 = require('aes256'); + +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(); + } + + 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"; + } +} \ No newline at end of file diff --git a/server/Multiplayer.js b/server/Multiplayer.js index deee157..9efd659 100644 --- a/server/Multiplayer.js +++ b/server/Multiplayer.js @@ -1,6 +1,5 @@ const osu = require("osu-packet"), getUserById = require("./util/getUserById.js"), - DatabaseHelper = require("./DatabaseHelper.js"), StatusUpdate = require("./Packets/StatusUpdate.js"); module.exports = { @@ -32,7 +31,7 @@ module.exports = { const User = getUserById(slot.playerId); // Get user score info from the database - const userScoreDB = DatabaseHelper.getFromDB(`SELECT * FROM users_modes_info WHERE user_id = ${User.id} AND mode_id = ${User.playMode} LIMIT 1`); + const userScoreDB = global.DatabaseHelper.getFromDB(`SELECT * FROM users_modes_info WHERE user_id = ${User.id} AND mode_id = ${User.playMode} LIMIT 1`); let UserStatusObject = { userId: User.id, @@ -88,7 +87,7 @@ module.exports = { const User = getUserById(slot.playerId); // Get user score info from the database - const userScoreDB = DatabaseHelper.getFromDB(`SELECT * FROM users_modes_info WHERE user_id = ${User.id} AND mode_id = ${User.playMode} LIMIT 1`); + const userScoreDB = global.DatabaseHelper.getFromDB(`SELECT * FROM users_modes_info WHERE user_id = ${User.id} AND mode_id = ${User.playMode} LIMIT 1`); let UserStatusObject = { userId: User.id, diff --git a/server/Packets/AddFriend.js b/server/Packets/AddFriend.js index 4cf64fa..75673e1 100644 --- a/server/Packets/AddFriend.js +++ b/server/Packets/AddFriend.js @@ -1,5 +1,3 @@ -const DatabaseHelper = require("../DatabaseHelper.js"); - module.exports = function(CurrentUser, FriendToAdd) { - DatabaseHelper.getFromDB(`INSERT INTO friends (user, friendsWith) VALUES (${CurrentUser.id}, ${FriendToAdd});`); + global.DatabaseHelper.executeInDB(`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 d072368..9faee74 100644 --- a/server/Packets/RemoveFriend.js +++ b/server/Packets/RemoveFriend.js @@ -1,5 +1,3 @@ -const DatabaseHelper = require("../DatabaseHelper.js"); - module.exports = function(CurrentUser, FriendToRemove) { - DatabaseHelper.getFromDB(`DELETE FROM friends WHERE user = ${CurrentUser.id} AND friendsWith = ${FriendToRemove} LIMIT 1`); + global.DatabaseHelper.executeInDB(`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 178c9de..23d0d18 100644 --- a/server/User.js +++ b/server/User.js @@ -1,5 +1,3 @@ -const DatabaseHelper = require("./DatabaseHelper.js"); - module.exports = class { constructor(id, username, uuid, connectTime) { this.id = id; @@ -52,8 +50,8 @@ module.exports = class { // Gets the user's score information from the database and caches it getNewUserInformationFromDatabase() { - const userScoreDB = DatabaseHelper.getFromDB(`SELECT * FROM users_modes_info WHERE user_id = ${this.id} AND mode_id = ${this.playMode} LIMIT 1`); - const userRankDB = DatabaseHelper.getFromDB(`SELECT user_id, pp_raw, FIND_IN_SET( pp_raw, ( SELECT GROUP_CONCAT( pp_raw ORDER BY pp_raw DESC ) FROM users_modes_info WHERE mode_id = ${this.playMode} ) ) AS rank FROM users_modes_info WHERE user_id = ${this.id} AND mode_id = ${this.playMode}`); + 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, FIND_IN_SET( pp_raw, ( SELECT GROUP_CONCAT( pp_raw ORDER BY pp_raw DESC ) FROM users_modes_info WHERE mode_id = ${this.playMode} ) ) AS rank FROM users_modes_info WHERE user_id = ${this.id} AND mode_id = ${this.playMode}`); if (userScoreDB == null || userRankDB == null) throw "fuck"; diff --git a/server/loginHandler.js b/server/loginHandler.js index 6d025a7..f5ca4d8 100644 --- a/server/loginHandler.js +++ b/server/loginHandler.js @@ -3,7 +3,6 @@ const osu = require("osu-packet"), { v4: uuid } = require('uuid'), request = require("sync-request"), - DatabaseHelper = require("./DatabaseHelper.js"), getUserByUsername = require("./util/getUserByUsername.js"), getUserByToken = require("./util/getUserByToken.js"), countryHelper = require("./countryHelper.js"), @@ -38,7 +37,7 @@ module.exports = function(req, res, loginInfo) { } // Get information about the user from the database - const userDB = DatabaseHelper.getFromDB(`SELECT id FROM users_info WHERE username = "${loginInfo.username}" LIMIT 1`); + const userDB = global.DatabaseHelper.getFromDB(`SELECT id FROM users_info WHERE username = "${loginInfo.username}" LIMIT 1`); // Create a token for the client const newClientToken = uuid(); @@ -88,7 +87,7 @@ module.exports = function(req, res, loginInfo) { // Permission level 4 is osu!supporter osuPacketWriter.LoginPermissions(4); // Construct user's friends list - const userFriends = DatabaseHelper.getListFromDB(`SELECT friendsWith FROM friends WHERE user = ${userClass.id}`); + const userFriends = global.DatabaseHelper.getFromDB(`SELECT friendsWith FROM friends WHERE user = ${userClass.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 b8ff0f7..32ff74b 100644 --- a/server/loginHelper.js +++ b/server/loginHelper.js @@ -1,5 +1,4 @@ -const DatabaseHelper = require("./DatabaseHelper.js"), - osu = require("osu-packet"); +const osu = require("osu-packet"); module.exports = { checkLogin:function(loginInfo) { @@ -8,7 +7,7 @@ module.exports = { // Check if there is any login information provided if (loginInfo == null) return incorrectDetailsResponse; - const userDBData = DatabaseHelper.getFromDB(`SELECT * FROM users_info WHERE username = "${loginInfo.username}" LIMIT 1`); + const userDBData = global.DatabaseHelper.getFromDB(`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 423847d..8ca2a4d 100644 --- a/server/serverHandler.js +++ b/server/serverHandler.js @@ -6,12 +6,22 @@ const osu = require("osu-packet"), User = require("./User.js"), getUserFromToken = require("./util/getUserByToken.js"), bakedResponses = require("./bakedResponses.js"), - Streams = require("./Streams.js"); + Streams = require("./Streams.js"), + DatabaseHelperClass = require("./DatabaseHelper.js"), + config = require("../config.json"); global.users = [ new User(3, "SillyBot", "SillyBot", new Date().getTime()) ]; +// Set the bot's position on the map +// First user will always be the bot +global.users[0].location[0] = 50; +global.users[0].location[1] = -32; + +global.DatabaseHelper = new DatabaseHelperClass(config.databaseAddress, config.databasePort, config.databaseKey); + + // Start a loop that gets new data for users from the database for use on the user panel // TODO: Some way of informing bancho that a user has set a score so details can be pulled down quickly // Possible solution, TCP socket between the score submit server and bancho? redis? (score submit is on a different server, redis probably wouldn't work) @@ -25,10 +35,6 @@ setInterval(() => { } }, 10000); -// Set the bot's position on the map -global.users[0].location[0] = 50; -global.users[0].location[1] = -32; - // An array containing all currently active multiplayer matches global.matches = []; // An array containing the last 15 messages in chat