2015-10-19 17:43:46 +00:00
|
|
|
package me.totalfreedom.totalfreedommod.banning;
|
|
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
import com.google.common.collect.Maps;
|
|
|
|
import com.google.common.collect.Sets;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Set;
|
2016-03-01 16:47:01 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.FreedomService;
|
2015-10-19 17:43:46 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
2016-03-06 15:56:15 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
2015-11-15 23:32:04 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.player.PlayerData;
|
2016-03-06 15:56:15 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FLog;
|
2015-11-15 23:32:04 +00:00
|
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
2015-10-19 17:43:46 +00:00
|
|
|
import net.pravian.aero.config.YamlConfig;
|
|
|
|
import net.pravian.aero.util.Ips;
|
2015-11-15 23:32:04 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
|
|
|
import org.bukkit.event.EventPriority;
|
|
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
|
|
|
import org.bukkit.event.player.PlayerLoginEvent;
|
2015-10-19 17:43:46 +00:00
|
|
|
|
2016-03-01 16:47:01 +00:00
|
|
|
public class BanManager extends FreedomService
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2015-11-22 18:26:47 +00:00
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
private final Set<Ban> bans = Sets.newHashSet();
|
|
|
|
private final Map<String, Ban> ipBans = Maps.newHashMap();
|
|
|
|
private final Map<String, Ban> nameBans = Maps.newHashMap();
|
2015-10-19 17:43:46 +00:00
|
|
|
private final List<String> unbannableUsernames = Lists.newArrayList();
|
|
|
|
//
|
|
|
|
private final YamlConfig config;
|
|
|
|
|
|
|
|
public BanManager(TotalFreedomMod plugin)
|
|
|
|
{
|
|
|
|
super(plugin);
|
2016-05-12 19:40:39 +00:00
|
|
|
this.config = new YamlConfig(plugin, "bans.yml");
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onStart()
|
|
|
|
{
|
|
|
|
config.load();
|
|
|
|
|
|
|
|
bans.clear();
|
|
|
|
for (String id : config.getKeys(false))
|
|
|
|
{
|
|
|
|
if (!config.isConfigurationSection(id))
|
|
|
|
{
|
|
|
|
FLog.warning("Could not load username ban: " + id + ". Invalid format!");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
Ban ban = new Ban();
|
2015-10-19 17:43:46 +00:00
|
|
|
ban.loadFrom(config.getConfigurationSection(id));
|
|
|
|
|
|
|
|
if (!ban.isValid())
|
|
|
|
{
|
|
|
|
FLog.warning("Not adding username ban: " + id + ". Missing information.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
bans.add(ban);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove expired bans, repopulate ipBans and nameBans,
|
|
|
|
updateViews();
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
FLog.info("Loaded " + ipBans.size() + " IP bans and " + nameBans.size() + " username bans.");
|
2015-10-19 17:43:46 +00:00
|
|
|
|
|
|
|
// Load unbannable usernames
|
|
|
|
unbannableUsernames.clear();
|
2016-03-06 15:56:15 +00:00
|
|
|
unbannableUsernames.addAll((Collection<? extends String>) ConfigEntry.FAMOUS_PLAYERS.getList());
|
2015-10-19 17:43:46 +00:00
|
|
|
FLog.info("Loaded " + unbannableUsernames.size() + " unbannable usernames.");
|
|
|
|
}
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
@Override
|
|
|
|
protected void onStop()
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2016-03-06 15:56:15 +00:00
|
|
|
saveAll();
|
|
|
|
logger.info("Saved " + bans.size() + " player bans");
|
|
|
|
}
|
2015-10-19 17:43:46 +00:00
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
public Set<Ban> getAllBans()
|
|
|
|
{
|
|
|
|
return Collections.unmodifiableSet(bans);
|
|
|
|
}
|
2015-10-19 17:43:46 +00:00
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
public Collection<Ban> getIpBans()
|
|
|
|
{
|
|
|
|
return Collections.unmodifiableCollection(ipBans.values());
|
|
|
|
}
|
2015-10-19 17:43:46 +00:00
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
public Collection<Ban> getUsernameBans()
|
|
|
|
{
|
|
|
|
return Collections.unmodifiableCollection(nameBans.values());
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void saveAll()
|
|
|
|
{
|
|
|
|
// Remove expired
|
|
|
|
updateViews();
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
config.clear();
|
2015-11-15 23:32:04 +00:00
|
|
|
for (Ban ban : bans)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
ban.saveTo(config.createSection(String.valueOf(ban.hashCode())));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save config
|
|
|
|
config.save();
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public Ban getByIp(String ip)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2015-11-15 23:32:04 +00:00
|
|
|
final Ban directBan = ipBans.get(ip);
|
2015-10-19 17:43:46 +00:00
|
|
|
if (directBan != null && !directBan.isExpired())
|
|
|
|
{
|
|
|
|
return directBan;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Match fuzzy IP
|
2015-11-15 23:32:04 +00:00
|
|
|
for (Ban loopBan : ipBans.values())
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
if (loopBan.isExpired())
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (String loopIp : loopBan.getIps())
|
|
|
|
{
|
|
|
|
if (!loopIp.contains("*"))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ips.fuzzyIpMatch(ip, loopIp, 4))
|
|
|
|
{
|
|
|
|
return loopBan;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public Ban getByUsername(String username)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
username = username.toLowerCase();
|
2015-11-15 23:32:04 +00:00
|
|
|
final Ban directBan = nameBans.get(username);
|
2015-10-19 17:43:46 +00:00
|
|
|
|
|
|
|
if (directBan != null && !directBan.isExpired())
|
|
|
|
{
|
|
|
|
return directBan;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public Ban unbanIp(String ip)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2015-11-15 23:32:04 +00:00
|
|
|
final Ban ban = getByIp(ip);
|
2015-10-19 17:43:46 +00:00
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
if (ban != null)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2016-03-06 15:56:15 +00:00
|
|
|
bans.remove(ban);
|
|
|
|
saveAll();
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ban;
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public Ban unbanUsername(String username)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2015-11-15 23:32:04 +00:00
|
|
|
final Ban ban = getByUsername(username);
|
2015-10-19 17:43:46 +00:00
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
if (ban != null)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2016-03-06 15:56:15 +00:00
|
|
|
bans.remove(ban);
|
|
|
|
saveAll();
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ban;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isIpBanned(String ip)
|
|
|
|
{
|
|
|
|
return getByIp(ip) != null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isUsernameBanned(String username)
|
|
|
|
{
|
|
|
|
return getByUsername(username) != null;
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public boolean addBan(Ban ban)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
if (bans.add(ban))
|
|
|
|
{
|
2016-03-06 15:56:15 +00:00
|
|
|
saveAll();
|
2015-10-19 17:43:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-11-15 23:32:04 +00:00
|
|
|
public boolean removeBan(Ban ban)
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
|
|
|
if (bans.remove(ban))
|
|
|
|
{
|
2016-03-06 15:56:15 +00:00
|
|
|
saveAll();
|
2015-10-19 17:43:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
public int purge()
|
2015-10-19 17:43:46 +00:00
|
|
|
{
|
2016-03-06 15:56:15 +00:00
|
|
|
config.clear();
|
|
|
|
config.save();
|
2015-10-19 17:43:46 +00:00
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
int size = bans.size();
|
|
|
|
bans.clear();
|
|
|
|
updateViews();
|
|
|
|
|
|
|
|
return size;
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|
2015-11-15 23:32:04 +00:00
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.LOW)
|
|
|
|
public void onPlayerLogin(PlayerLoginEvent event)
|
|
|
|
{
|
|
|
|
final String username = event.getPlayer().getName();
|
|
|
|
final String ip = Ips.getIp(event);
|
|
|
|
|
|
|
|
// Regular ban
|
2016-03-06 15:56:15 +00:00
|
|
|
Ban ban = getByUsername(username);
|
2015-11-15 23:32:04 +00:00
|
|
|
if (ban == null)
|
|
|
|
{
|
2016-03-06 15:56:15 +00:00
|
|
|
ban = getByIp(ip);
|
2015-11-15 23:32:04 +00:00
|
|
|
}
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
if (ban != null && !ban.isExpired())
|
2015-11-15 23:32:04 +00:00
|
|
|
{
|
|
|
|
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, ban.bakeKickMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler(priority = EventPriority.LOW)
|
|
|
|
public void onPlayerJoin(PlayerJoinEvent event)
|
|
|
|
{
|
|
|
|
final Player player = event.getPlayer();
|
|
|
|
final PlayerData data = plugin.pl.getData(player);
|
|
|
|
|
|
|
|
if (!plugin.al.isAdmin(player))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unban admins
|
|
|
|
for (String storedIp : data.getIps())
|
|
|
|
{
|
|
|
|
unbanIp(storedIp);
|
|
|
|
unbanIp(FUtil.getFuzzyIp(storedIp));
|
|
|
|
}
|
|
|
|
|
|
|
|
unbanUsername(player.getName());
|
|
|
|
player.setOp(true);
|
|
|
|
}
|
|
|
|
|
2016-03-06 15:56:15 +00:00
|
|
|
private void updateViews()
|
|
|
|
{
|
|
|
|
// Remove expired bans
|
|
|
|
for (Iterator<Ban> it = bans.iterator(); it.hasNext();)
|
|
|
|
{
|
|
|
|
if (it.next().isExpired())
|
|
|
|
{
|
|
|
|
it.remove();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nameBans.clear();
|
|
|
|
ipBans.clear();
|
|
|
|
for (Ban ban : bans)
|
|
|
|
{
|
|
|
|
if (ban.hasUsername())
|
|
|
|
{
|
|
|
|
nameBans.put(ban.getUsername().toLowerCase(), ban);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ban.hasIps())
|
|
|
|
{
|
|
|
|
for (String ip : ban.getIps())
|
|
|
|
{
|
|
|
|
ipBans.put(ip, ban);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-19 17:43:46 +00:00
|
|
|
}
|