From 73c30f52147ec3ddfd027a3ccfbfaaa6d0c84448 Mon Sep 17 00:00:00 2001 From: Justin Crawford Date: Fri, 4 Oct 2019 23:18:59 -0700 Subject: [PATCH] You can now configure the MOTD, also fixed an old bug. You can customize the MOTD you see when you login (with the exception of what server you're logged into and some other info) and this includes colorization support via minecraft color codes (you need the special color code character that minecraft uses though, the example motd.txt has an included rainbow you can work from.) Hopefully fixed an old bug with the logger writting to the disconnected client session which causes a ton of exceptions because the session is gone. --- pom.xml | 1 + .../ryanmichela/sshd/ConsoleShellFactory.java | 36 +++++++++++++------ .../java/com/ryanmichela/sshd/SshdPlugin.java | 16 ++++++++- src/main/resources/motd.txt | 8 +++++ 4 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 src/main/resources/motd.txt diff --git a/pom.xml b/pom.xml index e85a438..2110fd3 100644 --- a/pom.xml +++ b/pom.xml @@ -130,6 +130,7 @@ plugin.yml config.yml + motd.txt diff --git a/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java b/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java index acf79e6..b9d6a24 100644 --- a/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java +++ b/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java @@ -20,6 +20,10 @@ import org.apache.sshd.server.Environment; import org.apache.sshd.server.ExitCallback; import org.bukkit.Bukkit; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.BufferedReader; +import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; @@ -89,9 +93,9 @@ public class ConsoleShellFactory implements ShellFactory { this.ConsoleReader.addCompleter(new ConsoleCommandCompleter()); StreamHandler streamHandler = new FlushyStreamHandler(out, new ConsoleLogFormatter(), this.ConsoleReader); - streamHandlerAppender = new StreamHandlerAppender(streamHandler); + this.streamHandlerAppender = new StreamHandlerAppender(streamHandler); - ((Logger)LogManager.getRootLogger()).addAppender(streamHandlerAppender); + ((Logger)LogManager.getRootLogger()).addAppender(this.streamHandlerAppender); this.environment = env; this.Username = env.getEnv().get(Environment.ENV_USER); @@ -107,7 +111,7 @@ public class ConsoleShellFactory implements ShellFactory { } @Override - public void destroy(ChannelSession cs) { ((Logger)LogManager.getRootLogger()).removeAppender(streamHandlerAppender); } + public void destroy(ChannelSession cs) { ((Logger)LogManager.getRootLogger()).removeAppender(this.streamHandlerAppender); } public void run() { @@ -150,6 +154,9 @@ public class ConsoleShellFactory implements ShellFactory { } }); } + // This should help stop one of the bugs where bytes are waiting to be written + // but the client fucked off already so the plugin throws an exception. + ((Logger)LogManager.getRootLogger()).removeAppender(this.streamHandlerAppender); } catch (IOException e) { @@ -164,16 +171,25 @@ public class ConsoleShellFactory implements ShellFactory { private void printPreamble(ConsoleReader cr) throws IOException { - cr.println(" _____ _____ _ _ _____" + "\r"); - cr.println(" / ____/ ____| | | | __ \\" + "\r"); - cr.println("| (___| (___ | |__| | | | |" + "\r"); - cr.println(" \\___ \\\\___ \\| __ | | | |" + "\r"); - cr.println(" ____) |___) | | | | |__| |" + "\r"); - cr.println("|_____/_____/|_| |_|_____/" + "\r"); + File f = new File(SshdPlugin.instance.getDataFolder(), "motd.txt"); + try + { + BufferedReader br = new BufferedReader(new FileReader(f)); + + String st; + while ((st = br.readLine()) != null) + cr.println(ConsoleLogFormatter.ColorizeString(st) + "\r"); + } + catch (FileNotFoundException e) + { + SshdPlugin.instance.getLogger().log(Level.WARNING, "Could not open " + f + ": File does not exist."); + // Not showing the SSH motd is not a fatal failure, let the session continue. + } + // 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(ConsoleLogFormatter.ColorizeString(Bukkit.getServer().getMotd()).replaceAll("\n", "\r\n")); cr.println("\r"); cr.println("Type 'exit' to exit the shell." + "\r"); cr.println("===============================================" + "\r"); diff --git a/src/main/java/com/ryanmichela/sshd/SshdPlugin.java b/src/main/java/com/ryanmichela/sshd/SshdPlugin.java index 01e48c2..53f4c0c 100644 --- a/src/main/java/com/ryanmichela/sshd/SshdPlugin.java +++ b/src/main/java/com/ryanmichela/sshd/SshdPlugin.java @@ -10,7 +10,9 @@ import com.ryanmichela.sshd.ConsoleShellFactory; import java.io.File; import java.io.IOException; +import java.io.InputStream; import java.nio.file.FileSystems; +import java.nio.file.Files; import java.util.Collections; import java.util.logging.Level; @@ -29,8 +31,20 @@ class SshdPlugin extends JavaPlugin saveDefaultConfig(); File authorizedKeys = new File(getDataFolder(), "authorized_keys"); if (!authorizedKeys.exists()) - { authorizedKeys.mkdirs(); + + try + { + File motd = new File(getDataFolder(), "motd.txt"); + if (!motd.exists()) + { + InputStream link = (getClass().getResourceAsStream("/motd.txt")); + Files.copy(link, motd.getAbsoluteFile().toPath()); + } + } + catch (IOException e) + { + e.printStackTrace(); } // Don't go any lower than INFO or SSHD will cause a stack overflow exception. diff --git a/src/main/resources/motd.txt b/src/main/resources/motd.txt new file mode 100644 index 0000000..284c9a6 --- /dev/null +++ b/src/main/resources/motd.txt @@ -0,0 +1,8 @@ +§l§4 _____ _____ _ _ _____§r +§l§6 / ____/ ____| | | | __ \§r +§l§2| (___| (___ | |__| | | | |§r +§l§3 \___ \\___ \| __ | | | |§r +§l§9 ____) |___) | | | | |__| |§r +§l§5|_____/_____/|_| |_|_____/§r + +=============================================== \ No newline at end of file