Unify ban handling on login
This commit is contained in:
@@ -61,7 +61,6 @@ 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;
|
||||||
import java.sql.Timestamp;
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Calendar;
|
import java.util.Calendar;
|
||||||
@@ -504,37 +503,6 @@ public class Client extends ChannelInboundHandlerAdapter {
|
|||||||
return ++failedLoginAttempts < MAX_FAILED_LOGIN_ATTEMPTS;
|
return ++failedLoginAttempts < MAX_FAILED_LOGIN_ATTEMPTS;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check tempban directly on loaded account
|
|
||||||
@Deprecated
|
|
||||||
public Calendar getTempBanCalendarFromDB() {
|
|
||||||
final Calendar lTempban = Calendar.getInstance();
|
|
||||||
|
|
||||||
try (Connection con = DatabaseConnection.getConnection();
|
|
||||||
PreparedStatement ps = con.prepareStatement("SELECT `tempban` FROM accounts WHERE id = ?")) {
|
|
||||||
ps.setInt(1, getAccID());
|
|
||||||
|
|
||||||
final Timestamp tempban;
|
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
|
||||||
if (!rs.next()) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
tempban = rs.getTimestamp("tempban");
|
|
||||||
if (tempban.toLocalDateTime().equals(DefaultDates.getTempban())) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
lTempban.setTimeInMillis(tempban.getTime());
|
|
||||||
tempBanCalendar = lTempban;
|
|
||||||
return lTempban;
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
return null;//why oh why!?!
|
|
||||||
}
|
|
||||||
|
|
||||||
public Calendar getTempBanCalendar() {
|
public Calendar getTempBanCalendar() {
|
||||||
return tempBanCalendar;
|
return tempBanCalendar;
|
||||||
}
|
}
|
||||||
@@ -800,22 +768,6 @@ public class Client extends ChannelInboundHandlerAdapter {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final byte getGReason() {
|
|
||||||
try (Connection con = DatabaseConnection.getConnection();
|
|
||||||
PreparedStatement ps = con.prepareStatement("SELECT `greason` FROM `accounts` WHERE id = ?")) {
|
|
||||||
ps.setInt(1, accId);
|
|
||||||
|
|
||||||
try (ResultSet rs = ps.executeQuery()) {
|
|
||||||
if (rs.next()) {
|
|
||||||
return rs.getByte("greason");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (SQLException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
public byte getGender() {
|
public byte getGender() {
|
||||||
return gender;
|
return gender;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package database.account;
|
|||||||
import client.LoginState;
|
import client.LoginState;
|
||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
|
|
||||||
|
import java.time.Instant;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
@@ -13,7 +14,7 @@ import java.util.Objects;
|
|||||||
@Builder
|
@Builder
|
||||||
public record Account(int id, String name, String password, boolean acceptedTos, Byte gender, LocalDate birthdate,
|
public record Account(int id, String name, String password, boolean acceptedTos, Byte gender, LocalDate birthdate,
|
||||||
String pin, String pic, byte chrSlots, LoginState loginState, LocalDateTime lastLogin,
|
String pin, String pic, byte chrSlots, LoginState loginState, LocalDateTime lastLogin,
|
||||||
boolean banned, Byte banReason, String banDescription, LocalDateTime tempBannedUntil) {
|
boolean banned, Instant bannedUntil, Byte banReason, String banDescription) {
|
||||||
public Account {
|
public Account {
|
||||||
Objects.requireNonNull(name);
|
Objects.requireNonNull(name);
|
||||||
Objects.requireNonNull(password);
|
Objects.requireNonNull(password);
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ public class AccountRepository {
|
|||||||
public Optional<Account> findByNameIgnoreCase(String name) {
|
public Optional<Account> findByNameIgnoreCase(String name) {
|
||||||
String sql = """
|
String sql = """
|
||||||
SELECT id, name, password, pin, pic, birthdate, gender, tos_accepted, chr_slots, login_state,
|
SELECT id, name, password, pin, pic, birthdate, gender, tos_accepted, chr_slots, login_state,
|
||||||
last_login, banned, ban_reason, ban_description, temp_banned_until
|
last_login, banned, banned_until, ban_reason, ban_description
|
||||||
FROM account
|
FROM account
|
||||||
WHERE lower(name) = lower(:name)""";
|
WHERE lower(name) = lower(:name)""";
|
||||||
try (Handle handle = connection.getHandle()) {
|
try (Handle handle = connection.getHandle()) {
|
||||||
@@ -34,7 +34,7 @@ public class AccountRepository {
|
|||||||
public Optional<Account> findById(int accountId) {
|
public Optional<Account> findById(int accountId) {
|
||||||
String sql = """
|
String sql = """
|
||||||
SELECT id, name, password, pin, pic, birthdate, gender, tos_accepted, chr_slots, login_state,
|
SELECT id, name, password, pin, pic, birthdate, gender, tos_accepted, chr_slots, login_state,
|
||||||
last_login, banned, ban_reason, ban_description, temp_banned_until
|
last_login, banned, banned_until, ban_reason, ban_description
|
||||||
FROM account
|
FROM account
|
||||||
WHERE id = :id""";
|
WHERE id = :id""";
|
||||||
try (Handle handle = connection.getHandle()) {
|
try (Handle handle = connection.getHandle()) {
|
||||||
|
|||||||
@@ -35,11 +35,11 @@ public class AccountRowMapper implements RowMapper<Account> {
|
|||||||
.map(Timestamp::toLocalDateTime)
|
.map(Timestamp::toLocalDateTime)
|
||||||
.orElse(null))
|
.orElse(null))
|
||||||
.banned(rs.getBoolean("banned"))
|
.banned(rs.getBoolean("banned"))
|
||||||
|
.bannedUntil(Optional.ofNullable(rs.getTimestamp("banned_until"))
|
||||||
|
.map(Timestamp::toInstant)
|
||||||
|
.orElse(null))
|
||||||
.banReason(rs.getByte("ban_reason"))
|
.banReason(rs.getByte("ban_reason"))
|
||||||
.banDescription(rs.getString("ban_description"))
|
.banDescription(rs.getString("ban_description"))
|
||||||
.tempBannedUntil(Optional.ofNullable(rs.getTimestamp("temp_banned_until"))
|
|
||||||
.map(Timestamp::toLocalDateTime)
|
|
||||||
.orElse(null))
|
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ import java.sql.Date;
|
|||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.sql.Timestamp;
|
import java.sql.Timestamp;
|
||||||
import java.util.Calendar;
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
@@ -109,18 +108,13 @@ public final class LoginPasswordHandler implements PacketHandler {
|
|||||||
|
|
||||||
if (account.banned()) {
|
if (account.banned()) {
|
||||||
byte banReason = Objects.requireNonNullElse(account.banReason(), (byte) 0);
|
byte banReason = Objects.requireNonNullElse(account.banReason(), (byte) 0);
|
||||||
|
if (account.bannedUntil() != null) {
|
||||||
|
c.sendPacket(PacketCreator.getTempBan(banReason, account.bannedUntil().toEpochMilli()));
|
||||||
|
} else {
|
||||||
c.sendPacket(PacketCreator.getPermBan(banReason));
|
c.sendPacket(PacketCreator.getPermBan(banReason));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
boolean tempBanDisabled = false;
|
|
||||||
Calendar tempban = null;
|
|
||||||
if (!tempBanDisabled && (tempban = c.getTempBanCalendarFromDB()) != null) {
|
|
||||||
if (tempban.getTimeInMillis() > Calendar.getInstance().getTimeInMillis()) {
|
|
||||||
c.sendPacket(PacketCreator.getTempBan(tempban.getTimeInMillis(), c.getGReason()));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
boolean banCheckDisabled = false;
|
boolean banCheckDisabled = false;
|
||||||
if (!banCheckDisabled && (c.hasBannedIP() || c.hasBannedMac())) {
|
if (!banCheckDisabled && (c.hasBannedIP() || c.hasBannedMac())) {
|
||||||
@@ -128,14 +122,6 @@ public final class LoginPasswordHandler implements PacketHandler {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* TODO: check temp ban from account, something like this:
|
|
||||||
LocalDateTime tempBan = account.tempBanTimestamp();
|
|
||||||
if (tempBan != null && tempBan.isAfter(LocalDateTime.now())) {
|
|
||||||
Duration remainingTempBan = Duration.between(LocalDateTime.now(), tempBan);
|
|
||||||
c.sendPacket(PacketCreator.getTempBan());
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (account.loginState() != LoginState.LOGGED_OUT) {
|
if (account.loginState() != LoginState.LOGGED_OUT) {
|
||||||
c.sendPacket(PacketCreator.getLoginFailed(7));
|
c.sendPacket(PacketCreator.getLoginFailed(7));
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -711,7 +711,7 @@ public class PacketCreator {
|
|||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Packet getTempBan(long timestampTill, byte reason) {
|
public static Packet getTempBan(byte reason, long timestampTill) {
|
||||||
OutPacket p = OutPacket.create(SendOpcode.LOGIN_STATUS);
|
OutPacket p = OutPacket.create(SendOpcode.LOGIN_STATUS);
|
||||||
p.writeByte(2);
|
p.writeByte(2);
|
||||||
p.writeByte(0);
|
p.writeByte(0);
|
||||||
|
|||||||
@@ -16,10 +16,9 @@ CREATE TABLE account
|
|||||||
login_state smallint NOT NULL,
|
login_state smallint NOT NULL,
|
||||||
last_login timestamp,
|
last_login timestamp,
|
||||||
banned boolean DEFAULT false NOT NULL,
|
banned boolean DEFAULT false NOT NULL,
|
||||||
|
banned_until timestamp,
|
||||||
ban_reason smallint,
|
ban_reason smallint,
|
||||||
ban_description text,
|
ban_description text,
|
||||||
temp_banned_until timestamp,
|
|
||||||
greason smallint,
|
|
||||||
ip text,
|
ip text,
|
||||||
hwid text,
|
hwid text,
|
||||||
macs text,
|
macs text,
|
||||||
|
|||||||
Reference in New Issue
Block a user