2013-11-14 07:17:51 +00:00
|
|
|
package com.ryanmichela.sshd;
|
|
|
|
|
2018-03-25 19:44:41 +00:00
|
|
|
import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory;
|
2017-11-13 01:02:19 +00:00
|
|
|
import org.apache.sshd.server.SshServer;
|
2013-11-14 07:17:51 +00:00
|
|
|
import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
|
2018-03-25 19:44:41 +00:00
|
|
|
import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory;
|
2013-11-14 07:17:51 +00:00
|
|
|
import org.bukkit.plugin.java.JavaPlugin;
|
|
|
|
|
2019-10-03 02:14:56 +00:00
|
|
|
import com.ryanmichela.sshd.ConsoleShellFactory;
|
2019-10-06 07:08:34 +00:00
|
|
|
import com.ryanmichela.sshd.MkpasswdCommand;
|
2019-10-03 02:14:56 +00:00
|
|
|
|
2013-11-14 07:17:51 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2019-10-05 06:18:59 +00:00
|
|
|
import java.io.InputStream;
|
2018-03-25 19:44:41 +00:00
|
|
|
import java.nio.file.FileSystems;
|
2019-10-05 06:18:59 +00:00
|
|
|
import java.nio.file.Files;
|
2018-03-25 19:44:41 +00:00
|
|
|
import java.util.Collections;
|
2013-11-14 07:17:51 +00:00
|
|
|
import java.util.logging.Level;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Copyright 2013 Ryan Michela
|
|
|
|
*/
|
2019-10-03 02:14:56 +00:00
|
|
|
public
|
|
|
|
class SshdPlugin extends JavaPlugin
|
|
|
|
{
|
2018-05-06 16:42:57 +00:00
|
|
|
|
2019-10-03 02:14:56 +00:00
|
|
|
private SshServer sshd;
|
|
|
|
public static SshdPlugin instance;
|
2013-11-14 07:17:51 +00:00
|
|
|
|
2019-10-03 02:14:56 +00:00
|
|
|
@Override public void onLoad()
|
|
|
|
{
|
|
|
|
saveDefaultConfig();
|
|
|
|
File authorizedKeys = new File(getDataFolder(), "authorized_keys");
|
|
|
|
if (!authorizedKeys.exists())
|
|
|
|
authorizedKeys.mkdirs();
|
2019-10-05 06:18:59 +00:00
|
|
|
|
|
|
|
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();
|
2019-10-03 02:14:56 +00:00
|
|
|
}
|
2013-11-14 07:17:51 +00:00
|
|
|
|
2019-10-03 02:14:56 +00:00
|
|
|
// Don't go any lower than INFO or SSHD will cause a stack overflow exception.
|
|
|
|
// SSHD will log that it wrote bites to the output stream, which writes
|
|
|
|
// bytes to the output stream - ad nauseaum.
|
|
|
|
getLogger().setLevel(Level.INFO);
|
|
|
|
}
|
2013-11-14 07:17:51 +00:00
|
|
|
|
2019-10-03 02:14:56 +00:00
|
|
|
@Override public void onEnable()
|
|
|
|
{
|
|
|
|
instance = this;
|
2013-11-14 07:17:51 +00:00
|
|
|
|
2019-10-03 02:14:56 +00:00
|
|
|
sshd = SshServer.setUpDefaultServer();
|
2019-10-04 04:07:00 +00:00
|
|
|
sshd.setPort(getConfig().getInt("Port", 1025));
|
|
|
|
String host = getConfig().getString("ListenAddress", "all");
|
2019-10-03 02:14:56 +00:00
|
|
|
sshd.setHost(host.equals("all") ? null : host);
|
2013-11-14 07:17:51 +00:00
|
|
|
|
2019-10-03 02:14:56 +00:00
|
|
|
File hostKey = new File(getDataFolder(), "hostkey");
|
|
|
|
File authorizedKeys = new File(getDataFolder(), "authorized_keys");
|
2013-11-14 07:17:51 +00:00
|
|
|
|
2019-10-03 02:14:56 +00:00
|
|
|
sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(hostKey.toPath()));
|
|
|
|
sshd.setShellFactory(new ConsoleShellFactory());
|
|
|
|
sshd.setPasswordAuthenticator(new ConfigPasswordAuthenticator());
|
|
|
|
sshd.setPublickeyAuthenticator(new PublicKeyAuthenticator(authorizedKeys));
|
2018-03-25 19:44:41 +00:00
|
|
|
|
2019-10-04 04:07:00 +00:00
|
|
|
if (getConfig().getBoolean("EnableSFTP"))
|
2019-10-03 02:14:56 +00:00
|
|
|
{
|
|
|
|
sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory()));
|
|
|
|
sshd.setFileSystemFactory(
|
|
|
|
new VirtualFileSystemFactory(FileSystems.getDefault().getPath(getDataFolder().getAbsolutePath()).getParent().getParent()));
|
|
|
|
}
|
2018-03-25 19:44:41 +00:00
|
|
|
|
2019-10-06 07:08:34 +00:00
|
|
|
this.getCommand("mkpasswd").setExecutor(new MkpasswdCommand());
|
|
|
|
|
2019-10-03 02:14:56 +00:00
|
|
|
sshd.setCommandFactory(new ConsoleCommandFactory());
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sshd.start();
|
|
|
|
}
|
|
|
|
catch (IOException e)
|
|
|
|
{
|
|
|
|
getLogger().log(Level.SEVERE, "Failed to start SSH server! ", e);
|
|
|
|
}
|
|
|
|
}
|
2013-11-14 07:17:51 +00:00
|
|
|
|
2019-10-03 02:14:56 +00:00
|
|
|
@Override public void onDisable()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
sshd.stop();
|
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
}
|
2013-11-14 07:17:51 +00:00
|
|
|
}
|