diff --git a/Plan/src/main/java/com/djrapitops/plan/Plan.java b/Plan/src/main/java/com/djrapitops/plan/Plan.java index e70f74007..4fffae102 100644 --- a/Plan/src/main/java/com/djrapitops/plan/Plan.java +++ b/Plan/src/main/java/com/djrapitops/plan/Plan.java @@ -44,13 +44,10 @@ import main.java.com.djrapitops.plan.utilities.Check; import main.java.com.djrapitops.plan.utilities.MiscUtils; import main.java.com.djrapitops.plan.utilities.metrics.BStats; import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.core.LoggerContext; -import org.apache.logging.log4j.core.config.LoggerConfig; import java.io.*; import java.net.MalformedURLException; import java.net.URL; -import java.util.Collection; import java.util.HashSet; import java.util.Set; import java.util.concurrent.Executors; @@ -133,12 +130,12 @@ public class Plan extends BukkitPlugin { Benchmark.start("Enable: Copy default config"); getConfig().options().copyDefaults(true); - getConfig().options().header(Phrase.CONFIG_HEADER + ""); + getConfig().options().header(Phrase.CONFIG_HEADER.toString()); saveConfig(); Benchmark.stop("Enable: Copy default config"); Benchmark.start("Enable: Init Database"); - Log.info(Phrase.DB_INIT + ""); + Log.info(Phrase.DB_INIT.toString()); if (Check.ErrorIfFalse(initDatabase(), Phrase.DB_FAILURE_DISABLE.toString())) { Log.info(Phrase.DB_ESTABLISHED.parse(db.getConfigName())); } else { @@ -186,15 +183,12 @@ public class Plan extends BukkitPlugin { if (webserverIsEnabled) { uiServer = new WebServer(this); uiServer.initServer(); + if (!uiServer.isEnabled()) { - Log.error("WebServer was not Initialized."); - } - // Prevent passwords showing up on console. - LoggerContext ctx = (LoggerContext) LogManager.getContext(false); - Collection lc = ctx.getConfiguration().getLoggers().values(); - for (LoggerConfig c : lc) { - c.addFilter(new RegisterCommandFilter()); + Log.error("WebServer was not successfully initialized."); } + + setupFilter(); } else if (!hasDataViewCapability) { Log.infoColor(Phrase.ERROR_NO_DATA_VIEW.toString()); } @@ -434,6 +428,14 @@ public class Plan extends BukkitPlugin { } /** + * Setups the command console output filter + */ + private void setupFilter() { + org.apache.logging.log4j.core.Logger logger = (org.apache.logging.log4j.core.Logger) LogManager.getRootLogger(); + logger.addFilter(new RegisterCommandFilter()); + } + + /** * Stops initializing the locale * * @param usingLocale The locale that's used diff --git a/Plan/src/main/java/com/djrapitops/plan/command/commands/RegisterCommandFilter.java b/Plan/src/main/java/com/djrapitops/plan/command/commands/RegisterCommandFilter.java index 1dd9f9372..c18f65af5 100644 --- a/Plan/src/main/java/com/djrapitops/plan/command/commands/RegisterCommandFilter.java +++ b/Plan/src/main/java/com/djrapitops/plan/command/commands/RegisterCommandFilter.java @@ -2,9 +2,9 @@ package main.java.com.djrapitops.plan.command.commands; import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.Logger; +import org.apache.logging.log4j.core.filter.AbstractFilter; import org.apache.logging.log4j.message.Message; /** @@ -13,128 +13,52 @@ import org.apache.logging.log4j.message.Message; * @author Rsl1122 * @since 3.5.2 */ -public class RegisterCommandFilter implements Filter { - - private boolean start = true; - - private Result filter(String message) { - boolean block = message.contains("command: /plan register") - || message.contains("command: /plan web register") - || message.contains("command: /plan webuser register"); - if (block) { - return Result.DENY; - } - return null; - } +public class RegisterCommandFilter extends AbstractFilter { @Override public Result filter(LogEvent event) { - String message = event.getMessage().toString().toLowerCase(); - return filter(message); + if (event == null) { + return Result.NEUTRAL; + } + + return validateMessage(event.getMessage()); } @Override - public Result filter(Logger arg0, Level arg1, Marker arg2, String arg3, Object... arg4) { - return filter(arg3); + public Result filter(Logger logger, Level level, Marker marker, Message msg, Throwable t) { + return validateMessage(msg); } @Override - public Result filter(Logger arg0, Level arg1, Marker arg2, Object arg3, Throwable arg4) { - return null; + public Result filter(Logger logger, Level level, Marker marker, String msg, Object... params) { + return validateMessage(msg); } @Override - public Result filter(Logger arg0, Level arg1, Marker arg2, Message arg3, Throwable arg4) { - return filter(arg3.toString()); + public Result filter(Logger logger, Level level, Marker marker, Object msg, Throwable t) { + if (msg == null) { + return Result.NEUTRAL; + } + + return validateMessage(msg.toString()); } - @Override - public Result getOnMatch() { - return null; + private static Result validateMessage(Message message) { + if (message == null) { + return Result.NEUTRAL; + } + + return validateMessage(message.getFormattedMessage()); } - @Override - public Result getOnMismatch() { - return null; + private static Result validateMessage(String message) { + return isSensibleCommand(message) + ? Result.DENY + : Result.NEUTRAL; } - @Override - public Result filter(Logger logger, Level level, Marker marker, String s, Object o) { - return filter(s); - } - - @Override - public Result filter(Logger logger, Level level, Marker marker, String s, Object o, Object o1) { - return filter(s); - } - - @Override - public Result filter(Logger logger, Level level, Marker marker, String s, Object o, Object o1, Object o2) { - return filter(s); - } - - @Override - public Result filter(Logger logger, Level level, Marker marker, String s, Object o, Object o1, Object o2, Object o3) { - return filter(s); - } - - @Override - public Result filter(Logger logger, Level level, Marker marker, String s, Object o, Object o1, Object o2, Object o3, Object o4) { - return filter(s); - } - - @Override - public Result filter(Logger logger, Level level, Marker marker, String s, Object o, Object o1, Object o2, Object o3, Object o4, Object o5) { - return filter(s); - } - - @Override - public Result filter(Logger logger, Level level, Marker marker, String s, Object o, Object o1, Object o2, Object o3, Object o4, Object o5, Object o6) { - return filter(s); - } - - @Override - public Result filter(Logger logger, Level level, Marker marker, String s, Object o, Object o1, Object o2, Object o3, Object o4, Object o5, Object o6, Object o7) { - return filter(s); - } - - @Override - public Result filter(Logger logger, Level level, Marker marker, String s, Object o, Object o1, Object o2, Object o3, Object o4, Object o5, Object o6, Object o7, Object o8) { - return filter(s); - } - - @Override - public Result filter(Logger logger, Level level, Marker marker, String s, Object o, Object o1, Object o2, Object o3, Object o4, Object o5, Object o6, Object o7, Object o8, Object o9) { - return filter(s); - } - - @Override - public State getState() { - return null; - } - - @Override - public void initialize() { - - } - - @Override - public void start() { - start = true; - } - - @Override - public void stop() { - start = false; - } - - @Override - public boolean isStarted() { - return start; - } - - @Override - public boolean isStopped() { - return !start; + private static boolean isSensibleCommand(String message) { + message = message.toLowerCase(); + return message.contains("issued server command:") && message.contains("/plan register"); } }