Unban macs & hwid
This commit is contained in:
@@ -734,45 +734,6 @@ public class Character extends AbstractCharacterObject {
|
||||
visibleMapObjects.add(mo);
|
||||
}
|
||||
|
||||
public static boolean ban(String id, String reason, boolean accountId) {
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
if (id.matches("/[0-9]{1,3}\\..*")) {
|
||||
try (PreparedStatement ps = con.prepareStatement("INSERT INTO ipbans VALUES (DEFAULT, ?)")) {
|
||||
ps.setString(1, id);
|
||||
ps.executeUpdate();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
final String query;
|
||||
if (accountId) {
|
||||
query = "SELECT id FROM accounts WHERE name = ?";
|
||||
} else {
|
||||
query = "SELECT accountid FROM characters WHERE name = ?";
|
||||
}
|
||||
|
||||
boolean ret = false;
|
||||
try (PreparedStatement ps = con.prepareStatement(query)) {
|
||||
ps.setString(1, id);
|
||||
|
||||
try (ResultSet rs = ps.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
try (PreparedStatement ps2 = con.prepareStatement("UPDATE accounts SET banned = 1, banreason = ? WHERE id = ?")) {
|
||||
ps2.setString(1, reason);
|
||||
ps2.setInt(2, rs.getInt(1));
|
||||
ps2.executeUpdate();
|
||||
}
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public int calculateMaxBaseDamage(int watk, WeaponType weapon) {
|
||||
int mainstat, secondarystat;
|
||||
if (getJob().isA(Job.THIEF) && weapon == WeaponType.DAGGER_OTHER) {
|
||||
|
||||
@@ -39,4 +39,15 @@ public class HwidBanRepository {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteHwidBan(String hwid) {
|
||||
String sql = """
|
||||
DELETE FROM hwid_ban
|
||||
WHERE hwid = :hwid""";
|
||||
try (Handle handle = connection.getHandle()) {
|
||||
handle.createUpdate(sql)
|
||||
.bind("hwid", hwid)
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,18 +28,28 @@ public class MacBanRepository {
|
||||
}
|
||||
}
|
||||
|
||||
public boolean saveMacBan(int accountId, String mac) {
|
||||
public void saveMacBan(String mac, int accountId) {
|
||||
String sql = """
|
||||
INSERT INTO mac_ban (account_id, mac)
|
||||
VALUES (:accountId, :mac)""";
|
||||
try (Handle handle = connection.getHandle()) {
|
||||
return handle.createUpdate(sql)
|
||||
handle.createUpdate(sql)
|
||||
.bind("accountId", accountId)
|
||||
.bind("mac", mac)
|
||||
.execute() > 0;
|
||||
.execute();
|
||||
} catch (Exception e) {
|
||||
log.error("Failed to save mac ban. The mac is already banned? accountId: {}, mac: {}", accountId, mac, e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public void deleteMacBan(String mac) {
|
||||
String sql = """
|
||||
DELETE FROM mac_ban
|
||||
WHERE mac = :mac""";
|
||||
try (Handle handle = connection.getHandle()) {
|
||||
handle.createUpdate(sql)
|
||||
.bind("mac", mac)
|
||||
.execute();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,9 +6,10 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import net.jcip.annotations.ThreadSafe;
|
||||
import net.server.coordinator.session.Hwid;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -18,7 +19,7 @@ import java.util.Set;
|
||||
@Slf4j
|
||||
public class HwidBanManager {
|
||||
private final HwidBanRepository hwidBanRepository;
|
||||
private final Set<Hwid> bannedHwids = new HashSet<>();
|
||||
private final Map<Hwid, Integer> bannedHwids = new HashMap<>();
|
||||
|
||||
public HwidBanManager(HwidBanRepository hwidBanRepository) {
|
||||
this.hwidBanRepository = hwidBanRepository;
|
||||
@@ -27,30 +28,35 @@ public class HwidBanManager {
|
||||
public synchronized void loadHwidBans() {
|
||||
List<HwidBan> hwidBans = hwidBanRepository.getAllHwidBans();
|
||||
log.debug("Loaded {} hwid bans", hwidBans.size());
|
||||
bannedHwids.addAll(hwidBans.stream()
|
||||
.map(HwidBanManager::createHwid)
|
||||
.filter(Objects::nonNull)
|
||||
.toList()
|
||||
);
|
||||
}
|
||||
|
||||
private static Hwid createHwid(HwidBan hwidBan) {
|
||||
try {
|
||||
return new Hwid(hwidBan.hwid());
|
||||
} catch (IllegalArgumentException e) {
|
||||
log.warn("Unable to create Hwid from: {} due to bad 'hwid' value in database", hwidBan);
|
||||
return null;
|
||||
}
|
||||
hwidBans.forEach(hwidBan -> bannedHwids.put(new Hwid(hwidBan.hwid()), hwidBan.accountId()));
|
||||
}
|
||||
|
||||
public synchronized boolean isBanned(Hwid hwid) {
|
||||
return bannedHwids.contains(hwid);
|
||||
return bannedHwids.containsKey(hwid);
|
||||
}
|
||||
|
||||
public synchronized void banHwid(Hwid hwid, int accountId) {
|
||||
if (hwid == null) {
|
||||
throw new IllegalArgumentException("hwid cannot be null");
|
||||
}
|
||||
|
||||
bannedHwids.put(hwid, accountId);
|
||||
hwidBanRepository.saveHwidBan(hwid.hwid(), accountId);
|
||||
}
|
||||
|
||||
public synchronized void unbanAccountHwids(int accountId) {
|
||||
Set<Hwid> hwidsToUnban = new HashSet<>();
|
||||
for (Map.Entry<Hwid, Integer> bannedHwid : bannedHwids.entrySet()) {
|
||||
if (bannedHwid.getValue() == accountId) {
|
||||
hwidsToUnban.add(bannedHwid.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
hwidsToUnban.forEach(this::unbanHwid);
|
||||
}
|
||||
|
||||
private void unbanHwid(Hwid hwid) {
|
||||
bannedHwids.remove(hwid);
|
||||
hwidBanRepository.deleteHwidBan(hwid.hwid());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,8 +5,10 @@ import database.ban.MacBanRepository;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.jcip.annotations.ThreadSafe;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
@@ -16,7 +18,7 @@ import java.util.Set;
|
||||
@Slf4j
|
||||
public class MacBanManager {
|
||||
private final MacBanRepository macBanRepository;
|
||||
private final Set<String> bannedMacs = new HashSet<>();
|
||||
private final Map<String, Integer> bannedMacs = new HashMap<>();
|
||||
|
||||
public MacBanManager(MacBanRepository macBanRepository) {
|
||||
this.macBanRepository = macBanRepository;
|
||||
@@ -25,11 +27,11 @@ public class MacBanManager {
|
||||
public synchronized void loadMacBans() {
|
||||
List<MacBan> macBans = macBanRepository.getAllMacBans();
|
||||
log.debug("Loaded {} mac bans", macBans.size());
|
||||
bannedMacs.addAll(macBans.stream().map(MacBan::mac).toList());
|
||||
macBans.forEach(macBan -> bannedMacs.put(macBan.mac(), macBan.accountId()));
|
||||
}
|
||||
|
||||
public synchronized boolean isBanned(String mac) {
|
||||
return bannedMacs.contains(mac);
|
||||
return bannedMacs.containsKey(mac);
|
||||
}
|
||||
|
||||
public synchronized void banMac(String mac, int accountId) {
|
||||
@@ -38,7 +40,23 @@ public class MacBanManager {
|
||||
}
|
||||
// TODO: validate mac format. Or create "Mac" model class.
|
||||
|
||||
bannedMacs.add(mac);
|
||||
macBanRepository.saveMacBan(accountId, mac);
|
||||
bannedMacs.put(mac, accountId);
|
||||
macBanRepository.saveMacBan(mac, accountId);
|
||||
}
|
||||
|
||||
public synchronized void unbanAccountMacs(int accountId) {
|
||||
Set<String> macsToUnban = new HashSet<>();
|
||||
for (Map.Entry<String, Integer> bannedMac : bannedMacs.entrySet()) {
|
||||
if (bannedMac.getValue() == accountId) {
|
||||
macsToUnban.add(bannedMac.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
macsToUnban.forEach(this::unbanMac);
|
||||
}
|
||||
|
||||
private void unbanMac(String ip) {
|
||||
bannedMacs.remove(ip);
|
||||
macBanRepository.deleteMacBan(ip);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -182,8 +182,8 @@ public class BanService {
|
||||
int accountId = foundAccount.get().id();
|
||||
accountService.unban(accountId);
|
||||
ipBanManager.unbanAccountIps(accountId);
|
||||
// macBanManager.unbanAccountMacs(accountId);
|
||||
// hwidBanManager.unbanAccountHwids(accountId);
|
||||
macBanManager.unbanAccountMacs(accountId);
|
||||
hwidBanManager.unbanAccountHwids(accountId);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +37,7 @@ class MacBanManagerTest extends DatabaseTest {
|
||||
macBanManager.loadMacBans();
|
||||
assertFalse(macBanManager.isBanned(mac));
|
||||
|
||||
macBanRepository.saveMacBan(AnyValues.integer(), mac);
|
||||
macBanRepository.saveMacBan(mac, AnyValues.integer());
|
||||
macBanManager.loadMacBans();
|
||||
|
||||
assertTrue(macBanManager.isBanned(mac));
|
||||
|
||||
Reference in New Issue
Block a user