|
| 1 | +From e20772d61f9d4cef17b87c58d47b3e3c63cf392e Mon Sep 17 00:00:00 2001 |
| 2 | +From: Petr Ilin <hevav@hevav.dev> |
| 3 | +Date: Wed, 28 Jul 2021 01:28:22 +0300 |
| 4 | +Subject: [PATCH] SQLite Support |
| 5 | + |
| 6 | +Signed-off-by: Petr Ilin <hevav@hevav.dev> |
| 7 | + |
| 8 | +diff --git a/proxy/build.gradle b/proxy/build.gradle |
| 9 | +index 3916c990..5fd92cc8 100644 |
| 10 | +--- a/proxy/build.gradle |
| 11 | ++++ b/proxy/build.gradle |
| 12 | +@@ -80,6 +80,7 @@ dependencies { |
| 13 | + implementation "org.mariadb.jdbc:mariadb-java-client:2.7.3" |
| 14 | + implementation 'at.favre.lib:bcrypt:0.9.0' |
| 15 | + implementation 'dev.samstevens.totp:totp:1.7.1' |
| 16 | ++ implementation 'org.xerial:sqlite-jdbc:3.36.0.1' |
| 17 | + |
| 18 | + // Note: we depend on the API twice, first the main sourceset, and then the annotation processor. |
| 19 | + implementation project(/*':velocity-api'*/':elytraproxy-api') |
| 20 | +diff --git a/proxy/src/main/java/net/elytrium/elytraproxy/ElytraProxy.java b/proxy/src/main/java/net/elytrium/elytraproxy/ElytraProxy.java |
| 21 | +index 249ecd0f..7a27baae 100644 |
| 22 | +--- a/proxy/src/main/java/net/elytrium/elytraproxy/ElytraProxy.java |
| 23 | ++++ b/proxy/src/main/java/net/elytrium/elytraproxy/ElytraProxy.java |
| 24 | +@@ -55,6 +55,7 @@ import net.elytrium.elytraproxy.commands.UnregisterCommand; |
| 25 | + import net.elytrium.elytraproxy.config.Settings; |
| 26 | + import net.elytrium.elytraproxy.database.Database; |
| 27 | + import net.elytrium.elytraproxy.database.MySqlDatabase; |
| 28 | ++import net.elytrium.elytraproxy.database.SqliteDatabase; |
| 29 | + import net.elytrium.elytraproxy.stats.Statistics; |
| 30 | + import net.elytrium.elytraproxy.virtual.protocol.VirtualProtocol; |
| 31 | + import net.elytrium.elytraproxy.virtual.server.VirtualServer; |
| 32 | +@@ -111,12 +112,25 @@ public class ElytraProxy { |
| 33 | + */ |
| 34 | + |
| 35 | + public void initDatabase(VelocityServer server) { |
| 36 | +- Settings.DATABASE config = Settings.IMP.DATABASE; |
| 37 | +- if (config.ENABLED) { |
| 38 | ++ if (checkDatabaseEnabled()) { |
| 39 | ++ Settings.DATABASE config = Settings.IMP.DATABASE; |
| 40 | ++ |
| 41 | + try { |
| 42 | +- database = new MySqlDatabase(config.HOSTNAME, config.DATABASE, config.USER, config.PASSWORD); |
| 43 | ++ switch (config.STORAGE_TYPE) { |
| 44 | ++ case "sqlite": |
| 45 | ++ Class.forName("org.sqlite.JDBC"); |
| 46 | ++ database = new SqliteDatabase(config.FILENAME); |
| 47 | ++ break; |
| 48 | ++ case "mysql": |
| 49 | ++ database = new MySqlDatabase(config.HOSTNAME, config.DATABASE, config.USER, config.PASSWORD); |
| 50 | ++ break; |
| 51 | ++ default: |
| 52 | ++ logger.error(Settings.IMP.MESSAGES.ELYTRAPROXY.DB_FAILURE); |
| 53 | ++ server.shutdown(); |
| 54 | ++ break; |
| 55 | ++ } |
| 56 | + database.makeTable(Settings.IMP.AUTH.TABLE, RegisteredPlayer.getDatabaseItem()); |
| 57 | +- } catch (SQLException e) { |
| 58 | ++ } catch (SQLException | ClassNotFoundException e) { |
| 59 | + logger.error(e); |
| 60 | + logger.error(Settings.IMP.MESSAGES.ELYTRAPROXY.DB_FAILURE); |
| 61 | + server.shutdown(); |
| 62 | +@@ -124,6 +138,10 @@ public class ElytraProxy { |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | ++ public boolean checkDatabaseEnabled() { |
| 67 | ++ return Settings.IMP.AUTH.ENABLE; |
| 68 | ++ } |
| 69 | ++ |
| 70 | + /** |
| 71 | + * Initializes ElytraProxy commands. |
| 72 | + * Инициализирует команды ElytraProxy. |
| 73 | +diff --git a/proxy/src/main/java/net/elytrium/elytraproxy/config/Settings.java b/proxy/src/main/java/net/elytrium/elytraproxy/config/Settings.java |
| 74 | +index 413322cd..379888e0 100644 |
| 75 | +--- a/proxy/src/main/java/net/elytrium/elytraproxy/config/Settings.java |
| 76 | ++++ b/proxy/src/main/java/net/elytrium/elytraproxy/config/Settings.java |
| 77 | +@@ -339,9 +339,13 @@ public class Settings extends Config { |
| 78 | + |
| 79 | + @Comment("Database settings") |
| 80 | + public static class DATABASE { |
| 81 | +- // @Comment("Тип базы данных, mysql или mysql))") |
| 82 | +- // public String STORAGE_TYPE = "mysql"; |
| 83 | +- public boolean ENABLED = true; |
| 84 | ++ @Comment("Database type: mysql, sqlite") |
| 85 | ++ public String STORAGE_TYPE = "sqlite"; |
| 86 | ++ |
| 87 | ++ @Comment("Settings for SQLite: ") |
| 88 | ++ public String FILENAME = "elytraproxy.db"; |
| 89 | ++ |
| 90 | ++ @Comment("Settings for MySQL: ") |
| 91 | + public String HOSTNAME = "127.0.0.1:3306"; |
| 92 | + public String USER = "user"; |
| 93 | + public String PASSWORD = "password"; |
| 94 | +diff --git a/proxy/src/main/java/net/elytrium/elytraproxy/database/MySqlDatabase.java b/proxy/src/main/java/net/elytrium/elytraproxy/database/MySqlDatabase.java |
| 95 | +index e8844c94..09903666 100644 |
| 96 | +--- a/proxy/src/main/java/net/elytrium/elytraproxy/database/MySqlDatabase.java |
| 97 | ++++ b/proxy/src/main/java/net/elytrium/elytraproxy/database/MySqlDatabase.java |
| 98 | +@@ -52,6 +52,10 @@ public class MySqlDatabase extends Database { |
| 99 | + this.connection = DriverManager.getConnection(url, user, password); |
| 100 | + } |
| 101 | + |
| 102 | ++ public MySqlDatabase(Connection connection) { |
| 103 | ++ this.connection = connection; |
| 104 | ++ } |
| 105 | ++ |
| 106 | + @Override |
| 107 | + @SuppressFBWarnings(value = "OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE", |
| 108 | + justification = "We don't need completion of query here") |
| 109 | +@@ -211,13 +215,13 @@ public class MySqlDatabase extends Database { |
| 110 | + return result; |
| 111 | + } |
| 112 | + |
| 113 | +- private String joinStream(Stream<Object> stream) { |
| 114 | ++ protected String joinStream(Stream<Object> stream) { |
| 115 | + return stream |
| 116 | + .map(Object::toString) |
| 117 | + .collect(Collectors.joining(", ")); |
| 118 | + } |
| 119 | + |
| 120 | +- private String keyFromStream(Stream<Object> stream) { |
| 121 | ++ protected String keyFromStream(Stream<Object> stream) { |
| 122 | + return joinStream(stream.map(e -> (e instanceof String) ? "\"" + e + "\"" : e)); |
| 123 | + } |
| 124 | + |
| 125 | +diff --git a/proxy/src/main/java/net/elytrium/elytraproxy/database/SqliteDatabase.java b/proxy/src/main/java/net/elytrium/elytraproxy/database/SqliteDatabase.java |
| 126 | +new file mode 100644 |
| 127 | +index 00000000..3862dc4b |
| 128 | +--- /dev/null |
| 129 | ++++ b/proxy/src/main/java/net/elytrium/elytraproxy/database/SqliteDatabase.java |
| 130 | +@@ -0,0 +1,46 @@ |
| 131 | ++/* |
| 132 | ++ * Copyright (C) 2021 Elytrium |
| 133 | ++ * |
| 134 | ++ * This program is free software: you can redistribute it and/or modify |
| 135 | ++ * it under the terms of the GNU Affero General Public License as published by |
| 136 | ++ * the Free Software Foundation, either version 3 of the License, or |
| 137 | ++ * (at your option) any later version. |
| 138 | ++ * |
| 139 | ++ * This program is distributed in the hope that it will be useful, |
| 140 | ++ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 141 | ++ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 142 | ++ * GNU Affero General Public License for more details. |
| 143 | ++ * |
| 144 | ++ * You should have received a copy of the GNU Affero General Public License |
| 145 | ++ * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 146 | ++ */ |
| 147 | ++ |
| 148 | ++package net.elytrium.elytraproxy.database; |
| 149 | ++ |
| 150 | ++import java.sql.DriverManager; |
| 151 | ++import java.sql.SQLException; |
| 152 | ++import java.util.Map; |
| 153 | ++ |
| 154 | ++public class SqliteDatabase extends MySqlDatabase { |
| 155 | ++ |
| 156 | ++ /** |
| 157 | ++ * Initializes and connects to SQLite Database. |
| 158 | ++ * |
| 159 | ++ * @param filename Database filename |
| 160 | ++ * @throws SQLException Throws if connection was unsuccessful |
| 161 | ++ */ |
| 162 | ++ public SqliteDatabase(String filename) throws SQLException { |
| 163 | ++ super(DriverManager.getConnection("jdbc:sqlite:" + filename)); |
| 164 | ++ } |
| 165 | ++ |
| 166 | ++ @Override |
| 167 | ++ public void insertMap(String table, Map<String, Object> toInsert, boolean update) { |
| 168 | ++ String keyString = joinStream(toInsert.keySet().stream().map(s -> s)); |
| 169 | ++ String valueString = keyFromStream(toInsert.values().stream()); |
| 170 | ++ |
| 171 | ++ String request = "INSERT " + (update ? "OR REPLACE" : "OR IGNORE") |
| 172 | ++ + " INTO " + table + " (" + keyString + ") VALUES(" + valueString + ")"; |
| 173 | ++ |
| 174 | ++ query(request); |
| 175 | ++ } |
| 176 | ++} |
| 177 | +-- |
| 178 | +2.32.0 |
| 179 | + |
0 commit comments