mirror of
https://github.com/AtlasMediaGroup/TotalFreedomMod.git
synced 2025-06-29 11:46:40 +00:00
SQL DB is a WIP
This commit is contained in:
@ -4,6 +4,7 @@ import com.google.common.collect.Lists;
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
@ -14,16 +15,13 @@ import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
||||
import me.totalfreedom.totalfreedommod.util.FUtil;
|
||||
import net.pravian.aero.base.ConfigLoadable;
|
||||
import net.pravian.aero.base.ConfigSavable;
|
||||
import net.pravian.aero.base.Validatable;
|
||||
import net.pravian.aero.util.Ips;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public class Ban implements ConfigLoadable, ConfigSavable, Validatable
|
||||
public class Ban implements Validatable
|
||||
{
|
||||
|
||||
public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd \'at\' HH:mm:ss z");
|
||||
@ -53,22 +51,19 @@ public class Ban implements ConfigLoadable, ConfigSavable, Validatable
|
||||
public Ban(String username, String ip, String by, Date at, Date expire, String reason)
|
||||
{
|
||||
this(username,
|
||||
new String[]
|
||||
{
|
||||
ip
|
||||
},
|
||||
Arrays.asList(ip),
|
||||
by,
|
||||
at,
|
||||
expire,
|
||||
reason);
|
||||
}
|
||||
|
||||
public Ban(String username, String[] ips, String by, Date at, Date expire, String reason)
|
||||
public Ban(String username, List<String> ips, String by, Date at, Date expire, String reason)
|
||||
{
|
||||
this.username = username;
|
||||
if (ips != null)
|
||||
{
|
||||
this.ips.addAll(Arrays.asList(ips));
|
||||
this.ips.addAll(ips);
|
||||
}
|
||||
dedupeIps();
|
||||
this.by = by;
|
||||
@ -86,10 +81,7 @@ public class Ban implements ConfigLoadable, ConfigSavable, Validatable
|
||||
|
||||
public static Ban forPlayerIp(Player player, CommandSender by, Date expiry, String reason)
|
||||
{
|
||||
return new Ban(null, new String[]
|
||||
{
|
||||
Ips.getIp(player)
|
||||
}, by.getName(), Date.from(Instant.now()), expiry, reason);
|
||||
return new Ban(null, Arrays.asList(Ips.getIp(player)), by.getName(), Date.from(Instant.now()), expiry, reason);
|
||||
}
|
||||
|
||||
public static Ban forPlayerIp(String ip, CommandSender by, Date expiry, String reason)
|
||||
@ -107,7 +99,7 @@ public class Ban implements ConfigLoadable, ConfigSavable, Validatable
|
||||
public static Ban forPlayerName(String player, CommandSender by, Date expiry, String reason)
|
||||
{
|
||||
return new Ban(player,
|
||||
(String[])null,
|
||||
new ArrayList<String>(),
|
||||
by.getName(),
|
||||
Date.from(Instant.now()),
|
||||
expiry,
|
||||
@ -171,7 +163,7 @@ public class Ban implements ConfigLoadable, ConfigSavable, Validatable
|
||||
return hasExpiry() && expiryUnix < FUtil.getUnixTime();
|
||||
}
|
||||
|
||||
public String bakeKickMessage(String ip)
|
||||
public String bakeKickMessage()
|
||||
{
|
||||
final StringBuilder message = new StringBuilder(ChatColor.GOLD + "You");
|
||||
|
||||
@ -203,9 +195,6 @@ public class Ban implements ConfigLoadable, ConfigSavable, Validatable
|
||||
.append(DATE_FORMAT.format(FUtil.getUnixDate(expiryUnix)));
|
||||
}
|
||||
|
||||
message.append("\n").append(ChatColor.RED).append("IP Address: ").append(ChatColor.GOLD)
|
||||
.append(ip);
|
||||
|
||||
return message.toString();
|
||||
}
|
||||
|
||||
@ -246,31 +235,6 @@ public class Ban implements ConfigLoadable, ConfigSavable, Validatable
|
||||
return hash;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void loadFrom(ConfigurationSection cs)
|
||||
{
|
||||
this.username = cs.getString("username", null);
|
||||
this.ips.clear();
|
||||
this.ips.addAll(cs.getStringList("ips"));
|
||||
this.by = cs.getString("by", null);
|
||||
this.at = FUtil.stringToDate(cs.getString("at", null));
|
||||
this.reason = cs.getString("reason", null);
|
||||
this.expiryUnix = cs.getLong("expiry_unix", 0);
|
||||
dedupeIps();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void saveTo(ConfigurationSection cs)
|
||||
{
|
||||
dedupeIps();
|
||||
cs.set("username", username);
|
||||
cs.set("ips", ips.isEmpty() ? null : ips);
|
||||
cs.set("by", by);
|
||||
cs.set("at", FUtil.dateToString(at));
|
||||
cs.set("reason", reason);
|
||||
cs.set("expiry_unix", expiryUnix > 0 ? expiryUnix : null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid()
|
||||
{
|
||||
|
@ -3,8 +3,12 @@ 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.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@ -15,7 +19,6 @@ import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
||||
import me.totalfreedom.totalfreedommod.player.PlayerData;
|
||||
import me.totalfreedom.totalfreedommod.util.FLog;
|
||||
import me.totalfreedom.totalfreedommod.util.FUtil;
|
||||
import net.pravian.aero.config.YamlConfig;
|
||||
import net.pravian.aero.util.Ips;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.EventHandler;
|
||||
@ -30,41 +33,38 @@ public class BanManager extends FreedomService
|
||||
private final Map<String, Ban> ipBans = Maps.newHashMap();
|
||||
private final Map<String, Ban> nameBans = Maps.newHashMap();
|
||||
private final List<String> unbannableUsernames = Lists.newArrayList();
|
||||
public static final String CONFIG_FILENAME = "bans.yml";
|
||||
|
||||
//
|
||||
private final YamlConfig config;
|
||||
|
||||
public BanManager(TotalFreedomMod plugin)
|
||||
{
|
||||
super(plugin);
|
||||
this.config = new YamlConfig(plugin, "bans.yml");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart()
|
||||
{
|
||||
config.load();
|
||||
|
||||
bans.clear();
|
||||
for (String id : config.getKeys(false))
|
||||
try
|
||||
{
|
||||
if (!config.isConfigurationSection(id))
|
||||
ResultSet banSet = plugin.sql.getBanList();
|
||||
{
|
||||
FLog.warning("Could not load username ban: " + id + ". Invalid format!");
|
||||
continue;
|
||||
while (banSet.next())
|
||||
{
|
||||
String name = banSet.getString("name");
|
||||
List<String> ips = FUtil.stringToList(banSet.getString("ips"));
|
||||
String by = banSet.getString("by");
|
||||
Date at = new Date(banSet.getLong("at"));
|
||||
Date expires = new Date(banSet.getLong("expires"));
|
||||
String reason = banSet.getString("reason");
|
||||
Ban ban = new Ban(name, ips, by, at, expires, reason);
|
||||
bans.add(ban);
|
||||
}
|
||||
}
|
||||
|
||||
Ban ban = new Ban();
|
||||
ban.loadFrom(config.getConfigurationSection(id));
|
||||
|
||||
if (!ban.isValid())
|
||||
{
|
||||
FLog.warning("Not adding username ban: " + id + ". Missing information.");
|
||||
continue;
|
||||
}
|
||||
|
||||
bans.add(ban);
|
||||
}
|
||||
catch (SQLException e)
|
||||
{
|
||||
FLog.severe("Failed to load ban list: " + e.getMessage());
|
||||
}
|
||||
|
||||
// Remove expired bans, repopulate ipBans and nameBans,
|
||||
@ -81,8 +81,6 @@ public class BanManager extends FreedomService
|
||||
@Override
|
||||
protected void onStop()
|
||||
{
|
||||
saveAll();
|
||||
logger.info("Saved " + bans.size() + " player bans");
|
||||
}
|
||||
|
||||
public Set<Ban> getAllBans()
|
||||
@ -100,21 +98,6 @@ public class BanManager extends FreedomService
|
||||
return Collections.unmodifiableCollection(nameBans.values());
|
||||
}
|
||||
|
||||
public void saveAll()
|
||||
{
|
||||
// Remove expired
|
||||
updateViews();
|
||||
|
||||
config.clear();
|
||||
for (Ban ban : bans)
|
||||
{
|
||||
ban.saveTo(config.createSection(String.valueOf(ban.hashCode())));
|
||||
}
|
||||
|
||||
// Save config
|
||||
config.save();
|
||||
}
|
||||
|
||||
public Ban getByIp(String ip)
|
||||
{
|
||||
final Ban directBan = ipBans.get(ip);
|
||||
@ -168,7 +151,6 @@ public class BanManager extends FreedomService
|
||||
if (ban != null)
|
||||
{
|
||||
bans.remove(ban);
|
||||
saveAll();
|
||||
}
|
||||
|
||||
return ban;
|
||||
@ -181,7 +163,6 @@ public class BanManager extends FreedomService
|
||||
if (ban != null)
|
||||
{
|
||||
bans.remove(ban);
|
||||
saveAll();
|
||||
}
|
||||
|
||||
return ban;
|
||||
@ -199,12 +180,16 @@ public class BanManager extends FreedomService
|
||||
|
||||
public boolean addBan(Ban ban)
|
||||
{
|
||||
if (getByUsername(ban.getUsername()) != null)
|
||||
{
|
||||
removeBan(ban);
|
||||
}
|
||||
if (bans.add(ban))
|
||||
{
|
||||
saveAll();
|
||||
plugin.sql.addBan(ban);
|
||||
updateViews();
|
||||
return true;
|
||||
}
|
||||
updateViews();
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -213,23 +198,20 @@ public class BanManager extends FreedomService
|
||||
{
|
||||
if (bans.remove(ban))
|
||||
{
|
||||
saveAll();
|
||||
plugin.sql.removeBan(ban);
|
||||
updateViews();
|
||||
return true;
|
||||
}
|
||||
updateViews();
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public int purge()
|
||||
{
|
||||
config.clear();
|
||||
config.save();
|
||||
|
||||
int size = bans.size();
|
||||
bans.clear();
|
||||
updateViews();
|
||||
|
||||
plugin.sql.truncate("bans");
|
||||
return size;
|
||||
}
|
||||
|
||||
@ -248,7 +230,7 @@ public class BanManager extends FreedomService
|
||||
|
||||
if (ban != null && !ban.isExpired())
|
||||
{
|
||||
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, ban.bakeKickMessage(ip));
|
||||
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, ban.bakeKickMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@ -277,11 +259,12 @@ public class BanManager extends FreedomService
|
||||
private void updateViews()
|
||||
{
|
||||
// Remove expired bans
|
||||
for (Iterator<Ban> it = bans.iterator(); it.hasNext(); )
|
||||
for (Ban ban : new ArrayList<>(bans))
|
||||
{
|
||||
if (it.next().isExpired())
|
||||
if (ban.isExpired())
|
||||
{
|
||||
it.remove();
|
||||
bans.remove(ban);
|
||||
plugin.sql.removeBan(ban);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -1,76 +0,0 @@
|
||||
package me.totalfreedom.totalfreedommod.banning;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import java.util.Set;
|
||||
import lombok.Getter;
|
||||
import me.totalfreedom.totalfreedommod.FreedomService;
|
||||
import me.totalfreedom.totalfreedommod.TotalFreedomMod;
|
||||
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
||||
import me.totalfreedom.totalfreedommod.util.FLog;
|
||||
import me.totalfreedom.totalfreedommod.util.FUtil;
|
||||
import net.pravian.aero.config.YamlConfig;
|
||||
import net.pravian.aero.util.Ips;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.bukkit.event.player.PlayerLoginEvent;
|
||||
|
||||
public class VPNBanList extends FreedomService
|
||||
{
|
||||
|
||||
public static final String CONFIG_FILENAME = "vpnbans.yml";
|
||||
|
||||
@Getter
|
||||
private final Set<String> vpnIps = Sets.newHashSet();
|
||||
|
||||
public VPNBanList(TotalFreedomMod plugin)
|
||||
{
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStart()
|
||||
{
|
||||
vpnIps.clear();
|
||||
|
||||
final YamlConfig config = new YamlConfig(plugin, CONFIG_FILENAME, true);
|
||||
config.load();
|
||||
|
||||
for (String name : config.getKeys(false))
|
||||
{
|
||||
vpnIps.addAll(config.getStringList(name));
|
||||
}
|
||||
|
||||
FLog.info("Loaded " + vpnIps.size() + " VPN ips.");
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onStop()
|
||||
{
|
||||
}
|
||||
|
||||
@EventHandler(priority = EventPriority.LOWEST)
|
||||
public void onPlayerLogin(PlayerLoginEvent event)
|
||||
{
|
||||
final String ip = Ips.getIp(event);
|
||||
|
||||
// Permbanned IPs
|
||||
for (String testIp : getVPNIps())
|
||||
{
|
||||
if (FUtil.fuzzyIpMatch(testIp, ip, 4))
|
||||
{
|
||||
event.disallow(PlayerLoginEvent.Result.KICK_OTHER,
|
||||
ChatColor.RED + "Your IP address is detected as a VPN\n"
|
||||
+ "If you believe this is an error, release procedures are available at\n"
|
||||
+ ChatColor.GOLD + ConfigEntry.SERVER_PERMBAN_URL.getString());
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Set<String> getVPNIps()
|
||||
{
|
||||
return this.vpnIps;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user