]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/BasicSshServer.java
Merge tag 'v2.3.17' into testing
[lgpl/argeo-commons.git] / org.argeo.cms.lib.sshd / src / org / argeo / cms / ssh / BasicSshServer.java
1 package org.argeo.cms.ssh;
2
3 import java.io.IOException;
4 import java.nio.file.Path;
5 import java.nio.file.Paths;
6
7 import org.apache.sshd.scp.server.ScpCommandFactory;
8 import org.apache.sshd.server.SshServer;
9 import org.apache.sshd.server.config.keys.DefaultAuthorizedKeysAuthenticator;
10 import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
11 import org.apache.sshd.server.shell.ProcessShellFactory;
12 import org.argeo.cms.util.OS;
13
14 /** A simple SSH server with some defaults. Supports SCP. */
15 public class BasicSshServer {
16 private Integer port;
17 private Path hostKeyPath;
18
19 private SshServer sshd = null;
20
21 public BasicSshServer(Integer port, Path hostKeyPath) {
22 this.port = port;
23 this.hostKeyPath = hostKeyPath;
24 }
25
26 public void init() {
27 try {
28 sshd = SshServer.setUpDefaultServer();
29 sshd.setPort(port);
30 if (hostKeyPath == null)
31 throw new IllegalStateException("An SSH server key must be set");
32 sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(hostKeyPath));
33 // sshd.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i",
34 // "-l" }));
35 String[] shellCommand = OS.LOCAL.getDefaultShellCommand();
36 // FIXME transfer args
37 // sshd.setShellFactory(new ProcessShellFactory(shellCommand));
38 sshd.setShellFactory(new ProcessShellFactory(shellCommand[0], shellCommand));
39 sshd.setCommandFactory(new ScpCommandFactory());
40
41 sshd.setPublickeyAuthenticator(new DefaultAuthorizedKeysAuthenticator(true));
42 sshd.start();
43 } catch (Exception e) {
44 throw new RuntimeException("Cannot start SSH server on port " + port, e);
45 }
46 }
47
48 public void destroy() {
49 try {
50 sshd.stop();
51 } catch (IOException e) {
52 throw new RuntimeException("Cannot stop SSH server on port " + port, e);
53 }
54 }
55
56 public Integer getPort() {
57 return port;
58 }
59
60 public void setPort(Integer port) {
61 this.port = port;
62 }
63
64 public Path getHostKeyPath() {
65 return hostKeyPath;
66 }
67
68 public void setHostKeyPath(Path hostKeyPath) {
69 this.hostKeyPath = hostKeyPath;
70 }
71
72 public static void main(String[] args) {
73 int port = 2222;
74 Path hostKeyPath = Paths.get("hostkey.ser");
75 try {
76 if (args.length > 0)
77 port = Integer.parseInt(args[0]);
78 if (args.length > 1)
79 hostKeyPath = Paths.get(args[1]);
80 } catch (Exception e1) {
81 printUsage();
82 }
83
84 BasicSshServer sshServer = new BasicSshServer(port, hostKeyPath);
85 sshServer.init();
86 Runtime.getRuntime().addShutdownHook(new Thread("Shutdown SSH server") {
87
88 @Override
89 public void run() {
90 sshServer.destroy();
91 }
92 });
93 try {
94 synchronized (sshServer) {
95 sshServer.wait();
96 }
97 } catch (InterruptedException e) {
98 sshServer.destroy();
99 }
100
101 }
102
103 public static void printUsage() {
104 System.out.println("java " + BasicSshServer.class.getName() + " [port] [server key path]");
105 }
106
107 }