Prevent command arguments from using long number strings. Resolves #782

Mass format
This commit is contained in:
Jerom van der Sar 2015-09-06 17:02:06 +02:00
parent 3b87323c41
commit 400038265b
5 changed files with 179 additions and 85 deletions

View File

@ -1,3 +1,3 @@
#Build Number for ANT. Do not edit! #Build Number for ANT. Do not edit!
#Thu Jun 11 22:27:16 CEST 2015 #Sun Sep 06 17:00:40 CEST 2015
build.number=1054 build.number=1055

View File

@ -3,6 +3,8 @@ package me.StevenLawson.TotalFreedomMod;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader; import me.StevenLawson.TotalFreedomMod.Commands.TFM_CommandLoader;
import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry; import me.StevenLawson.TotalFreedomMod.Config.TFM_ConfigEntry;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -14,12 +16,8 @@ import org.bukkit.entity.Player;
public class TFM_CommandBlocker public class TFM_CommandBlocker
{ {
private static final Map<String, CommandBlockerEntry> BLOCKED_COMMANDS; public static Pattern NUMBER_FLAG_PATTERN = Pattern.compile("(:([0-9]){5,})");
private static final Map<String, CommandBlockerEntry> BLOCKED_COMMANDS = new HashMap<String, CommandBlockerEntry>();
static
{
BLOCKED_COMMANDS = new HashMap<String, CommandBlockerEntry>();
}
private TFM_CommandBlocker() private TFM_CommandBlocker()
{ {
@ -112,33 +110,50 @@ public class TFM_CommandBlocker
return false; return false;
} }
// Format
command = command.toLowerCase().trim(); command = command.toLowerCase().trim();
command = command.startsWith("/") ? command.substring(1) : command;
if (command.split(" ")[0].contains(":")) // Check for plugin specific commands
final String[] commandParts = command.split(" ");
if (commandParts[0].contains(":"))
{ {
TFM_Util.playerMsg(sender, "Plugin-specific commands are disabled."); if (doAction)
{
TFM_Util.playerMsg(sender, "Plugin specific commands are disabled.");
}
return true; return true;
} }
if (command.startsWith("/")) for (String part : commandParts)
{ {
command = command.substring(1); Matcher matcher = NUMBER_FLAG_PATTERN.matcher(part);
if (!matcher.matches())
{
continue;
}
if (doAction)
{
TFM_Util.playerMsg(sender, "That command contains an illegal number: " + matcher.group(1));
}
return true;
} }
final String[] commandParts = command.split(" "); // Obtain sub command, if it exists
String subCommand = null; String subCommand = null;
if (commandParts.length > 1) if (commandParts.length > 1)
{ {
subCommand = StringUtils.join(commandParts, " ", 1, commandParts.length).toLowerCase(); subCommand = StringUtils.join(commandParts, " ", 1, commandParts.length).toLowerCase();
} }
// Obtain entry
final CommandBlockerEntry entry = BLOCKED_COMMANDS.get(commandParts[0]); final CommandBlockerEntry entry = BLOCKED_COMMANDS.get(commandParts[0]);
if (entry == null) if (entry == null)
{ {
return false; return false;
} }
// Validate sub command
if (entry.getSubCommand() != null) if (entry.getSubCommand() != null)
{ {
if (subCommand == null || !subCommand.startsWith(entry.getSubCommand())) if (subCommand == null || !subCommand.startsWith(entry.getSubCommand()))

View File

@ -11,53 +11,66 @@ import me.StevenLawson.TotalFreedomMod.Config.TFM_Config;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class TFM_PlayerList { public class TFM_PlayerList
{
private static final Map<UUID, TFM_Player> PLAYER_LIST = new HashMap<UUID, TFM_Player>(); private static final Map<UUID, TFM_Player> PLAYER_LIST = new HashMap<UUID, TFM_Player>();
private TFM_PlayerList() { private TFM_PlayerList()
{
throw new AssertionError(); throw new AssertionError();
} }
public static Set<TFM_Player> getAllPlayers() { public static Set<TFM_Player> getAllPlayers()
{
return Collections.unmodifiableSet(Sets.newHashSet(PLAYER_LIST.values())); return Collections.unmodifiableSet(Sets.newHashSet(PLAYER_LIST.values()));
} }
public static void load() { public static void load()
{
PLAYER_LIST.clear(); PLAYER_LIST.clear();
// Load online players // Load online players
for (Player player : Bukkit.getOnlinePlayers()) { for (Player player : Bukkit.getOnlinePlayers())
{
getEntry(player); getEntry(player);
} }
TFM_Log.info("Loaded playerdata for " + PLAYER_LIST.size() + " players"); TFM_Log.info("Loaded playerdata for " + PLAYER_LIST.size() + " players");
} }
public static void saveAll() { public static void saveAll()
for (TFM_Player entry : PLAYER_LIST.values()) { {
for (TFM_Player entry : PLAYER_LIST.values())
{
save(entry); save(entry);
} }
} }
// May return null // May return null
public static TFM_Player getEntry(UUID uuid) { public static TFM_Player getEntry(UUID uuid)
if (PLAYER_LIST.containsKey(uuid)) { {
if (PLAYER_LIST.containsKey(uuid))
{
return PLAYER_LIST.get(uuid); return PLAYER_LIST.get(uuid);
} }
final File configFile = getConfigFile(uuid); final File configFile = getConfigFile(uuid);
if (!configFile.exists()) { if (!configFile.exists())
{
return null; return null;
} }
final TFM_Player entry = new TFM_Player(uuid, getConfig(uuid)); final TFM_Player entry = new TFM_Player(uuid, getConfig(uuid));
if (entry.isComplete()) { if (entry.isComplete())
{
PLAYER_LIST.put(uuid, entry); PLAYER_LIST.put(uuid, entry);
return entry; return entry;
} else { }
else
{
TFM_Log.warning("Could not load entry: Entry is not complete!"); TFM_Log.warning("Could not load entry: Entry is not complete!");
configFile.delete(); configFile.delete();
} }
@ -65,11 +78,13 @@ public class TFM_PlayerList {
return null; return null;
} }
public static TFM_Player getEntry(Player player) { public static TFM_Player getEntry(Player player)
{
final UUID uuid = TFM_UuidManager.getUniqueId(player); final UUID uuid = TFM_UuidManager.getUniqueId(player);
TFM_Player entry = getEntry(uuid); TFM_Player entry = getEntry(uuid);
if (entry != null) { if (entry != null)
{
return entry; return entry;
} }
@ -87,10 +102,12 @@ public class TFM_PlayerList {
return entry; return entry;
} }
public static void removeEntry(Player player) { public static void removeEntry(Player player)
{
final UUID uuid = TFM_UuidManager.getUniqueId(player); final UUID uuid = TFM_UuidManager.getUniqueId(player);
if (!PLAYER_LIST.containsKey(uuid)) { if (!PLAYER_LIST.containsKey(uuid))
{
return; return;
} }
@ -99,16 +116,20 @@ public class TFM_PlayerList {
PLAYER_LIST.remove(uuid); PLAYER_LIST.remove(uuid);
} }
public static boolean existsEntry(Player player) { public static boolean existsEntry(Player player)
{
return existsEntry(TFM_UuidManager.getUniqueId(player)); return existsEntry(TFM_UuidManager.getUniqueId(player));
} }
public static boolean existsEntry(UUID uuid) { public static boolean existsEntry(UUID uuid)
{
return getConfigFile(uuid).exists(); return getConfigFile(uuid).exists();
} }
public static void setUniqueId(TFM_Player entry, UUID newUuid) { public static void setUniqueId(TFM_Player entry, UUID newUuid)
if (entry.getUniqueId().equals(newUuid)) { {
if (entry.getUniqueId().equals(newUuid))
{
TFM_Log.warning("Not setting new UUID: UUIDs match!"); TFM_Log.warning("Not setting new UUID: UUIDs match!");
return; return;
} }
@ -127,13 +148,16 @@ public class TFM_PlayerList {
// Remove old entry // Remove old entry
PLAYER_LIST.remove(entry.getUniqueId()); PLAYER_LIST.remove(entry.getUniqueId());
final File oldFile = getConfigFile(entry.getUniqueId()); final File oldFile = getConfigFile(entry.getUniqueId());
if (oldFile.exists() && !oldFile.delete()) { if (oldFile.exists() && !oldFile.delete())
{
TFM_Log.warning("Could not delete config: " + getConfigFile(entry.getUniqueId()).getName()); TFM_Log.warning("Could not delete config: " + getConfigFile(entry.getUniqueId()).getName());
} }
} }
public static void purgeAll() { public static void purgeAll()
for (File file : getConfigFolder().listFiles()) { {
for (File file : getConfigFolder().listFiles())
{
file.delete(); file.delete();
} }
@ -141,22 +165,27 @@ public class TFM_PlayerList {
load(); load();
} }
public static File getConfigFolder() { public static File getConfigFolder()
{
return new File(TotalFreedomMod.plugin.getDataFolder(), "players"); return new File(TotalFreedomMod.plugin.getDataFolder(), "players");
} }
public static File getConfigFile(UUID uuid) { public static File getConfigFile(UUID uuid)
{
return new File(getConfigFolder(), uuid + ".yml"); return new File(getConfigFolder(), uuid + ".yml");
} }
public static TFM_Config getConfig(UUID uuid) { public static TFM_Config getConfig(UUID uuid)
{
final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, getConfigFile(uuid), false); final TFM_Config config = new TFM_Config(TotalFreedomMod.plugin, getConfigFile(uuid), false);
config.load(); config.load();
return config; return config;
} }
public static void save(TFM_Player entry) { public static void save(TFM_Player entry)
if (!entry.isComplete()) { {
if (!entry.isComplete())
{
throw new IllegalArgumentException("Entry is not complete!"); throw new IllegalArgumentException("Entry is not complete!");
} }

View File

@ -77,9 +77,12 @@ public class TFM_ServerInterface
continue; continue;
} }
if (!isAdmin) { if (!isAdmin)
{
event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Your username is already logged into this server."); event.disallow(AsyncPlayerPreLoginEvent.Result.KICK_OTHER, "Your username is already logged into this server.");
} else { }
else
{
event.allow(); event.allow();
TFM_Sync.playerKick(onlinePlayer, "An admin just logged in with the username you are using."); TFM_Sync.playerKick(onlinePlayer, "An admin just logged in with the username you are using.");
} }

View File

@ -25,18 +25,21 @@ import org.json.simple.JSONArray;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser; import org.json.simple.parser.JSONParser;
public class TFM_UuidManager { public class TFM_UuidManager
{
public static final String TABLE_NAME = "uuids"; public static final String TABLE_NAME = "uuids";
private static final TFM_SqliteDatabase SQL; private static final TFM_SqliteDatabase SQL;
private static final Statement FIND; private static final Statement FIND;
private static final Statement UPDATE; private static final Statement UPDATE;
private TFM_UuidManager() { private TFM_UuidManager()
{
throw new AssertionError(); throw new AssertionError();
} }
static { static
{
SQL = new TFM_SqliteDatabase( SQL = new TFM_SqliteDatabase(
"uuids.db", "uuids.db",
TABLE_NAME, TABLE_NAME,
@ -46,34 +49,40 @@ public class TFM_UuidManager {
UPDATE = SQL.addPreparedStatement("REPLACE INTO " + TABLE_NAME + " (username, uuid) VALUES (?, ?);"); UPDATE = SQL.addPreparedStatement("REPLACE INTO " + TABLE_NAME + " (username, uuid) VALUES (?, ?);");
} }
public static void load() { public static void load()
{
// Init DB // Init DB
SQL.connect(); SQL.connect();
} }
public static void close() { public static void close()
{
SQL.close(); SQL.close();
} }
public static int purge() { public static int purge()
{
return SQL.purge(); return SQL.purge();
} }
public static UUID newPlayer(Player player, String ip) { public static UUID newPlayer(Player player, String ip)
{
TFM_Log.info("Obtaining UUID for new player: " + player.getName()); TFM_Log.info("Obtaining UUID for new player: " + player.getName());
final String username = player.getName().toLowerCase(); final String username = player.getName().toLowerCase();
// Look in DB // Look in DB
final UUID dbUuid = find(username); final UUID dbUuid = find(username);
if (dbUuid != null) { if (dbUuid != null)
{
return dbUuid; return dbUuid;
} }
// Find UUID and update in DB if not found // Find UUID and update in DB if not found
// Try API // Try API
UUID uuid = TFM_UuidResolver.getUUIDOf(username); UUID uuid = TFM_UuidResolver.getUUIDOf(username);
if (uuid == null) { if (uuid == null)
{
// Spoof // Spoof
uuid = generateSpoofUuid(username); uuid = generateSpoofUuid(username);
} }
@ -82,9 +91,11 @@ public class TFM_UuidManager {
return uuid; return uuid;
} }
public static UUID getUniqueId(OfflinePlayer offlinePlayer) { public static UUID getUniqueId(OfflinePlayer offlinePlayer)
{
// Online check first // Online check first
if (offlinePlayer.isOnline() && TFM_PlayerData.hasPlayerData(offlinePlayer.getPlayer())) { if (offlinePlayer.isOnline() && TFM_PlayerData.hasPlayerData(offlinePlayer.getPlayer()))
{
return TFM_PlayerData.getPlayerData(offlinePlayer.getPlayer()).getUniqueId(); return TFM_PlayerData.getPlayerData(offlinePlayer.getPlayer()).getUniqueId();
} }
@ -92,16 +103,19 @@ public class TFM_UuidManager {
return getUniqueId(offlinePlayer.getName()); return getUniqueId(offlinePlayer.getName());
} }
public static UUID getUniqueId(String username) { public static UUID getUniqueId(String username)
{
// Look in DB // Look in DB
final UUID dbUuid = find(username); final UUID dbUuid = find(username);
if (dbUuid != null) { if (dbUuid != null)
{
return dbUuid; return dbUuid;
} }
// Try API // Try API
final UUID apiUuid = TFM_UuidResolver.getUUIDOf(username); final UUID apiUuid = TFM_UuidResolver.getUUIDOf(username);
if (apiUuid != null) { if (apiUuid != null)
{
return apiUuid; return apiUuid;
} }
@ -109,8 +123,10 @@ public class TFM_UuidManager {
return generateSpoofUuid(username); return generateSpoofUuid(username);
} }
public static void rawSetUUID(String name, UUID uuid) { public static void rawSetUUID(String name, UUID uuid)
if (name == null || uuid == null || name.isEmpty()) { {
if (name == null || uuid == null || name.isEmpty())
{
TFM_Log.warning("Not setting raw UUID: name and uuid may not be null!"); TFM_Log.warning("Not setting raw UUID: name and uuid may not be null!");
return; return;
} }
@ -118,67 +134,86 @@ public class TFM_UuidManager {
update(name.toLowerCase().trim(), uuid); update(name.toLowerCase().trim(), uuid);
} }
private static UUID find(String searchName) { private static UUID find(String searchName)
if (!SQL.connect()) { {
if (!SQL.connect())
{
return null; return null;
} }
final ResultSet result; final ResultSet result;
try { try
{
final PreparedStatement statement = FIND.getStatement(); final PreparedStatement statement = FIND.getStatement();
statement.clearParameters(); statement.clearParameters();
statement.setString(1, searchName.toLowerCase()); statement.setString(1, searchName.toLowerCase());
result = statement.executeQuery(); result = statement.executeQuery();
} catch (Exception ex) { }
catch (Exception ex)
{
TFM_Log.severe("Could not execute find statement!"); TFM_Log.severe("Could not execute find statement!");
TFM_Log.severe(ex); TFM_Log.severe(ex);
return null; return null;
} }
if (!TFM_SqlUtil.hasData(result)) { if (!TFM_SqlUtil.hasData(result))
{
TFM_SqlUtil.close(result); TFM_SqlUtil.close(result);
return null; return null;
} }
try { try
{
final String uuidString = result.getString("uuid"); final String uuidString = result.getString("uuid");
return UUID.fromString(uuidString); return UUID.fromString(uuidString);
} catch (Exception ex) { }
catch (Exception ex)
{
TFM_Log.severe(ex); TFM_Log.severe(ex);
return null; return null;
} finally { }
finally
{
TFM_SqlUtil.close(result); TFM_SqlUtil.close(result);
} }
} }
private static boolean update(String username, UUID uuid) { private static boolean update(String username, UUID uuid)
if (!SQL.connect()) { {
if (!SQL.connect())
{
return false; return false;
} }
try { try
{
final PreparedStatement statement = UPDATE.getStatement(); final PreparedStatement statement = UPDATE.getStatement();
statement.clearParameters(); statement.clearParameters();
statement.setString(1, username.toLowerCase()); statement.setString(1, username.toLowerCase());
statement.setString(2, uuid.toString()); statement.setString(2, uuid.toString());
statement.executeUpdate(); statement.executeUpdate();
return true; return true;
} catch (Exception ex) { }
catch (Exception ex)
{
TFM_Log.severe("Could not execute update statement!"); TFM_Log.severe("Could not execute update statement!");
TFM_Log.severe(ex); TFM_Log.severe(ex);
return false; return false;
} }
} }
private static UUID generateSpoofUuid(String name) { private static UUID generateSpoofUuid(String name)
{
name = name.toLowerCase(); name = name.toLowerCase();
TFM_Log.info("Generating spoof UUID for " + name); TFM_Log.info("Generating spoof UUID for " + name);
try { try
{
final MessageDigest digest = MessageDigest.getInstance("SHA1"); final MessageDigest digest = MessageDigest.getInstance("SHA1");
final byte[] result = digest.digest(name.getBytes()); final byte[] result = digest.digest(name.getBytes());
final StringBuilder builder = new StringBuilder(); final StringBuilder builder = new StringBuilder();
for (int i = 0; i < result.length; i++) { for (int i = 0; i < result.length; i++)
{
builder.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1)); builder.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
} }
@ -188,30 +223,37 @@ public class TFM_UuidManager {
+ "-" + builder.substring(12, 16) + "-" + builder.substring(12, 16)
+ "-" + builder.substring(16, 20) + "-" + builder.substring(16, 20)
+ "-" + builder.substring(20, 32)); + "-" + builder.substring(20, 32));
} catch (NoSuchAlgorithmException ex) { }
catch (NoSuchAlgorithmException ex)
{
TFM_Log.warning("Could not generate spoof UUID: SHA1 algorithm not found!"); TFM_Log.warning("Could not generate spoof UUID: SHA1 algorithm not found!");
} }
return UUID.randomUUID(); return UUID.randomUUID();
} }
public static class TFM_UuidResolver implements Callable<Map<String, UUID>> { public static class TFM_UuidResolver implements Callable<Map<String, UUID>>
{
private static final double PROFILES_PER_REQUEST = 100; private static final double PROFILES_PER_REQUEST = 100;
private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft"; private static final String PROFILE_URL = "https://api.mojang.com/profiles/minecraft";
private final JSONParser jsonParser = new JSONParser(); private final JSONParser jsonParser = new JSONParser();
private final List<String> names; private final List<String> names;
public TFM_UuidResolver(List<String> names) { public TFM_UuidResolver(List<String> names)
{
this.names = ImmutableList.copyOf(names); this.names = ImmutableList.copyOf(names);
} }
@Override @Override
public Map<String, UUID> call() { public Map<String, UUID> call()
{
final Map<String, UUID> uuidMap = new HashMap<String, UUID>(); final Map<String, UUID> uuidMap = new HashMap<String, UUID>();
int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST); int requests = (int) Math.ceil(names.size() / PROFILES_PER_REQUEST);
for (int i = 0; i < requests; i++) { for (int i = 0; i < requests; i++)
try { {
try
{
final URL url = new URL(PROFILE_URL); final URL url = new URL(PROFILE_URL);
final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
@ -230,7 +272,8 @@ public class TFM_UuidManager {
final JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream())); final JSONArray array = (JSONArray) jsonParser.parse(new InputStreamReader(connection.getInputStream()));
for (Object profile : array) { for (Object profile : array)
{
final JSONObject jsonProfile = (JSONObject) profile; final JSONObject jsonProfile = (JSONObject) profile;
final String id = (String) jsonProfile.get("id"); final String id = (String) jsonProfile.get("id");
final String name = (String) jsonProfile.get("name"); final String name = (String) jsonProfile.get("name");
@ -243,10 +286,13 @@ public class TFM_UuidManager {
uuidMap.put(name, uuid); uuidMap.put(name, uuid);
} }
if (i != requests - 1) { if (i != requests - 1)
{
Thread.sleep(100L); Thread.sleep(100L);
} }
} catch (Exception ex) { }
catch (Exception ex)
{
TFM_Log.severe("Could not resolve UUID(s) of " TFM_Log.severe("Could not resolve UUID(s) of "
+ StringUtils.join(names.subList(i * 100, Math.min((i + 1) * 100, names.size())), ", ")); + StringUtils.join(names.subList(i * 100, Math.min((i + 1) * 100, names.size())), ", "));
//TFM_Log.severe(ex); //TFM_Log.severe(ex);
@ -255,7 +301,8 @@ public class TFM_UuidManager {
return uuidMap; return uuidMap;
} }
public static UUID getUUIDOf(String name) { public static UUID getUUIDOf(String name)
{
return new TFM_UuidResolver(Arrays.asList(name)).call().get(name); return new TFM_UuidResolver(Arrays.asList(name)).call().get(name);
} }
} }