mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
Fixed MysqlHWIDHandler
This commit is contained in:
parent
4828e8e895
commit
8a426914f0
1 changed files with 154 additions and 80 deletions
|
@ -15,67 +15,135 @@
|
|||
|
||||
public class MysqlHWIDHandler extends HWIDHandler {
|
||||
private MySQLSourceConfig mySQLHolder;
|
||||
private String query;
|
||||
private String banMessage;
|
||||
private String isBannedName;
|
||||
private String loginName;
|
||||
private String hwidName;
|
||||
private String[] queryParams;
|
||||
private String queryUpd;
|
||||
private String[] queryParamsUpd;
|
||||
|
||||
private String tableUsers;
|
||||
private String tableHwids;
|
||||
|
||||
private String userFieldHwid;
|
||||
private String userFieldLogin;
|
||||
|
||||
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[] queryParamsBan;
|
||||
private String querySelect;
|
||||
private String[] queryParamsSelect;
|
||||
private String[] paramsBan;
|
||||
|
||||
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
|
||||
public void check0(HWID hwid, String username) throws HWIDException {
|
||||
try {
|
||||
Connection c = mySQLHolder.getConnection();
|
||||
if(hwid instanceof OshiHWID) {
|
||||
OshiHWID oshiHWID = (OshiHWID) hwid;
|
||||
try {
|
||||
Connection c = mySQLHolder.getConnection();
|
||||
|
||||
PreparedStatement s = c.prepareStatement(query);
|
||||
String[] replaceParams = {"hwid", String.valueOf(hwid.getSerializeString()), "login", username};
|
||||
for (int i = 0; i < queryParams.length; i++) {
|
||||
s.setString(i + 1, CommonHelper.replace(queryParams[i], replaceParams));
|
||||
}
|
||||
PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1",
|
||||
userFieldHwid, userFieldLogin, tableUsers, userFieldLogin));
|
||||
s.setString(1, username);
|
||||
|
||||
// Execute SQL query
|
||||
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||
try (ResultSet set = s.executeQuery()) {
|
||||
boolean isOne = false;
|
||||
boolean needWrite = true;
|
||||
while (set.next()) {
|
||||
isOne = true;
|
||||
boolean isBanned = set.getBoolean(isBannedName);
|
||||
if (isBanned) throw new HWIDException(banMessage);
|
||||
String login = set.getString(loginName);
|
||||
if (username.equals(login)) {
|
||||
needWrite = false;
|
||||
// Execute SQL query
|
||||
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||
try (ResultSet set = s.executeQuery()) {
|
||||
if(set.next()) {
|
||||
int hwid_id = set.getInt(userFieldHwid);
|
||||
if(hwid_id == 0) {
|
||||
onUpdateInfo(oshiHWID, username, c);
|
||||
} else {
|
||||
onCheckInfo(oshiHWID, username, c);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!isOne) {
|
||||
writeHWID(hwid, username, c);
|
||||
return;
|
||||
}
|
||||
if (needWrite) {
|
||||
writeHWID(hwid, username, c);
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 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));
|
||||
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);
|
||||
}
|
||||
a.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||
a.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -83,28 +151,29 @@ public void writeHWID(HWID hwid, String username, Connection c) {
|
|||
|
||||
public void setIsBanned(HWID hwid, boolean isBanned) {
|
||||
LogHelper.debug("%s Request HWID: %s", isBanned ? "Ban" : "UnBan", hwid.toString());
|
||||
Connection c = null;
|
||||
try {
|
||||
c = mySQLHolder.getConnection();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
try (PreparedStatement a = c.prepareStatement(queryBan)) {
|
||||
//IF
|
||||
String[] replaceParamsUpd = {"hwid", String.valueOf(hwid.getSerializeString()), "isBanned", isBanned ? "1" : "0"};
|
||||
for (int i = 0; i < queryParamsBan.length; i++) {
|
||||
a.setString(i + 1, CommonHelper.replace(queryParamsBan[i], replaceParamsUpd));
|
||||
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));
|
||||
}
|
||||
a.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||
a.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
a.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||
a.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void ban(List<HWID> list) {
|
||||
|
||||
for (HWID hwid : list) {
|
||||
setIsBanned(hwid, true);
|
||||
}
|
||||
|
@ -119,35 +188,40 @@ public void unban(List<HWID> list) {
|
|||
|
||||
@Override
|
||||
public List<HWID> getHwid(String username) {
|
||||
ArrayList<HWID> list = new ArrayList<>();
|
||||
try {
|
||||
LogHelper.debug("Try find HWID from username %s", username);
|
||||
Connection c = mySQLHolder.getConnection();
|
||||
PreparedStatement s = c.prepareStatement(querySelect);
|
||||
String[] replaceParams = {"login", username};
|
||||
for (int i = 0; i < queryParamsSelect.length; i++) {
|
||||
s.setString(i + 1, CommonHelper.replace(queryParamsSelect[i], replaceParams));
|
||||
}
|
||||
String hwid_str;
|
||||
PreparedStatement s = c.prepareStatement(String.format("SELECT %s, %s FROM `%s` WHERE `%s` = ? LIMIT 1", userFieldHwid, userFieldLogin, tableUsers, userFieldLogin));
|
||||
s.setString(1, username);
|
||||
|
||||
// Execute SQL query
|
||||
s.setQueryTimeout(MySQLSourceConfig.TIMEOUT);
|
||||
|
||||
try (ResultSet set = s.executeQuery()) {
|
||||
if (!set.next()) {
|
||||
if(set.next()) {
|
||||
int hwid_id = set.getInt(userFieldHwid);
|
||||
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);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LogHelper.error(new HWIDException("HWID not found"));
|
||||
return new ArrayList<>();
|
||||
}
|
||||
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 {
|
||||
list.add(hwid);
|
||||
LogHelper.debug("Username: %s HWID: %s", username, hwid.toString());
|
||||
}
|
||||
return list;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
return list;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
Loading…
Reference in a new issue