Added basic subnet bans

This commit is contained in:
Anuken 2020-01-09 12:03:45 -05:00
parent 2345b48e97
commit e06f514023
3 changed files with 54 additions and 2 deletions

View File

@ -76,7 +76,7 @@ public class NetServer implements ApplicationListener{
public NetServer(){
net.handleServer(Connect.class, (con, connect) -> {
if(admins.isIPBanned(connect.addressTCP)){
if(admins.isIPBanned(connect.addressTCP) || admins.isSubnetBanned(connect.addressTCP)){
con.kick(KickReason.banned);
}
});
@ -94,7 +94,7 @@ public class NetServer implements ApplicationListener{
String uuid = packet.uuid;
if(admins.isIPBanned(con.address)) return;
if(admins.isIPBanned(con.address) || admins.isSubnetBanned(con.address)) return;
if(con.hasBegunConnecting){
con.kick(KickReason.idInUse);

View File

@ -18,6 +18,7 @@ public class Administration{
private Array<String> whitelist = new Array<>();
private Array<ChatFilter> chatFilters = new Array<>();
private Array<ActionFilter> actionFilters = new Array<>();
private Array<String> subnetBans = new Array<>();
public Administration(){
load();
@ -55,6 +56,24 @@ public class Administration{
});
}
public Array<String> getSubnetBans(){
return subnetBans;
}
public void removeSubnetBan(String ip){
subnetBans.remove(ip);
save();
}
public void addSubnetBan(String ip){
subnetBans.add(ip);
save();
}
public boolean isSubnetBanned(String ip){
return subnetBans.contains(ip::startsWith);
}
/** Adds a chat filter. This will transform the chat messages of every player.
* This functionality can be used to implement things like swear filters and special commands.
* Note that commands (starting with /) are not filtered.*/
@ -354,6 +373,7 @@ public class Administration{
Core.settings.putObject("player-info", playerInfo);
Core.settings.putObject("banned-ips", bannedIPs);
Core.settings.putObject("whitelisted", whitelist);
Core.settings.putObject("subnet-bans", subnetBans);
Core.settings.save();
}
@ -362,6 +382,7 @@ public class Administration{
playerInfo = Core.settings.getObject("player-info", ObjectMap.class, ObjectMap::new);
bannedIPs = Core.settings.getObject("banned-ips", Array.class, Array::new);
whitelist = Core.settings.getObject("whitelisted", Array.class, Array::new);
subnetBans = Core.settings.getObject("subnet-bans", Array.class, Array::new);
}
/** Server configuration definition. Each config value can be a string, boolean or number. */

View File

@ -491,6 +491,37 @@ public class ServerControl implements ApplicationListener{
}
});
handler.register("subnet-ban", "[add/remove] [address]", "Ban a subnet. This simply rejects all connections with IPs starting with some string.", arg -> {
if(arg.length == 0){
Log.info("Subnets banned: &lc{0}", netServer.admins.getSubnetBans().isEmpty() ? "<none>" : "");
for(String subnet : netServer.admins.getSubnetBans()){
Log.info("&ly " + subnet + "");
}
}else if(arg.length == 1){
err("You must provide a subnet to add or remove.");
}else{
if(arg[0].equals("add")){
if(netServer.admins.getSubnetBans().contains(arg[1])){
err("That subnet is already banned.");
return;
}
netServer.admins.addSubnetBan(arg[1]);
Log.info("Banned &ly{0}&lc**", arg[1]);
}else if(arg[0].equals("remove")){
if(!netServer.admins.getSubnetBans().contains(arg[1])){
err("That subnet isn't banned.");
return;
}
netServer.admins.removeSubnetBan(arg[1]);
Log.info("Unbanned &ly{0}&lc**", arg[1]);
}else{
err("Incorrect usage. You must provide add/remove as the second argument.");
}
}
});
handler.register("whitelisted", "List the entire whitelist.", arg -> {
if(netServer.admins.getWhitelisted().isEmpty()){
info("&lyNo whitelisted players found.");