Fixed a bug where sometimes sessions are disconnected due to some hosts not resolving localhost properly

This commit is contained in:
Justin Crawford 2020-04-06 06:06:47 -07:00
parent e0080fb1a0
commit d348e26b49
No known key found for this signature in database
GPG Key ID: 0D84DEDBB8EF259C
1 changed files with 18 additions and 1 deletions

View File

@ -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");