Simple (S)FTP/TCP embedded server.
To add this product to your project, the first thing to is to add in the API.
maven:
<dependency>
<groupId>org.yggd.server</groupId>
<artifactId>embeddedserver</artifactId>
<version>0.1.0</version>
</dependency>
gradle:
repositories {
mavenCentral()
}
dependencies {
compile group: 'org.yggd.server', name: 'embeddedserver', version: '0.1.0'
}
- Setup and start server
final EmbeddedServer ftpServer = ServerBuilder.withFtp()
.port(10021)
.user(user -> {
user.setName("user1");
user.setPassword("password");
user.setHomeDirectory("home/directory");
})
.filesystem(filesystem -> filesystem.setCreateHome(true))
.build();
ftpServer.start();
For more information 'filesystem' above: https://mina.apache.org/sshd-project/apidocs/org/apache/sshd/common/file/nativefs/NativeFileSystemFactory.html
- Shutdown server
ftpServer.stop();
"SFTP" means SSH File Transfer Protocol (not FTP over SSL/TLS).
- generate SSH key
generate SSH public/private keyset.
example using OpenSSH below:
$ ssh-keygen -f ./id_rsa
Generating public/private rsa key pair.
Enter passphrase (empty for no passphrase): <-- not displayed, remember your passphrase
Enter same passphrase again:
Your identification has been saved in ./id_rsa.
Your public key has been saved in ./id_rsa.pub.
The key fingerprint is:
SHA256:n8MD/mBF1xPk73i6VumcewpaYK82aAFkkkRo1Jok+Mo username@yourhost.
The key's randomart image is:
+---[RSA 2048]----+
|...=o. .o |
|o + + o o . |
| = o + . . + |
| + . . . o |
|.. .S + o|
|.E ..* + +.|
| +o* + +.+|
| .ooo* ..*.|
| . .+. .++o|
+----[SHA256]-----+
$ ls
id_rsa id_rsa.pub
$
- Setup and start server
final EmbeddedServer sftpServer = ServerBuilder.withSftp()
.port(10022)
.directory(Paths.get("home/directory"))
.keyPairProvider(Paths.get("path-to-put/hostkey.ser")) // create ser file in the first running.
.publicKeyAuthenticate(Paths.get("ssh-keys/id_rsa.pub"),
(username, clientkey, session, serverkey) ->
"user1".equals(username) && serverkey.equals(clientkey)) // implement client authentication.
.build();
sftpServer.start();
Use "public" key in server.
- Stop server
sftpServer.stop();
You can also use Spring Resources instead of Path.
final EmbeddedServer sftpServer = ServerBuilder.withSftp()
.port(PORT)
.directory(home)
.keyPairProvider(new ClassPathResource("/classpath/hostkey.ser"))
.publicKeyAuthenticate(new ClassPathResource("/classpath/id_rsa.pub"),
(username, clientkey, session, serverkey) ->
USER.equals(username) && serverkey.equals(clientkey))
.build();
You can test this embedded servers, using FTP Client: FileZilla, WinSCP and so on. If you use those client in java code, refer test cases in this project.
- Setup and start server
final EmbeddedServer tcpServer = ServerBuilder.withTcp()
.port(PORT)
.active( () -> "response".getBytes())
.read( b -> {
try {
serverRead.put(b);
} catch (InterruptedException e) {
e.printStackTrace();
}
})
.build();
tcpServer.start();
- Stop server
sftpServer.stop();