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