Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ public class LoginSecurityConfig extends AbstractConfig {
@ConfigKey(path="password-max-length")
private int passwordMaxLength = 32;

/**
* accounts limit
*/
@ConfigHeader("limit of accounts that can be registered from an IP")
@ConfigKey(path="Accounts.LimitAccounts")
private int LimitAccounts = 2;

/**
* Join settings.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import javax.sql.DataSource;
import java.sql.*;
import java.time.Instant;
import java.util.ArrayList;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
Expand Down Expand Up @@ -241,4 +242,19 @@ private <T> void resolveError(Consumer<AsyncResult<T>> callback, Exception error
Bukkit.getScheduler().runTask(loginSecurity, () ->
callback.accept(new AsyncResult<T>(false, null, error)));
}

public ArrayList<PlayerProfile> SearchUsersByIP(String ip) throws SQLException {
ArrayList<PlayerProfile> r = new ArrayList<>();
try(Connection connection = dataSource.getConnection()) {
try(PreparedStatement statement = connection.prepareStatement("SELECT * FROM ls_players WHERE ip_address=?;")) {
statement.setString(1, ip);
try(ResultSet result = statement.executeQuery()) {
while(result.next()) {
r.add(parseResultSet(result));
}
}
}
}
return r;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageEvent;
import org.bukkit.event.entity.EntityPickupItemEvent;
import org.bukkit.event.entity.EntityTargetEvent;
import org.bukkit.event.entity.FoodLevelChangeEvent;
import org.bukkit.event.inventory.InventoryOpenEvent;
Expand Down Expand Up @@ -97,6 +98,16 @@ public void onPlayerPreLogin(AsyncPlayerPreLoginEvent event) {
}
}

@EventHandler(priority = EventPriority.MONITOR)
public void PlayerPickupItem(EntityPickupItemEvent event){
final Player player = (Player) event.getEntity();
if(isInvalidPlayer(player)) return;
final PlayerSession session = LoginSecurity.getSessionManager().getPlayerSession(player);
if(session.isAuthorized()) return;

event.setCancelled(true);
}

@EventHandler(priority = EventPriority.MONITOR)
public void onPlayerQuit(PlayerQuitEvent event) {
// Unload player
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.lenis0012.bukkit.loginsecurity.session.action;

import com.lenis0012.bukkit.loginsecurity.LoginSecurity;
import com.lenis0012.bukkit.loginsecurity.LoginSecurityConfig;
import com.lenis0012.bukkit.loginsecurity.hashing.Algorithm;
import com.lenis0012.bukkit.loginsecurity.session.*;
import com.lenis0012.bukkit.loginsecurity.session.exceptions.ProfileRefreshException;
import com.lenis0012.bukkit.loginsecurity.storage.PlayerProfile;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;

public class RegisterAction extends AuthAction {
Expand All @@ -32,7 +34,17 @@ public AuthMode run(PlayerSession session, ActionResponse response) {
final String hash = Algorithm.BCRYPT.hash(password);
profile.setPassword(hash);
profile.setHashingAlgorithm(Algorithm.BCRYPT.getId());
profile.setIpAddress(session.getPlayer().getAddress().getAddress().toString());
try {
LoginSecurityConfig config = LoginSecurity.getConfiguration();
ArrayList<PlayerProfile> ListByIp = plugin.datastore().getProfileRepository().SearchUsersByIP(profile.getIpAddress());
String ListUsersByIp = "";
for (PlayerProfile user : ListByIp) { ListUsersByIp += user.getLastName()+" ";}
if (ListByIp.size() >= config.getLimitAccounts()){
response.setSuccess(false);
response.setErrorMessage( "Accounts limit: "+config.getLimitAccounts()+" \n Registered accounts: "+ListByIp.size()+" \n You have reached the accounts limit, you can enter with: "+ListUsersByIp);
return null;
}
plugin.datastore().getProfileRepository().insertBlocking(profile);
} catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, "Failed to register user", e);
Expand Down