Merge pull request #113 from sanik/master

Fixed MysqlHWIDHandler
This commit is contained in:
Gravit 2018-12-27 12:50:13 +07:00 committed by GitHub
commit 19fa135a61
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -15,67 +15,135 @@
public class MysqlHWIDHandler extends HWIDHandler { public class MysqlHWIDHandler extends HWIDHandler {
private MySQLSourceConfig mySQLHolder; private MySQLSourceConfig mySQLHolder;
private String query;
private String banMessage; private String tableUsers;
private String isBannedName; private String tableHwids;
private String loginName;
private String hwidName; private String userFieldHwid;
private String[] queryParams; private String userFieldLogin;
private String queryUpd;
private String[] queryParamsUpd; private String hwidFieldTotalMemory;
private String hwidFieldSerialNumber;
private String hwidFieldHWDiskSerial;
private String hwidFieldProcessorID;
private String hwidFieldBanned;
private String queryHwids;
private String[] paramsHwids;
private String queryBan; private String queryBan;
private String[] queryParamsBan; private String[] paramsBan;
private String querySelect;
private String[] queryParamsSelect; private String banMessage;
/*
//Добавить поля hwid в базу с пользователями
//Создание таблицы для хранения HWID
CREATE TABLE `fc_user_hwids` (
`id` int(16) NOT NULL,
`totalMemory` varchar(32) NOT NULL,
`serialNumber` varchar(64) NOT NULL,
`HWDiskSerial` varchar(64) NOT NULL,
`processorID` varchar(64) NOT NULL,
`isBanned` tinyint(1) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `fc_user_hwids` ADD UNIQUE KEY `id` (`id`);
ALTER TABLE `fc_user_hwids` MODIFY `id` int(16) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=2;
*/
@Override @Override
public void check0(HWID hwid, String username) throws HWIDException { public void check0(HWID hwid, String username) throws HWIDException {
if(hwid instanceof OshiHWID) {
OshiHWID oshiHWID = (OshiHWID) hwid;
try { try {
Connection c = mySQLHolder.getConnection(); Connection c = mySQLHolder.getConnection();
PreparedStatement s = c.prepareStatement(query); PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1",
String[] replaceParams = {"hwid", String.valueOf(hwid.getSerializeString()), "login", username}; userFieldHwid, userFieldLogin, tableUsers, userFieldLogin));
for (int i = 0; i < queryParams.length; i++) { s.setString(1, username);
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()) {
boolean isOne = false; if(set.next()) {
boolean needWrite = true; int hwid_id = set.getInt(userFieldHwid);
while (set.next()) { if(hwid_id == 0) {
isOne = true; onUpdateInfo(oshiHWID, username, c);
boolean isBanned = set.getBoolean(isBannedName); } else {
if (isBanned) throw new HWIDException(banMessage); onCheckInfo(oshiHWID, username, c);
String login = set.getString(loginName);
if (username.equals(login)) {
needWrite = false;
} }
} }
if (!isOne) {
writeHWID(hwid, username, c);
return;
}
if (needWrite) {
writeHWID(hwid, username, c);
}
} }
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
public void writeHWID(HWID hwid, String username, Connection c) {
LogHelper.debug("Write HWID %s from username %s", hwid.toString(), username);
try (PreparedStatement a = c.prepareStatement(queryUpd)) {
//IF
String[] replaceParamsUpd = {"hwid", String.valueOf(hwid.getSerializeString()), "login", username};
for (int i = 0; i < queryParamsUpd.length; i++) {
a.setString(i + 1, CommonHelper.replace(queryParamsUpd[i], replaceParamsUpd));
} }
a.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
a.executeUpdate(); public void onUpdateInfo(OshiHWID hwid, String username, Connection c) throws HWIDException {
try (PreparedStatement a = c.prepareStatement(queryHwids)) {
String[] replaceParams = {"totalMemory", String.valueOf(hwid.totalMemory), "serialNumber", hwid.serialNumber, "HWDiskSerial", hwid.HWDiskSerial, "processorID", hwid.processorID};
for (int i = 0; i < paramsHwids.length; i++) {
a.setString(i + 1, CommonHelper.replace(paramsHwids[i], replaceParams));
}
ResultSet set = a.executeQuery();
PreparedStatement ps;
if(set.next()) {
int id = set.getInt("id");
boolean isBanned = set.getBoolean(hwidFieldBanned);
ps = c.prepareStatement(String.format("UPDATE `%s` SET `%s` = ? WHERE `%s` = ?",
tableUsers, userFieldHwid, userFieldLogin));
ps.setInt(1, id);
ps.setString(2, username);
ps.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
ps.executeUpdate();
if(isBanned) {
throw new HWIDException(banMessage);
}
} else {
ps = c.prepareStatement(String.format("INSERT INTO `%s` (`%s`, `%s`, `%s`, `%s`) VALUES (?, ?, ?, ?);",
tableHwids, hwidFieldTotalMemory, hwidFieldSerialNumber, hwidFieldHWDiskSerial, hwidFieldProcessorID));
ps.setString(1, String.valueOf(hwid.totalMemory));
ps.setString(2, hwid.serialNumber);
ps.setString(3, hwid.HWDiskSerial);
ps.setString(4, hwid.processorID);
ps.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
ps.executeUpdate();
ps = c.prepareStatement(String.format("UPDATE `%s` SET `%s` = LAST_INSERT_ID() WHERE `%s` = ?;",
tableUsers, userFieldHwid, userFieldLogin));
ps.setString(1, username);
ps.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
ps.executeUpdate();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
public void onCheckInfo(OshiHWID hwid, String username, Connection c) throws HWIDException {
try (PreparedStatement a = c.prepareStatement(queryHwids)) {
String[] replaceParams = {"totalMemory", String.valueOf(hwid.totalMemory), "serialNumber", hwid.serialNumber, "HWDiskSerial", hwid.HWDiskSerial, "processorID", hwid.processorID};
for (int i = 0; i < paramsHwids.length; i++) {
a.setString(i + 1, CommonHelper.replace(paramsHwids[i], replaceParams));
}
ResultSet set = a.executeQuery();
if(set.next()) {
boolean isBanned = set.getBoolean(hwidFieldBanned);
if(isBanned) {
throw new HWIDException(banMessage);
}
} else {
onUpdateInfo(hwid, username, c);
}
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -83,6 +151,8 @@ public void writeHWID(HWID hwid, String username, Connection c) {
public void setIsBanned(HWID hwid, boolean isBanned) { 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) {
OshiHWID oshiHWID = (OshiHWID) hwid;
Connection c = null; Connection c = null;
try { try {
c = mySQLHolder.getConnection(); c = mySQLHolder.getConnection();
@ -90,10 +160,9 @@ public void setIsBanned(HWID hwid, boolean isBanned) {
e.printStackTrace(); e.printStackTrace();
} }
try (PreparedStatement a = c.prepareStatement(queryBan)) { try (PreparedStatement a = c.prepareStatement(queryBan)) {
//IF String[] replaceParamsUpd = {"totalMemory", String.valueOf(oshiHWID.totalMemory), "serialNumber", oshiHWID.serialNumber, "HWDiskSerial", oshiHWID.HWDiskSerial, "processorID", oshiHWID.processorID, "isBanned", isBanned ? "1" : "0"};
String[] replaceParamsUpd = {"hwid", String.valueOf(hwid.getSerializeString()), "isBanned", isBanned ? "1" : "0"}; for (int i = 0; i < paramsBan.length; i++) {
for (int i = 0; i < queryParamsBan.length; i++) { a.setString(i + 1, CommonHelper.replace(paramsBan[i], replaceParamsUpd));
a.setString(i + 1, CommonHelper.replace(queryParamsBan[i], replaceParamsUpd));
} }
a.setQueryTimeout(MySQLSourceConfig.TIMEOUT); a.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
a.executeUpdate(); a.executeUpdate();
@ -101,10 +170,10 @@ public void setIsBanned(HWID hwid, boolean isBanned) {
e.printStackTrace(); e.printStackTrace();
} }
} }
}
@Override @Override
public void ban(List<HWID> list) { public void ban(List<HWID> list) {
for (HWID hwid : list) { for (HWID hwid : list) {
setIsBanned(hwid, true); setIsBanned(hwid, true);
} }
@ -119,35 +188,40 @@ 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<>();
try { try {
LogHelper.debug("Try find HWID from username %s", username); LogHelper.debug("Try find HWID from username %s", username);
Connection c = mySQLHolder.getConnection(); Connection c = mySQLHolder.getConnection();
PreparedStatement s = c.prepareStatement(querySelect); PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1", userFieldHwid, userFieldLogin, tableUsers, userFieldLogin));
String[] replaceParams = {"login", username}; s.setString(1, username);
for (int i = 0; i < queryParamsSelect.length; i++) {
s.setString(i + 1, CommonHelper.replace(queryParamsSelect[i], replaceParams)); // Execute SQL query
} s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
String hwid_str;
try (ResultSet set = s.executeQuery()) { try (ResultSet set = s.executeQuery()) {
if (!set.next()) { if(set.next()) {
LogHelper.error(new HWIDException("HWID not found")); int hwid_id = set.getInt(userFieldHwid);
return new ArrayList<>(); if(hwid_id != 0) {
s = c.prepareStatement(String.format("SELECT * FROM `%s` WHERE `id` = ? LIMIT 1", tableHwids));
s.setInt(1, hwid_id);
ResultSet rs = s.executeQuery();
if (rs.next()) {
OshiHWID oshiHWID = new OshiHWID();
oshiHWID.totalMemory = Long.valueOf(rs.getString(hwidFieldTotalMemory));
oshiHWID.serialNumber = rs.getString(hwidFieldSerialNumber);
oshiHWID.HWDiskSerial = rs.getString(hwidFieldHWDiskSerial);
oshiHWID.processorID = rs.getString(hwidFieldProcessorID);
list.add(oshiHWID);
} }
hwid_str = set.getString(hwidName);
} }
ArrayList<HWID> list = new ArrayList<>();
HWID hwid = OshiHWID.gson.fromJson(hwid_str, OshiHWID.class);
if (hwid.isNull()) {
LogHelper.warning("Null HWID");
} else { } else {
list.add(hwid); LogHelper.error(new HWIDException("HWID not found"));
LogHelper.debug("Username: %s HWID: %s", username, hwid.toString()); }
} }
return list;
} catch (SQLException e) { } catch (SQLException e) {
e.printStackTrace(); e.printStackTrace();
} }
return null; return list;
} }
@Override @Override