TFM-4.3-Reloaded/src/me/StevenLawson/TotalFreedomMod/TFM_ServerInterface.java

367 lines
12 KiB
Java
Raw Normal View History

package me.StevenLawson.TotalFreedomMod;
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
2014-04-04 11:45:35 +00:00
import net.minecraft.server.v1_7_R2.BanEntry;
import net.minecraft.server.v1_7_R2.BanList;
import net.minecraft.server.v1_7_R2.MinecraftServer;
import net.minecraft.server.v1_7_R2.PlayerList;
import net.minecraft.server.v1_7_R2.PropertyManager;
import org.bukkit.ChatColor;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerLoginEvent;
import org.bukkit.event.player.PlayerLoginEvent.Result;
public class TFM_ServerInterface
{
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd \'at\' HH:mm:ss z");
public static void setOnlineMode(boolean mode)
{
PropertyManager propertyManager = MinecraftServer.getServer().getPropertyManager();
propertyManager.a("online-mode", mode);
propertyManager.savePropertiesFile();
}
public static void wipeNameBans()
{
2012-12-21 02:33:22 +00:00
BanList nameBans = MinecraftServer.getServer().getPlayerList().getNameBans();
nameBans.getEntries().clear();
nameBans.save();
}
public static void wipeIpBans()
{
2012-12-21 02:33:22 +00:00
BanList IPBans = MinecraftServer.getServer().getPlayerList().getIPBans();
IPBans.getEntries().clear();
IPBans.save();
}
public static void unbanUsername(String name)
{
name = name.toLowerCase().trim();
2012-12-21 02:33:22 +00:00
BanList nameBans = MinecraftServer.getServer().getPlayerList().getNameBans();
nameBans.remove(name);
}
2013-08-28 15:26:08 +00:00
@SuppressWarnings("unchecked")
public static void banUsername(String name, String reason, String source, Date expireDate)
{
name = name.toLowerCase().trim();
2013-08-26 22:44:18 +00:00
if (TFM_AdminList.getSuperadminUUIDs().contains(name))
{
TFM_Log.info("Not banning username " + name + ": is superadmin");
return;
}
2013-08-26 22:44:18 +00:00
2013-08-25 15:32:24 +00:00
for (String username : (List<String>) TFM_ConfigEntry.UNBANNABLE_USERNAMES.getList())
{
if (username.toLowerCase().trim().equals(name))
{
TFM_Log.info("Not banning username " + name + ": is unbannable as defined in config.");
return;
}
}
2013-08-26 22:44:18 +00:00
BanEntry entry = new BanEntry(name);
if (expireDate != null)
{
entry.setExpires(expireDate);
}
if (reason != null)
{
entry.setReason(reason);
}
if (source != null)
{
entry.setSource(source);
}
2012-12-21 02:33:22 +00:00
BanList nameBans = MinecraftServer.getServer().getPlayerList().getNameBans();
nameBans.add(entry);
}
public static boolean isNameBanned(String name)
{
name = name.toLowerCase().trim();
2012-12-21 02:33:22 +00:00
BanList nameBans = MinecraftServer.getServer().getPlayerList().getNameBans();
nameBans.removeExpired();
return nameBans.getEntries().containsKey(name);
}
public static void banIP(String ip, String reason, String source, Date expireDate)
{
ip = ip.toLowerCase().trim();
BanEntry entry = new BanEntry(ip);
if (expireDate != null)
{
entry.setExpires(expireDate);
}
if (reason != null)
{
entry.setReason(reason);
}
if (source != null)
{
entry.setSource(source);
}
2012-12-21 02:33:22 +00:00
BanList ipBans = MinecraftServer.getServer().getPlayerList().getIPBans();
ipBans.add(entry);
}
public static void unbanIP(String ip)
{
ip = ip.toLowerCase().trim();
2012-12-21 02:33:22 +00:00
BanList ipBans = MinecraftServer.getServer().getPlayerList().getIPBans();
ipBans.remove(ip);
}
public static boolean isIPBanned(String ip)
{
ip = ip.toLowerCase().trim();
2012-12-21 02:33:22 +00:00
BanList ipBans = MinecraftServer.getServer().getPlayerList().getIPBans();
ipBans.removeExpired();
return ipBans.getEntries().containsKey(ip);
}
public static int purgeWhitelist()
{
2012-12-21 02:33:22 +00:00
Set whitelisted = MinecraftServer.getServer().getPlayerList().getWhitelisted();
int size = whitelisted.size();
whitelisted.clear();
return size;
}
public static void handlePlayerLogin(PlayerLoginEvent event)
{
// this should supersede all other onPlayerLogin authentication on the TFM server.
// when using the TFM CraftBukkit, CraftBukkit itself should not do any of its own authentication.
final Server server = TotalFreedomMod.server;
final PlayerList playerList = MinecraftServer.getServer().getPlayerList();
final BanList ipBans = playerList.getIPBans();
final BanList nameBans = playerList.getNameBans();
2013-08-14 14:01:42 +00:00
final Player player = event.getPlayer();
final String username = player.getName();
final String ip = event.getAddress().getHostAddress().trim();
if (username.trim().length() <= 2)
{
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Your username is too short (must be at least 3 characters long).");
return;
}
else if (Pattern.compile("[^a-zA-Z0-9\\-\\.\\_]").matcher(username).find())
{
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Your username contains invalid characters.");
return;
}
2014-04-08 18:31:04 +00:00
// not safe to use TFM_Util.isSuperAdmin for player logging in because player.getAddress() will return a null until after player login.
boolean isSuperadmin;
if (server.getOnlineMode())
{
isSuperadmin = TFM_AdminList.getSuperadminUUIDs().contains(username.toLowerCase());
}
else
{
isSuperadmin = TFM_AdminList.checkPartialSuperadminIp(ip, username.toLowerCase());
}
// Validation below this point
if (!isSuperadmin) // non-admins
{
// banned-players.txt
if (nameBans.isBanned(username.toLowerCase()))
{
final BanEntry nameBan = (BanEntry) nameBans.getEntries().get(username.toLowerCase());
2013-12-03 15:41:43 +00:00
String kickMessage = ChatColor.RED + "You are temporarily banned from this server.\nAppeal at http://totalfreedom.boards.net/.";
if (nameBan != null)
{
kickMessage = kickMessage + "\nReason: " + nameBan.getReason();
if (nameBan.getExpires() != null)
{
kickMessage = kickMessage + "\nYour ban will be removed on " + dateFormat.format(nameBan.getExpires());
}
}
event.disallow(Result.KICK_OTHER, kickMessage);
return;
}
// banned-ips.txt
final Iterator ipBansIt = ipBans.getEntries().keySet().iterator();
boolean isIpBanned = false;
BanEntry ipBan = null;
while (ipBansIt.hasNext())
{
String testIp = (String) ipBansIt.next();
if (!testIp.matches("^\\d{1,3}\\.\\d{1,3}\\.(\\d{1,3}|\\*)\\.(\\d{1,3}|\\*)$"))
{
continue;
}
if (ip.equals(testIp))
{
isIpBanned = true;
ipBan = (BanEntry) ipBans.getEntries().get(testIp);
break;
}
if (TFM_Util.fuzzyIpMatch(testIp, ip, 4))
{
isIpBanned = true;
ipBan = (BanEntry) ipBans.getEntries().get(testIp);
break;
}
}
if (isIpBanned)
{
2013-12-03 15:41:43 +00:00
String kickMessage = ChatColor.RED + "Your IP address is temporarily banned from this server.\nAppeal at http://totalfreedom.boards.net/.";
if (ipBan != null)
{
kickMessage = kickMessage + "\nReason: " + ipBan.getReason();
if (ipBan.getExpires() != null)
{
kickMessage = kickMessage + "\nYour ban will be removed on " + dateFormat.format(ipBan.getExpires());
}
}
event.disallow(Result.KICK_OTHER, kickMessage);
return;
}
// permban.yml - ips
for (String testIp : TotalFreedomMod.permbannedIps)
{
if (TFM_Util.fuzzyIpMatch(testIp, ip, 4))
{
event.disallow(Result.KICK_OTHER, ChatColor.RED + "Your IP address is permanently banned from this server.\nRelease procedures are available at http://bit.ly/TF_PermBan");
return;
}
}
// permban.yml - names
for (String testPlayer : TotalFreedomMod.permbannedPlayers)
{
if (testPlayer.equalsIgnoreCase(username))
{
event.disallow(Result.KICK_OTHER, ChatColor.RED + "Your username is permanently banned from this server.\nRelease procedures are available at http://bit.ly/TF_PermBan");
return;
}
}
// Server full check
if (server.getOnlinePlayers().length >= server.getMaxPlayers())
{
event.disallow(PlayerLoginEvent.Result.KICK_FULL, "Sorry, but this server is full.");
return;
}
// Admin-only mode
2013-08-18 18:52:46 +00:00
if (TFM_ConfigEntry.ADMIN_ONLY_MODE.getBoolean())
{
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Server is temporarily open to admins only.");
return;
}
// Lockdown mode
2013-08-12 18:47:36 +00:00
if (TotalFreedomMod.lockdownEnabled)
{
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Server is currently in lockdown mode.");
return;
}
// Whitelist check
if (playerList.hasWhitelist)
{
if (!playerList.getWhitelisted().contains(username.toLowerCase()))
{
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "You are not whitelisted on this server.");
return;
}
}
// Username already logged in check
for (Player test_player : server.getOnlinePlayers())
{
if (test_player.getName().equalsIgnoreCase(username))
{
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Your username is already logged into this server.");
return;
}
}
}
else // Player is superadmin
2013-07-02 18:31:22 +00:00
{
// force-allow superadmins to log in
event.allow();
if (isIPBanned(ip))
{
unbanIP(ip);
}
if (isNameBanned(username))
{
unbanUsername(username);
}
for (Player testPlayer : server.getOnlinePlayers())
{
if (testPlayer.getName().equalsIgnoreCase(username))
{
testPlayer.kickPlayer("An admin just logged in with the username you are using.");
}
}
2013-07-02 18:31:22 +00:00
int count = server.getOnlinePlayers().length;
if (count >= server.getMaxPlayers())
{
for (Player p : server.getOnlinePlayers())
{
if (!TFM_AdminList.isSuperAdmin(p))
{
p.kickPlayer("You have been kicked to free up room for an admin.");
count--;
}
if (count < server.getMaxPlayers())
{
break;
}
}
}
2013-08-12 18:47:36 +00:00
if (count >= server.getMaxPlayers())
2013-08-12 18:47:36 +00:00
{
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "The server is full and a player could not be kicked, sorry!");
return;
2013-08-12 18:47:36 +00:00
}
}
if (TotalFreedomMod.lockdownEnabled)
{
TFM_Util.playerMsg(player, "Warning: Server is currenty in lockdown-mode, new players will not be able to join!", ChatColor.RED);
}
}
public static String getVersion()
{
return MinecraftServer.getServer().getVersion();
}
}