forked from ronancpl/HeavenMS
-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Redo ip bans - reduce amount of db queries on login
Works by loading all ip bans on startup and querying the collection in memory rather than making calls on every login.
- Loading branch information
Showing
15 changed files
with
228 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package database.ban; | ||
|
||
import lombok.Builder; | ||
|
||
import java.util.Objects; | ||
|
||
@Builder | ||
public record IpBan(String ip, Integer accountId) { | ||
public IpBan { | ||
Objects.requireNonNull(ip); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package database.ban; | ||
|
||
import database.PgDatabaseConnection; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jdbi.v3.core.Handle; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* @author Ponk | ||
*/ | ||
@Slf4j | ||
public class IpBanRepository { | ||
private final PgDatabaseConnection connection; | ||
|
||
public IpBanRepository(PgDatabaseConnection connection) { | ||
this.connection = connection; | ||
} | ||
|
||
public List<IpBan> getAllIpBans() { | ||
String sql = """ | ||
SELECT ip, account_id | ||
FROM ip_ban"""; | ||
try (Handle handle = connection.getHandle()) { | ||
return handle.createQuery(sql) | ||
.mapTo(IpBan.class) | ||
.list(); | ||
} | ||
} | ||
|
||
public boolean saveIpBan(int accountId, String ip) { | ||
String sql = """ | ||
INSERT INTO ip_ban (account_id, ip) | ||
VALUES (:accountId, :ip)"""; | ||
try (Handle handle = connection.getHandle()) { | ||
return handle.createUpdate(sql) | ||
.bind("accountId", accountId) | ||
.bind("ip", ip) | ||
.execute() > 0; | ||
} catch (Exception e) { | ||
log.error("Failed to save ip ban. The ip is already banned? accountId: {}, ip: {}", accountId, ip, e); | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package database.ban; | ||
|
||
import org.jdbi.v3.core.mapper.RowMapper; | ||
import org.jdbi.v3.core.statement.StatementContext; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
public class IpBanRowMapper implements RowMapper<IpBan> { | ||
|
||
@Override | ||
public IpBan map(ResultSet rs, StatementContext ctx) throws SQLException { | ||
return IpBan.builder() | ||
.ip(rs.getString("ip")) | ||
.accountId(rs.getObject("account_id", Integer.class)) | ||
.build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package server.ban; | ||
|
||
import database.ban.IpBan; | ||
import database.ban.IpBanRepository; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.jcip.annotations.ThreadSafe; | ||
|
||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* @author Ponk | ||
*/ | ||
@ThreadSafe | ||
@Slf4j | ||
public class IpBanManager { | ||
private final IpBanRepository ipBanRepository; | ||
private final Set<String> bannedIps = new HashSet<>(); | ||
|
||
public IpBanManager(IpBanRepository ipBanRepository) { | ||
this.ipBanRepository = ipBanRepository; | ||
} | ||
|
||
public synchronized void loadIpBans() { | ||
List<IpBan> ipBans = ipBanRepository.getAllIpBans(); | ||
log.debug("Loaded {} ip bans", ipBans.size()); | ||
bannedIps.addAll(ipBans.stream().map(IpBan::ip).toList()); | ||
} | ||
|
||
public synchronized boolean isBanned(String ip) { | ||
return bannedIps.contains(ip); | ||
} | ||
|
||
public synchronized void banIp(String ip, int accountId) { | ||
if (ip == null) { | ||
throw new IllegalArgumentException("ip cannot be null"); | ||
} | ||
// TODO: validate ip format. Or create "Ip" model class. | ||
|
||
bannedIps.add(ip); | ||
ipBanRepository.saveIpBan(accountId, ip); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CREATE TABLE ip_ban | ||
( | ||
ip varchar(15) NOT NULL, | ||
account_id integer, | ||
created_at timestamp DEFAULT now() NOT NULL, | ||
PRIMARY KEY (ip) | ||
); | ||
GRANT SELECT, INSERT ON TABLE ip_ban TO ${server-username}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.