From 3e45f7ebf4bee9c8cfd716f2f8487427a5201f4d Mon Sep 17 00:00:00 2001 From: Justin Crawford Date: Thu, 3 Oct 2019 22:16:29 -0700 Subject: [PATCH] Tweak the SSH MOTD a bit --- pom.xml | 2 +- .../ryanmichela/sshd/ConsoleLogFormatter.java | 94 ++++++++++--------- .../ryanmichela/sshd/ConsoleShellFactory.java | 9 +- .../sshd/PublicKeyAuthenticator.java | 2 - 4 files changed, 56 insertions(+), 51 deletions(-) diff --git a/pom.xml b/pom.xml index c3873fd..e85a438 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.ryanmichela sshd - 1.3.4.2 + 1.3.5 https://github.com/Justasic/Bukkit-SSHD/ diff --git a/src/main/java/com/ryanmichela/sshd/ConsoleLogFormatter.java b/src/main/java/com/ryanmichela/sshd/ConsoleLogFormatter.java index 4df1697..c8cd6dd 100644 --- a/src/main/java/com/ryanmichela/sshd/ConsoleLogFormatter.java +++ b/src/main/java/com/ryanmichela/sshd/ConsoleLogFormatter.java @@ -18,11 +18,55 @@ import java.util.logging.LogRecord; public class ConsoleLogFormatter extends Formatter { private SimpleDateFormat dateFormat; + private static final Map replacements = new EnumMap(ChatColor.class); public ConsoleLogFormatter() { this.dateFormat = new SimpleDateFormat("HH:mm:ss"); } + public static String ColorizeString(String str) + { + // ORIGINAL CODE FROM org.bukkit.craftbukkit.command.ColouredConsoleSender + + replacements.put(ChatColor.BLACK, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString()); + replacements.put(ChatColor.DARK_BLUE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString()); + replacements.put(ChatColor.DARK_GREEN, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString()); + replacements.put(ChatColor.DARK_AQUA, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.CYAN).boldOff().toString()); + replacements.put(ChatColor.DARK_RED, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.RED).boldOff().toString()); + replacements.put(ChatColor.DARK_PURPLE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.MAGENTA).boldOff().toString()); + replacements.put(ChatColor.GOLD, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.YELLOW).boldOff().toString()); + replacements.put(ChatColor.GRAY, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.WHITE).boldOff().toString()); + replacements.put(ChatColor.DARK_GRAY, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLACK).bold().toString()); + replacements.put(ChatColor.BLUE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLUE).bold().toString()); + replacements.put(ChatColor.GREEN, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.GREEN).bold().toString()); + replacements.put(ChatColor.AQUA, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.CYAN).bold().toString()); + replacements.put(ChatColor.RED, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.RED).bold().toString()); + replacements.put(ChatColor.LIGHT_PURPLE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.MAGENTA).bold().toString()); + replacements.put(ChatColor.YELLOW, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.YELLOW).bold().toString()); + replacements.put(ChatColor.WHITE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.WHITE).bold().toString()); + replacements.put(ChatColor.MAGIC, Ansi.ansi().a(Ansi.Attribute.BLINK_SLOW).toString()); + replacements.put(ChatColor.BOLD, Ansi.ansi().a(Ansi.Attribute.UNDERLINE_DOUBLE).toString()); + replacements.put(ChatColor.STRIKETHROUGH, Ansi.ansi().a(Ansi.Attribute.STRIKETHROUGH_ON).toString()); + replacements.put(ChatColor.UNDERLINE, Ansi.ansi().a(Ansi.Attribute.UNDERLINE).toString()); + replacements.put(ChatColor.ITALIC, Ansi.ansi().a(Ansi.Attribute.ITALIC).toString()); + replacements.put(ChatColor.RESET, Ansi.ansi().a(Ansi.Attribute.RESET).toString()); + + String result = str; + for (ChatColor color : ChatColor.values()) + { + if (replacements.containsKey(color)) + { + result = result.replaceAll("(?i)" + color.toString(), replacements.get(color)); + } + else + { + result = result.replaceAll("(?i)" + color.toString(), ""); + } + } + result += Ansi.ansi().reset().toString(); + return result; + } + public String format(LogRecord logrecord) { try { Class.forName("org.bukkit.craftbukkit.command.ColouredConsoleSender"); @@ -50,52 +94,10 @@ public class ConsoleLogFormatter extends Formatter { return stringbuilder.toString(); } - private void colorize(LogRecord logrecord) { - // ORIGINAL CODE FROM org.bukkit.craftbukkit.command.ColouredConsoleSender - final Map replacements = new EnumMap<>(ChatColor.class); - - replacements - .put(ChatColor.BLACK, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString()); - replacements - .put(ChatColor.DARK_BLUE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString()); - replacements.put(ChatColor.DARK_GREEN, - Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString()); - replacements - .put(ChatColor.DARK_AQUA, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.CYAN).boldOff().toString()); - replacements - .put(ChatColor.DARK_RED, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.RED).boldOff().toString()); - replacements.put(ChatColor.DARK_PURPLE, - Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.MAGENTA).boldOff().toString()); - replacements - .put(ChatColor.GOLD, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.YELLOW).boldOff().toString()); - replacements.put(ChatColor.GRAY, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.WHITE).boldOff().toString()); - replacements - .put(ChatColor.DARK_GRAY, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLACK).bold().toString()); - replacements.put(ChatColor.BLUE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLUE).bold().toString()); - replacements.put(ChatColor.GREEN, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.GREEN).bold().toString()); - replacements.put(ChatColor.AQUA, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.CYAN).bold().toString()); - replacements.put(ChatColor.RED, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.RED).bold().toString()); - replacements.put(ChatColor.LIGHT_PURPLE, - Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.MAGENTA).bold().toString()); - replacements.put(ChatColor.YELLOW, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.YELLOW).bold().toString()); - replacements.put(ChatColor.WHITE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.WHITE).bold().toString()); - replacements.put(ChatColor.MAGIC, Ansi.ansi().a(Ansi.Attribute.BLINK_SLOW).toString()); - replacements.put(ChatColor.BOLD, Ansi.ansi().a(Ansi.Attribute.UNDERLINE_DOUBLE).toString()); - replacements.put(ChatColor.STRIKETHROUGH, Ansi.ansi().a(Ansi.Attribute.STRIKETHROUGH_ON).toString()); - replacements.put(ChatColor.UNDERLINE, Ansi.ansi().a(Ansi.Attribute.UNDERLINE).toString()); - replacements.put(ChatColor.ITALIC, Ansi.ansi().a(Ansi.Attribute.ITALIC).toString()); - replacements.put(ChatColor.RESET, Ansi.ansi().a(Ansi.Attribute.RESET).toString()); - - String result = logrecord.getMessage(); - for (ChatColor color : ChatColor.values()) { - if (replacements.containsKey(color)) { - result = result.replaceAll("(?i)" + color.toString(), replacements.get(color)); - } else { - result = result.replaceAll("(?i)" + color.toString(), ""); - } - } - result += Ansi.ansi().reset().toString(); - logrecord.setMessage(result); + private void colorize(LogRecord logrecord) + { + String result = ColorizeString(logrecord.getMessage()); + logrecord.setMessage(result); } } diff --git a/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java b/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java index 15acb7c..acf79e6 100644 --- a/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java +++ b/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java @@ -8,6 +8,7 @@ import com.ryanmichela.sshd.SshTerminal; import com.ryanmichela.sshd.SshdPlugin; import com.ryanmichela.sshd.StreamHandlerAppender; import com.ryanmichela.sshd.implementations.SSHDCommandSender; +import com.ryanmichela.sshd.ConsoleLogFormatter; import jline.console.ConsoleReader; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.core.Logger; @@ -22,6 +23,7 @@ import org.bukkit.Bukkit; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.net.InetAddress; import java.util.logging.Level; import java.util.logging.StreamHandler; @@ -155,6 +157,7 @@ public class ConsoleShellFactory implements ShellFactory { } finally { + SshdPlugin.instance.getLogger().log(Level.INFO, this.Username + " disconnected from SSH."); callback.onExit(0); } } @@ -167,8 +170,10 @@ public class ConsoleShellFactory implements ShellFactory { cr.println(" \\___ \\\\___ \\| __ | | | |" + "\r"); cr.println(" ____) |___) | | | | |__| |" + "\r"); cr.println("|_____/_____/|_| |_|_____/" + "\r"); - cr.println("Connected to: " + Bukkit.getServer().getName() + "\r"); - cr.println("- " + Bukkit.getServer().getMotd() + "\r"); + // Doesn't really guarantee our actual system hostname but + // it's better than not having one at all. + cr.println("Connected to: " + InetAddress.getLocalHost().getHostName() + " (" + Bukkit.getServer().getName() + ")\r"); + cr.println(ConsoleLogFormatter.ColorizeString(Bukkit.getServer().getMotd()) + "\r"); cr.println("\r"); cr.println("Type 'exit' to exit the shell." + "\r"); cr.println("===============================================" + "\r"); diff --git a/src/main/java/com/ryanmichela/sshd/PublicKeyAuthenticator.java b/src/main/java/com/ryanmichela/sshd/PublicKeyAuthenticator.java index 0a14696..4543553 100644 --- a/src/main/java/com/ryanmichela/sshd/PublicKeyAuthenticator.java +++ b/src/main/java/com/ryanmichela/sshd/PublicKeyAuthenticator.java @@ -42,8 +42,6 @@ public class PublicKeyAuthenticator implements PublickeyAuthenticator // Validate that the logging in user has the same valid SSH key if (auth.authenticate(username, key, session)) { - SshdPlugin.instance.getLogger().info( - username + " successfully authenticated via SSH session using key file " + keyFile.getAbsolutePath()); FailCounts.put(username, 0); return true; }