From 69d31175d413475e89a56f1032ac4e1c0caedfc7 Mon Sep 17 00:00:00 2001 From: Tony Date: Sun, 25 Mar 2018 13:44:41 -0600 Subject: [PATCH] Add SFTP support (#7) Add SFTP support --- src/main/java/com/ryanmichela/sshd/SshdPlugin.java | 14 ++++++++++++++ src/main/resources/config.yml | 4 ++++ 2 files changed, 18 insertions(+) diff --git a/src/main/java/com/ryanmichela/sshd/SshdPlugin.java b/src/main/java/com/ryanmichela/sshd/SshdPlugin.java index 2469d01..3340c5c 100644 --- a/src/main/java/com/ryanmichela/sshd/SshdPlugin.java +++ b/src/main/java/com/ryanmichela/sshd/SshdPlugin.java @@ -1,11 +1,15 @@ package com.ryanmichela.sshd; +import org.apache.sshd.common.file.virtualfs.VirtualFileSystemFactory; import org.apache.sshd.server.SshServer; import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; +import org.apache.sshd.server.subsystem.sftp.SftpSubsystemFactory; import org.bukkit.plugin.java.JavaPlugin; import java.io.File; import java.io.IOException; +import java.nio.file.FileSystems; +import java.util.Collections; import java.util.logging.Level; /** @@ -45,6 +49,16 @@ public class SshdPlugin extends JavaPlugin { sshd.setShellFactory(new ConsoleShellFactory()); sshd.setPasswordAuthenticator(new ConfigPasswordAuthenticator()); sshd.setPublickeyAuthenticator(new PublicKeyAuthenticator(authorizedKeys)); + + if (getConfig().getBoolean("enableSFTP")) { + sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); + sshd.setFileSystemFactory(new VirtualFileSystemFactory( + FileSystems.getDefault().getPath( + getDataFolder().getAbsolutePath() + ).getParent().getParent() + )); + } + sshd.setCommandFactory(new ConsoleCommandFactory()); try { sshd.start(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6d9b05c..5427555 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -7,6 +7,10 @@ port: 22 # Operational mode. Don't touch if you don't know what you're doing. Can be either DEFAULT or RPC mode: DEFAULT +# Enable built-in SFTP server or not. You'll be able to connect and upload/download files via SFTP protocol. +# Might be useful for testing purposes as well , i. e. docker containers. +enableSFTP: true + # By default, only public key authentication is enabled. This is the most secure mode. # To authorize a user to log in with public key authentication, install their public # PEM certificate in the authorized_users directory. Name the key file with user's user