From 0268a48fe6e1fae982652000e29e04237884083e Mon Sep 17 00:00:00 2001 From: Gravit Date: Sat, 16 Mar 2019 00:59:17 +0700 Subject: [PATCH] =?UTF-8?q?[FIX]=20=D0=97=D0=B0=D0=BA=D1=80=D1=8B=D1=82?= =?UTF-8?q?=D0=B8=D0=B5=20=D1=81=D0=BE=D0=B5=D0=B4=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B9=20=D0=BF=D0=BE=D1=81=D0=BB=D0=B5=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=BF=D1=80=D0=BE=D1=81=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/handler/MySQLAuthHandler.java | 13 +++++---- .../auth/hwid/MysqlHWIDHandler.java | 29 +++++++++---------- .../auth/provider/MySQLAuthProvider.java | 21 +++++++++----- 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/MySQLAuthHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/MySQLAuthHandler.java index a98811e3..345bd489 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/MySQLAuthHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/handler/MySQLAuthHandler.java @@ -4,6 +4,7 @@ import ru.gravit.utils.helper.LogHelper; import java.io.IOException; +import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -64,8 +65,8 @@ protected Entry fetchEntry(UUID uuid) throws IOException { } private Entry query(String sql, String value) throws IOException { - try { - PreparedStatement s = mySQLHolder.getConnection().prepareStatement(sql); + try(Connection c = mySQLHolder.getConnection()) { + PreparedStatement s = c.prepareStatement(sql); s.setString(1, value); s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); try (ResultSet set = s.executeQuery()) { @@ -78,8 +79,8 @@ private Entry query(String sql, String value) throws IOException { @Override protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException { - try { - PreparedStatement s = mySQLHolder.getConnection().prepareStatement(updateAuthSQL); + try(Connection c = mySQLHolder.getConnection()) { + PreparedStatement s = c.prepareStatement(updateAuthSQL); s.setString(1, username); // Username case s.setString(2, accessToken); s.setString(3, uuid.toString()); @@ -92,8 +93,8 @@ protected boolean updateAuth(UUID uuid, String username, String accessToken) thr @Override protected boolean updateServerID(UUID uuid, String serverID) throws IOException { - try { - PreparedStatement s = mySQLHolder.getConnection().prepareStatement(updateServerIDSQL); + try(Connection c = mySQLHolder.getConnection()) { + PreparedStatement s = c.prepareStatement(updateServerIDSQL); s.setString(1, serverID); s.setString(2, uuid.toString()); s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MysqlHWIDHandler.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MysqlHWIDHandler.java index 8eb46a41..1201815b 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MysqlHWIDHandler.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/hwid/MysqlHWIDHandler.java @@ -63,8 +63,7 @@ public class MysqlHWIDHandler extends HWIDHandler { public void check0(HWID hwid, String username) throws HWIDException { if (hwid instanceof OshiHWID) { OshiHWID oshiHWID = (OshiHWID) hwid; - try { - Connection c = mySQLHolder.getConnection(); + try(Connection c = mySQLHolder.getConnection()) { PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1", userFieldHwid, userFieldLogin, tableUsers, userFieldLogin)); @@ -178,22 +177,21 @@ public void setIsBanned(HWID hwid, boolean isBanned) { LogHelper.debug("%s Request HWID: %s", isBanned ? "Ban" : "UnBan", hwid.toString()); if (hwid instanceof OshiHWID) { OshiHWID oshiHWID = (OshiHWID) hwid; - Connection c = null; - try { - c = mySQLHolder.getConnection(); - } catch (SQLException e) { - e.printStackTrace(); - } - try (PreparedStatement a = c.prepareStatement(queryBan)) { - String[] replaceParamsUpd = {"totalMemory", String.valueOf(oshiHWID.totalMemory), "serialNumber", oshiHWID.serialNumber, "HWDiskSerial", oshiHWID.HWDiskSerial, "processorID", oshiHWID.processorID, "isBanned", isBanned ? "1" : "0"}; - for (int i = 0; i < paramsBan.length; i++) { - a.setString(i + 1, CommonHelper.replace(paramsBan[i], replaceParamsUpd)); + try(Connection c = mySQLHolder.getConnection()) { + try (PreparedStatement a = c.prepareStatement(queryBan)) { + String[] replaceParamsUpd = {"totalMemory", String.valueOf(oshiHWID.totalMemory), "serialNumber", oshiHWID.serialNumber, "HWDiskSerial", oshiHWID.HWDiskSerial, "processorID", oshiHWID.processorID, "isBanned", isBanned ? "1" : "0"}; + for (int i = 0; i < paramsBan.length; i++) { + a.setString(i + 1, CommonHelper.replace(paramsBan[i], replaceParamsUpd)); + } + a.setQueryTimeout(MySQLSourceConfig.TIMEOUT); + a.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); } - a.setQueryTimeout(MySQLSourceConfig.TIMEOUT); - a.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } + } } @@ -214,9 +212,8 @@ public void unban(List list) { @Override public List getHwid(String username) { ArrayList list = new ArrayList<>(); - try { + try(Connection c = mySQLHolder.getConnection()) { LogHelper.debug("Try find HWID from username %s", username); - Connection c = mySQLHolder.getConnection(); PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1", userFieldHwid, userFieldLogin, tableUsers, userFieldLogin)); s.setString(1, username); diff --git a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MySQLAuthProvider.java b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MySQLAuthProvider.java index 3a3c5cfa..0f578583 100644 --- a/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MySQLAuthProvider.java +++ b/LaunchServer/src/main/java/ru/gravit/launchserver/auth/provider/MySQLAuthProvider.java @@ -8,6 +8,7 @@ import ru.gravit.utils.helper.LogHelper; import ru.gravit.utils.helper.SecurityHelper; +import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -28,16 +29,20 @@ public void init() { @Override public AuthProviderResult auth(String login, String password, String ip) throws SQLException, AuthException { - PreparedStatement s = mySQLHolder.getConnection().prepareStatement(query); - String[] replaceParams = {"login", login, "password", password, "ip", ip}; - for (int i = 0; i < queryParams.length; i++) - s.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams)); + try(Connection c = mySQLHolder.getConnection()) + { + PreparedStatement s = c.prepareStatement(query); + String[] replaceParams = {"login", login, "password", password, "ip", ip}; + for (int i = 0; i < queryParams.length; i++) + s.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams)); - // Execute SQL query - s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); - try (ResultSet set = s.executeQuery()) { - return set.next() ? new AuthProviderResult(set.getString(1), SecurityHelper.randomStringToken(), usePermission ? new ClientPermissions(set.getLong(2)) : LaunchServer.server.config.permissionsHandler.getPermissions(set.getString(1))) : authError(message); + // Execute SQL query + s.setQueryTimeout(MySQLSourceConfig.TIMEOUT); + try (ResultSet set = s.executeQuery()) { + return set.next() ? new AuthProviderResult(set.getString(1), SecurityHelper.randomStringToken(), usePermission ? new ClientPermissions(set.getLong(2)) : LaunchServer.server.config.permissionsHandler.getPermissions(set.getString(1))) : authError(message); + } } + } @Override