From d348e26b49ba3277d22c021c62e5eab3e494a216 Mon Sep 17 00:00:00 2001 From: Justin Crawford Date: Mon, 6 Apr 2020 06:06:47 -0700 Subject: [PATCH] Fixed a bug where sometimes sessions are disconnected due to some hosts not resolving localhost properly --- .../ryanmichela/sshd/ConsoleShellFactory.java | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java b/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java index b554080..35a2ebc 100644 --- a/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java +++ b/src/main/java/com/ryanmichela/sshd/ConsoleShellFactory.java @@ -28,6 +28,7 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.InetAddress; +import java.net.UnknownHostException; import java.util.StringTokenizer; import java.util.Optional; import java.util.logging.Level; @@ -200,6 +201,7 @@ public class ConsoleShellFactory implements ShellFactory } catch (IOException e) { + e.printStackTrace(); SshdPlugin.instance.getLogger().log(Level.SEVERE, "Error processing command from SSH", e); } finally @@ -209,6 +211,20 @@ public class ConsoleShellFactory implements ShellFactory } } + private String GetHostname() + { + try + { + return InetAddress.getLocalHost().getHostName(); + } + catch (UnknownHostException e) + { + e.printStackTrace(); + SshdPlugin.instance.getLogger().log(Level.INFO, "The above stacktrace can be ignored, you likely have a misconfigured system hosts file."); + return "Unknown"; + } + } + private void printPreamble(ConsoleReader cr) throws IOException { File f = new File(SshdPlugin.instance.getDataFolder(), "motd.txt"); @@ -222,13 +238,14 @@ public class ConsoleShellFactory implements ShellFactory } catch (FileNotFoundException e) { + e.printStackTrace(); 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("Connected to: " + this.GetHostname() + " (" + Bukkit.getServer().getName() + ")\r"); cr.println(ConsoleLogFormatter.ColorizeString(Bukkit.getServer().getMotd()).replaceAll("\n", "\r\n")); cr.println("\r"); cr.println("Type 'exit' to exit the shell." + "\r");