From 20988971040a7aeca54f8a644f71b2951d42bb4a Mon Sep 17 00:00:00 2001 From: samo_lego <34912839+samolego@users.noreply.github.com> Date: Wed, 7 Oct 2020 22:27:44 +0200 Subject: [PATCH] Starting with MongoDB --- build.gradle | 7 +- .../org/samo_lego/simpleauth/SimpleAuth.java | 4 +- .../simpleauth/storage/AuthConfig.java | 26 +++- ...{SimpleAuthDatabase.java => DBHelper.java} | 20 ++- .../simpleauth/storage/database/LevelDB.java | 136 ++++++++++++++++++ .../simpleauth/storage/database/MongoDB.java | 32 +++++ 6 files changed, 208 insertions(+), 17 deletions(-) rename src/main/java/org/samo_lego/simpleauth/storage/{SimpleAuthDatabase.java => DBHelper.java} (87%) create mode 100644 src/main/java/org/samo_lego/simpleauth/storage/database/LevelDB.java create mode 100644 src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java diff --git a/build.gradle b/build.gradle index c8f4d4a..ee2d616 100644 --- a/build.gradle +++ b/build.gradle @@ -45,12 +45,17 @@ dependencies { include "at.favre.lib:bytes:${bytes_version}" // Storage - // leveldb database + // LevelDB database implementation group: 'org.iq80.leveldb', name: 'leveldb', version: '0.12' implementation group: 'org.iq80.leveldb', name: 'leveldb-api', version: '0.12' include group: 'org.iq80.leveldb', name: 'leveldb', version: '0.12' include group: 'org.iq80.leveldb', name: 'leveldb-api', version: '0.12' + // MongoDB driver + implementation 'org.mongodb:mongodb-driver:3.12.7' + include 'org.mongodb:mongodb-driver:3.12.7' + + // JNA lib include 'net.java.dev.jna:jna:5.5.0' diff --git a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java index 76b0876..5331f12 100644 --- a/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java +++ b/src/main/java/org/samo_lego/simpleauth/SimpleAuth.java @@ -24,7 +24,7 @@ import org.samo_lego.simpleauth.event.item.DropItemCallback; import org.samo_lego.simpleauth.event.item.TakeItemCallback; import org.samo_lego.simpleauth.storage.AuthConfig; import org.samo_lego.simpleauth.storage.PlayerCache; -import org.samo_lego.simpleauth.storage.SimpleAuthDatabase; +import org.samo_lego.simpleauth.storage.DBHelper; import java.io.File; import java.io.FileReader; @@ -46,7 +46,7 @@ import static org.samo_lego.simpleauth.utils.UuidConverter.convertUuid; public class SimpleAuth implements DedicatedServerModInitializer { - public static SimpleAuthDatabase DB = new SimpleAuthDatabase(); + public static DBHelper DB = new DBHelper(); public static final ExecutorService THREADPOOL = Executors.newCachedThreadPool(); diff --git a/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java b/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java index dfa379c..242bdc3 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/AuthConfig.java @@ -86,9 +86,9 @@ public class AuthConfig { public boolean allowFalling = false; /** - * Whether to tp player to spawn when joining (to hide coordinates) + * Whether to tp player to spawn when joining (to hide coordinates). */ - public boolean spawnOnJoin = false; + public boolean spawnOnJoin = false; /** * Data for spawn (where deauthenticated players are teleported). @@ -103,6 +103,27 @@ public class AuthConfig { public double y; public double z; } + + /** + * Whether to use MongoDB instead of LevelDB. + * Note: you need to install MongoDB yourself. + */ + public boolean useMongoDB = false; + + public static class MongoDBCredentials { + /** + * Credentials for MongoDB database. + * Leave this as-is if you are using LevelDB or don't need + * special credentials. + */ + public String host = "localhost"; + public int port = 27017; + public boolean useSsl = true; + public String databaseName = "SimpleAuthPlayerData"; + public String password; + public String username; + } + } public static class LangConfig { public String enterPassword = "ยง6You need to enter your password!"; @@ -193,6 +214,7 @@ public class AuthConfig { public MainConfig main = new MainConfig(); public MainConfig.WorldSpawn worldSpawn = new MainConfig.WorldSpawn(); + public MainConfig.MongoDBCredentials mongoDBCredentials = new MainConfig.MongoDBCredentials(); public LangConfig lang = new LangConfig(); public ExperimentalConfig experimental = new ExperimentalConfig(); diff --git a/src/main/java/org/samo_lego/simpleauth/storage/SimpleAuthDatabase.java b/src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java similarity index 87% rename from src/main/java/org/samo_lego/simpleauth/storage/SimpleAuthDatabase.java rename to src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java index 020a406..3c5c1ca 100644 --- a/src/main/java/org/samo_lego/simpleauth/storage/SimpleAuthDatabase.java +++ b/src/main/java/org/samo_lego/simpleauth/storage/DBHelper.java @@ -2,18 +2,17 @@ package org.samo_lego.simpleauth.storage; import org.iq80.leveldb.DB; import org.iq80.leveldb.DBException; -import org.iq80.leveldb.Options; -import org.samo_lego.simpleauth.SimpleAuth; +import org.samo_lego.simpleauth.storage.database.LevelDB; +import org.samo_lego.simpleauth.storage.database.MongoDB; -import java.io.File; import java.io.IOException; import static org.iq80.leveldb.impl.Iq80DBFactory.bytes; -import static org.iq80.leveldb.impl.Iq80DBFactory.factory; import static org.samo_lego.simpleauth.utils.SimpleLogger.logError; import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo; +import static org.samo_lego.simpleauth.SimpleAuth.config; -public class SimpleAuthDatabase { +public class DBHelper { private DB levelDBStore; public DB getLevelDBStore() { @@ -24,13 +23,10 @@ public class SimpleAuthDatabase { * Connects to the DB. */ public void openConnection() { - try { - - Options options = new Options(); - levelDBStore = factory.open(new File(SimpleAuth.gameDirectory + "/mods/SimpleAuth/levelDBStore"), options); - } catch (Error | IOException e) { - logError(e.getMessage()); - } + if(config.main.useMongoDB) + MongoDB.initialize(); + else + LevelDB.initialize(); } /** diff --git a/src/main/java/org/samo_lego/simpleauth/storage/database/LevelDB.java b/src/main/java/org/samo_lego/simpleauth/storage/database/LevelDB.java new file mode 100644 index 0000000..2f2a4d6 --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/storage/database/LevelDB.java @@ -0,0 +1,136 @@ +package org.samo_lego.simpleauth.storage.database; + +import org.iq80.leveldb.DB; +import org.iq80.leveldb.DBException; +import org.iq80.leveldb.Options; +import org.samo_lego.simpleauth.SimpleAuth; + +import java.io.File; +import java.io.IOException; + +import static org.iq80.leveldb.impl.Iq80DBFactory.bytes; +import static org.iq80.leveldb.impl.Iq80DBFactory.factory; +import static org.samo_lego.simpleauth.utils.SimpleLogger.logError; +import static org.samo_lego.simpleauth.utils.SimpleLogger.logInfo; + +public class LevelDB { + private static DB levelDBStore; + + public static DB getLevelDBStore() { + return levelDBStore; + } + + /** + * Connects to the LevelDB. + */ + public static void initialize() { + Options options = new Options(); + try { + levelDBStore = factory.open(new File(SimpleAuth.gameDirectory + "/mods/SimpleAuth/levelDBStore"), options); + } catch (IOException e) { + logError(e.getMessage()); + } + } + + /** + * Closes database connection. + */ + public static void close() { + if (levelDBStore != null) { + try { + levelDBStore.close(); + logInfo("Database connection closed successfully."); + } catch (Error | IOException e) { + logError(e.getMessage()); + } + } + } + + /** + * Tells whether DB connection is closed. + * + * @return false if connection is open, otherwise false + */ + public static boolean isClosed() { + return levelDBStore == null; + } + + + /** + * Inserts the data for the player. + * + * @param uuid uuid of the player to insert data for + * @param data data to put inside database + * @return true if operation was successful, otherwise false + */ + public static boolean registerUser(String uuid, String data) { + try { + if(!isUserRegistered(uuid)) { + levelDBStore.put(bytes("UUID:" + uuid), bytes("data:" + data)); + return true; + } + return false; + } catch (Error e) { + logError("Register error: " + e.getMessage()); + return false; + } + } + + /** + * Checks if player is registered. + * + * @param uuid player's uuid + * @return true if registered, otherwise false + */ + public static boolean isUserRegistered(String uuid) { + try { + return levelDBStore.get(bytes("UUID:" + uuid)) != null; + } catch (DBException e) { + logError(e.getMessage()); + } + return false; + } + + /** + * Deletes data for the provided uuid. + * + * @param uuid uuid of player to delete data for + */ + public static void deleteUserData(String uuid) { + try { + levelDBStore.delete(bytes("UUID:" + uuid)); + } catch (Error e) { + logError(e.getMessage()); + } + } + + /** + * Updates player's data. + * + * @param uuid uuid of the player to update data for + * @param data data to put inside database + */ + public static void updateUserData(String uuid, String data) { + try { + levelDBStore.put(bytes("UUID:" + uuid), bytes("data:" + data)); + } catch (Error e) { + logError(e.getMessage()); + } + } + + /** + * Gets the hashed password from DB. + * + * @param uuid uuid of the player to get data for. + * @return data as string if player has it, otherwise empty string. + */ + public static String getData(String uuid){ + try { + if(isUserRegistered(uuid)) // Gets password from db and removes "data:" prefix from it + return new String(levelDBStore.get(bytes("UUID:" + uuid))).substring(5); + } catch (Error e) { + logError("Error getting data: " + e.getMessage()); + } + return ""; + } +} diff --git a/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java b/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java new file mode 100644 index 0000000..6ee9fcb --- /dev/null +++ b/src/main/java/org/samo_lego/simpleauth/storage/database/MongoDB.java @@ -0,0 +1,32 @@ +package org.samo_lego.simpleauth.storage.database; + + +import com.mongodb.client.MongoClient; +import com.mongodb.client.MongoClients; +import com.mongodb.client.MongoCollection; +import com.mongodb.client.MongoDatabase; +import org.bson.Document; + + +import static org.samo_lego.simpleauth.SimpleAuth.config; + +public class MongoDB { + private static MongoCollection collection; + + public static void initialize() { + /*mongoClient = MongoClients.create( + String.format( + "mongodb://%s:%s@%s:%s/?authSource=db1&ssl=%s", + config.mongoDBCredentials.username, + config.mongoDBCredentials.password, + config.mongoDBCredentials.host, + config.mongoDBCredentials.port, + config.mongoDBCredentials.useSsl + + ) + );*/ + MongoClient mongoClient = MongoClients.create(String.format("mongodb://%s:%s", config.mongoDBCredentials.host, config.mongoDBCredentials.port)); + MongoDatabase database = mongoClient.getDatabase(config.mongoDBCredentials.databaseName); + collection = database.getCollection("players"); + } +}