mirror of
https://github.com/GravitLauncher/Launcher
synced 2025-04-07 08:51:52 +03:00
Закрывайте коннекты, неизлечимые.
This commit is contained in:
parent
c658a85c35
commit
2bc3f43292
3 changed files with 44 additions and 19 deletions
|
@ -4,6 +4,7 @@
|
||||||
import ru.gravit.utils.helper.LogHelper;
|
import ru.gravit.utils.helper.LogHelper;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
@ -64,8 +65,9 @@ protected Entry fetchEntry(UUID uuid) throws IOException {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Entry query(String sql, String value) throws IOException {
|
private Entry query(String sql, String value) throws IOException {
|
||||||
|
Connection conn = mySQLHolder.getConnection();
|
||||||
try {
|
try {
|
||||||
PreparedStatement s = mySQLHolder.getConnection().prepareStatement(sql);
|
PreparedStatement s = conn.prepareStatement(sql);
|
||||||
s.setString(1, value);
|
s.setString(1, value);
|
||||||
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||||
try (ResultSet set = s.executeQuery()) {
|
try (ResultSet set = s.executeQuery()) {
|
||||||
|
@ -73,13 +75,17 @@ private Entry query(String sql, String value) throws IOException {
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new IOException(e);
|
throw new IOException(e);
|
||||||
|
} finally {
|
||||||
|
if (conn != null)
|
||||||
|
conn.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException {
|
protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException {
|
||||||
|
Connection conn = mySQLHolder.getConnection();
|
||||||
try {
|
try {
|
||||||
PreparedStatement s = mySQLHolder.getConnection().prepareStatement(updateAuthSQL);
|
PreparedStatement s = conn.prepareStatement(updateAuthSQL);
|
||||||
s.setString(1, username); // Username case
|
s.setString(1, username); // Username case
|
||||||
s.setString(2, accessToken);
|
s.setString(2, accessToken);
|
||||||
s.setString(3, uuid.toString());
|
s.setString(3, uuid.toString());
|
||||||
|
@ -87,19 +93,26 @@ protected boolean updateAuth(UUID uuid, String username, String accessToken) thr
|
||||||
return s.executeUpdate() > 0;
|
return s.executeUpdate() > 0;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new IOException(e);
|
throw new IOException(e);
|
||||||
|
} finally {
|
||||||
|
if (conn != null)
|
||||||
|
conn.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean updateServerID(UUID uuid, String serverID) throws IOException {
|
protected boolean updateServerID(UUID uuid, String serverID) throws IOException {
|
||||||
|
Connection conn = mySQLHolder.getConnection();
|
||||||
try {
|
try {
|
||||||
PreparedStatement s = mySQLHolder.getConnection().prepareStatement(updateServerIDSQL);
|
PreparedStatement s = conn.prepareStatement(updateServerIDSQL);
|
||||||
s.setString(1, serverID);
|
s.setString(1, serverID);
|
||||||
s.setString(2, uuid.toString());
|
s.setString(2, uuid.toString());
|
||||||
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||||
return s.executeUpdate() > 0;
|
return s.executeUpdate() > 0;
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
throw new IOException(e);
|
throw new IOException(e);
|
||||||
|
} finally {
|
||||||
|
if (conn != null)
|
||||||
|
conn.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,8 +63,8 @@ public class MysqlHWIDHandler extends HWIDHandler {
|
||||||
public void check0(HWID hwid, String username) throws HWIDException {
|
public void check0(HWID hwid, String username) throws HWIDException {
|
||||||
if (hwid instanceof OshiHWID) {
|
if (hwid instanceof OshiHWID) {
|
||||||
OshiHWID oshiHWID = (OshiHWID) hwid;
|
OshiHWID oshiHWID = (OshiHWID) hwid;
|
||||||
|
Connection c = mySQLHolder.getConnection();
|
||||||
try {
|
try {
|
||||||
Connection c = mySQLHolder.getConnection();
|
|
||||||
|
|
||||||
PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1",
|
PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1",
|
||||||
userFieldHwid, userFieldLogin, tableUsers, userFieldLogin));
|
userFieldHwid, userFieldLogin, tableUsers, userFieldLogin));
|
||||||
|
@ -84,6 +84,9 @@ public void check0(HWID hwid, String username) throws HWIDException {
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (c != null)
|
||||||
|
c.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -178,13 +181,9 @@ public void setIsBanned(HWID hwid, boolean isBanned) {
|
||||||
LogHelper.debug("%s Request HWID: %s", isBanned ? "Ban" : "UnBan", hwid.toString());
|
LogHelper.debug("%s Request HWID: %s", isBanned ? "Ban" : "UnBan", hwid.toString());
|
||||||
if (hwid instanceof OshiHWID) {
|
if (hwid instanceof OshiHWID) {
|
||||||
OshiHWID oshiHWID = (OshiHWID) hwid;
|
OshiHWID oshiHWID = (OshiHWID) hwid;
|
||||||
Connection c = null;
|
Connection c = mySQLHolder.getConnection();
|
||||||
try {
|
try {
|
||||||
c = mySQLHolder.getConnection();
|
PreparedStatement a = c.prepareStatement(queryBan)
|
||||||
} 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"};
|
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++) {
|
for (int i = 0; i < paramsBan.length; i++) {
|
||||||
a.setString(i + 1, CommonHelper.replace(paramsBan[i], replaceParamsUpd));
|
a.setString(i + 1, CommonHelper.replace(paramsBan[i], replaceParamsUpd));
|
||||||
|
@ -193,6 +192,9 @@ public void setIsBanned(HWID hwid, boolean isBanned) {
|
||||||
a.executeUpdate();
|
a.executeUpdate();
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (c != null)
|
||||||
|
c.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -214,9 +216,9 @@ public void unban(List<HWID> list) {
|
||||||
@Override
|
@Override
|
||||||
public List<HWID> getHwid(String username) {
|
public List<HWID> getHwid(String username) {
|
||||||
ArrayList<HWID> list = new ArrayList<>();
|
ArrayList<HWID> list = new ArrayList<>();
|
||||||
|
Connection c = mySQLHolder.getConnection();
|
||||||
try {
|
try {
|
||||||
LogHelper.debug("Try find HWID from username %s", username);
|
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));
|
PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1", userFieldHwid, userFieldLogin, tableUsers, userFieldLogin));
|
||||||
s.setString(1, username);
|
s.setString(1, username);
|
||||||
|
|
||||||
|
@ -245,6 +247,9 @@ public List<HWID> getHwid(String username) {
|
||||||
}
|
}
|
||||||
} catch (SQLException e) {
|
} catch (SQLException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
} finally {
|
||||||
|
if (c != null)
|
||||||
|
c.close();
|
||||||
}
|
}
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,6 +8,7 @@
|
||||||
import ru.gravit.utils.helper.LogHelper;
|
import ru.gravit.utils.helper.LogHelper;
|
||||||
import ru.gravit.utils.helper.SecurityHelper;
|
import ru.gravit.utils.helper.SecurityHelper;
|
||||||
|
|
||||||
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
@ -28,15 +29,21 @@ public void init() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public AuthProviderResult auth(String login, String password, String ip) throws SQLException, AuthException {
|
public AuthProviderResult auth(String login, String password, String ip) throws SQLException, AuthException {
|
||||||
PreparedStatement s = mySQLHolder.getConnection().prepareStatement(query);
|
Connection conn = mySQLHolder.getConnection();
|
||||||
String[] replaceParams = {"login", login, "password", password, "ip", ip};
|
try {
|
||||||
for (int i = 0; i < queryParams.length; i++)
|
PreparedStatement s = conn.prepareStatement(query);
|
||||||
s.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams));
|
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
|
// Execute SQL query
|
||||||
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||||
try (ResultSet set = s.executeQuery()) {
|
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);
|
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);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (conn != null)
|
||||||
|
conn.close();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue