Unban account & ip
This commit is contained in:
@@ -27,10 +27,6 @@ import client.Character;
|
|||||||
import client.Client;
|
import client.Client;
|
||||||
import client.command.Command;
|
import client.command.Command;
|
||||||
import client.command.CommandContext;
|
import client.command.CommandContext;
|
||||||
import tools.DatabaseConnection;
|
|
||||||
|
|
||||||
import java.sql.Connection;
|
|
||||||
import java.sql.PreparedStatement;
|
|
||||||
|
|
||||||
public class UnBanCommand extends Command {
|
public class UnBanCommand extends Command {
|
||||||
{
|
{
|
||||||
@@ -45,25 +41,12 @@ public class UnBanCommand extends Command {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
try (Connection con = DatabaseConnection.getConnection()) {
|
String chrName = params[0];
|
||||||
int aid = Character.getAccountIdByName(params[0]);
|
if (!ctx.banService().unban(chrName)) {
|
||||||
|
player.message("Failed to unban " + chrName);
|
||||||
try (PreparedStatement p = con.prepareStatement("UPDATE accounts SET banned = -1 WHERE id = " + aid)) {
|
|
||||||
p.executeUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
try (PreparedStatement p = con.prepareStatement("DELETE FROM ipbans WHERE aid = " + aid)) {
|
|
||||||
p.executeUpdate();
|
|
||||||
}
|
|
||||||
|
|
||||||
try (PreparedStatement p = con.prepareStatement("DELETE FROM macbans WHERE aid = " + aid)) {
|
|
||||||
p.executeUpdate();
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
player.message("Failed to unban " + params[0]);
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
player.message("Unbanned " + params[0]);
|
|
||||||
|
player.message("Unbanned " + chrName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -157,14 +157,15 @@ public class AccountRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean setBanned(int accountId, Instant bannedUntil, byte banReason, String description) {
|
public boolean setBanned(int accountId, boolean banned, Instant bannedUntil, Byte banReason, String description) {
|
||||||
String sql = """
|
String sql = """
|
||||||
UPDATE account
|
UPDATE account
|
||||||
SET banned = true, banned_until = :bannedUntil, ban_reason = :banReason, ban_description = :banDescription
|
SET banned = :banned, banned_until = :bannedUntil, ban_reason = :banReason, ban_description = :banDescription
|
||||||
WHERE id = :id""";
|
WHERE id = :id""";
|
||||||
try (Handle handle = connection.getHandle()) {
|
try (Handle handle = connection.getHandle()) {
|
||||||
return handle.createUpdate(sql)
|
return handle.createUpdate(sql)
|
||||||
.bind("id", accountId)
|
.bind("id", accountId)
|
||||||
|
.bind("banned", banned)
|
||||||
.bind("bannedUntil", bannedUntil)
|
.bind("bannedUntil", bannedUntil)
|
||||||
.bind("banReason", banReason)
|
.bind("banReason", banReason)
|
||||||
.bind("banDescription", description)
|
.bind("banDescription", description)
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ public class IpBanRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean saveIpBan(int accountId, String ip) {
|
public boolean saveIpBan(String ip, int accountId) {
|
||||||
String sql = """
|
String sql = """
|
||||||
INSERT INTO ip_ban (account_id, ip)
|
INSERT INTO ip_ban (account_id, ip)
|
||||||
VALUES (:accountId, :ip)""";
|
VALUES (:accountId, :ip)""";
|
||||||
@@ -42,4 +42,15 @@ public class IpBanRepository {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteIpBan(String ip) {
|
||||||
|
String sql = """
|
||||||
|
DELETE FROM ip_ban
|
||||||
|
WHERE ip = :ip""";
|
||||||
|
try (Handle handle = connection.getHandle()) {
|
||||||
|
handle.createUpdate(sql)
|
||||||
|
.bind("ip", ip)
|
||||||
|
.execute();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,10 @@ import database.ban.IpBanRepository;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.jcip.annotations.ThreadSafe;
|
import net.jcip.annotations.ThreadSafe;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -16,7 +18,7 @@ import java.util.Set;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class IpBanManager {
|
public class IpBanManager {
|
||||||
private final IpBanRepository ipBanRepository;
|
private final IpBanRepository ipBanRepository;
|
||||||
private final Set<String> bannedIps = new HashSet<>();
|
private final Map<String, Integer> bannedIps = new HashMap<>();
|
||||||
|
|
||||||
public IpBanManager(IpBanRepository ipBanRepository) {
|
public IpBanManager(IpBanRepository ipBanRepository) {
|
||||||
this.ipBanRepository = ipBanRepository;
|
this.ipBanRepository = ipBanRepository;
|
||||||
@@ -25,11 +27,11 @@ public class IpBanManager {
|
|||||||
public synchronized void loadIpBans() {
|
public synchronized void loadIpBans() {
|
||||||
List<IpBan> ipBans = ipBanRepository.getAllIpBans();
|
List<IpBan> ipBans = ipBanRepository.getAllIpBans();
|
||||||
log.debug("Loaded {} ip bans", ipBans.size());
|
log.debug("Loaded {} ip bans", ipBans.size());
|
||||||
bannedIps.addAll(ipBans.stream().map(IpBan::ip).toList());
|
ipBans.forEach(ipBan -> bannedIps.put(ipBan.ip(), ipBan.accountId()));
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized boolean isBanned(String ip) {
|
public synchronized boolean isBanned(String ip) {
|
||||||
return bannedIps.contains(ip);
|
return bannedIps.containsKey(ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void banIp(String ip, int accountId) {
|
public synchronized void banIp(String ip, int accountId) {
|
||||||
@@ -38,8 +40,24 @@ public class IpBanManager {
|
|||||||
}
|
}
|
||||||
// TODO: validate ip format. Or create "Ip" model class.
|
// TODO: validate ip format. Or create "Ip" model class.
|
||||||
|
|
||||||
bannedIps.add(ip);
|
bannedIps.put(ip, accountId);
|
||||||
ipBanRepository.saveIpBan(accountId, ip);
|
ipBanRepository.saveIpBan(ip, accountId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public synchronized void unbanAccountIps(int accountId) {
|
||||||
|
Set<String> ipsToUnban = new HashSet<>();
|
||||||
|
for (Map.Entry<String, Integer> bannedIp : bannedIps.entrySet()) {
|
||||||
|
if (bannedIp.getValue() == accountId) {
|
||||||
|
ipsToUnban.add(bannedIp.getKey());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ipsToUnban.forEach(this::unbanIp);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void unbanIp(String ip) {
|
||||||
|
bannedIps.remove(ip);
|
||||||
|
ipBanRepository.deleteIpBan(ip);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -317,7 +317,11 @@ public class AccountService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean ban(int accountId, Instant bannedUntil, byte banReason, String description) {
|
public boolean ban(int accountId, Instant bannedUntil, byte banReason, String description) {
|
||||||
return accountRepository.setBanned(accountId, bannedUntil, banReason, description);
|
return accountRepository.setBanned(accountId, true, bannedUntil, banReason, description);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean unban(int accountId) {
|
||||||
|
return accountRepository.setBanned(accountId, false, null, null, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -173,6 +173,20 @@ public class BanService {
|
|||||||
hwidBanManager.banHwid(hwid, accountId);
|
hwidBanManager.banHwid(hwid, accountId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean unban(String chrName) {
|
||||||
|
Optional<Account> foundAccount = accountService.getAccountIdByChrName(chrName);
|
||||||
|
if (foundAccount.isEmpty()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
int accountId = foundAccount.get().id();
|
||||||
|
accountService.unban(accountId);
|
||||||
|
ipBanManager.unbanAccountIps(accountId);
|
||||||
|
// macBanManager.unbanAccountMacs(accountId);
|
||||||
|
// hwidBanManager.unbanAccountHwids(accountId);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean isBanned(Client c) {
|
public boolean isBanned(Client c) {
|
||||||
return isIpBanned(c) || isHwidBanned(c) || isMacBanned(c);
|
return isIpBanned(c) || isHwidBanned(c) || isMacBanned(c);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ CREATE TABLE ip_ban
|
|||||||
created_at timestamp DEFAULT now() NOT NULL,
|
created_at timestamp DEFAULT now() NOT NULL,
|
||||||
PRIMARY KEY (ip)
|
PRIMARY KEY (ip)
|
||||||
);
|
);
|
||||||
GRANT SELECT, INSERT ON TABLE ip_ban TO ${server-username};
|
GRANT SELECT, INSERT, DELETE ON TABLE ip_ban TO ${server-username};
|
||||||
|
|
||||||
CREATE TABLE hwid_ban
|
CREATE TABLE hwid_ban
|
||||||
(
|
(
|
||||||
@@ -14,7 +14,7 @@ CREATE TABLE hwid_ban
|
|||||||
created_at timestamp DEFAULT now() NOT NULL,
|
created_at timestamp DEFAULT now() NOT NULL,
|
||||||
PRIMARY KEY (hwid)
|
PRIMARY KEY (hwid)
|
||||||
);
|
);
|
||||||
GRANT SELECT, INSERT ON TABLE hwid_ban TO ${server-username};
|
GRANT SELECT, INSERT, DELETE ON TABLE hwid_ban TO ${server-username};
|
||||||
|
|
||||||
CREATE TABLE mac_ban
|
CREATE TABLE mac_ban
|
||||||
(
|
(
|
||||||
@@ -23,4 +23,4 @@ account_id integer,
|
|||||||
created_at timestamp DEFAULT now() NOT NULL,
|
created_at timestamp DEFAULT now() NOT NULL,
|
||||||
PRIMARY KEY (mac)
|
PRIMARY KEY (mac)
|
||||||
);
|
);
|
||||||
GRANT SELECT, INSERT ON TABLE mac_ban TO ${server-username};
|
GRANT SELECT, INSERT, DELETE ON TABLE mac_ban TO ${server-username};
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ class IpBanManagerTest extends DatabaseTest {
|
|||||||
ipBanManager.loadIpBans();
|
ipBanManager.loadIpBans();
|
||||||
assertFalse(ipBanManager.isBanned(ip));
|
assertFalse(ipBanManager.isBanned(ip));
|
||||||
|
|
||||||
ipBanRepository.saveIpBan(AnyValues.integer(), ip);
|
ipBanRepository.saveIpBan(ip, AnyValues.integer());
|
||||||
ipBanManager.loadIpBans();
|
ipBanManager.loadIpBans();
|
||||||
|
|
||||||
assertTrue(ipBanManager.isBanned(ip));
|
assertTrue(ipBanManager.isBanned(ip));
|
||||||
|
|||||||
Reference in New Issue
Block a user