2

我会使用 Apache SSHD 创建 SFTP 服务器并使用 SftpFileSystemProvider 挂载远程目录。

我按照文档https://github.com/apache/mina-sshd/blob/master/docs/sftp.md#using-sftpfilesystemprovider-to-create-an-sftpfilesystem成功使用 SftpFileSystemProvider 创建了虚拟文件系统。

但是,即使使用文档https://github.com/apache/mina-sshd/blob/master/docs/sftp.md#configuring-the-sftpfilesystemprovider,我在安装远程目录时也被卡住了。它保持安装根目录而不是目标目录。

我试过了:

  • 将目标目录添加到 sftp uri(不起作用)
  • 从路径获取新文件系统(不工作)

这是一个简单的例子。

object Main:
  
  class Events extends SftpEventListener

  class Auth extends PasswordAuthenticator {
    override def authenticate(username: String, password: String, session: ServerSession): Boolean = {
      true
    }
  }

  class FilesSystem extends VirtualFileSystemFactory {

    override def createFileSystem(session: SessionContext): FileSystem = {
      val uri = new URI("sftp://xxx:yyy@host/plop")
//      val uri = SftpFileSystemProvider.createFileSystemURI("host", 22, "xxx", "yyy")
      val fs = Try(FileSystems.newFileSystem(uri, Collections.emptyMap[String, Object](), new SftpFileSystemProvider().getClass().getClassLoader())) match {
        case Failure(exception) =>
          println("Failed to mount bucket")
          println(exception.getMessage)
          throw exception
        case Success(filesSystem) =>
          println("Bucket mounted")
          filesSystem
      }
      //fs.getPath("plop").getFileSystem
      fs
    }
  }

  private val fs = new FilesSystem()

  private val sftpSubSystem = new SftpSubsystemFactory.Builder().build()
  sftpSubSystem.addSftpEventListener(new Events())
  private val sshd: SshServer = SshServer.setUpDefaultServer()
  sshd.setPort(22)
  sshd.setHost("0.0.0.0")
  sshd.setSubsystemFactories(Collections.singletonList(sftpSubSystem))
  sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(Paths.get("hostkey.ser")))
  sshd.setShellFactory(new InteractiveProcessShellFactory())
  sshd.setCommandFactory(new ScpCommandFactory())
  sshd.setFileSystemFactory(fs)
  sshd.setPasswordAuthenticator(new Auth())
  sshd.setSessionHeartbeat(HeartbeatType.IGNORE, Duration.ofSeconds(30L))

  @main def m() = {
    sshd.start()
    while (sshd.isStarted) {

    }
  }

end Main

我错过了什么吗?

SSHD 版本 2.8.0、SFTP 协议版本 3、Scala3、Java11

4

0 回答 0