]> git.argeo.org Git - gpl/argeo-slc.git/blob - cms/org.argeo.cms.integration/src/org/argeo/ssh/BasicSshServer.java
Improve merging
[gpl/argeo-slc.git] / cms / org.argeo.cms.integration / src / org / argeo / ssh / BasicSshServer.java
1 package org.argeo.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.server.SshServer;
8 import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider;
9 import org.apache.sshd.server.scp.ScpCommandFactory;
10 import org.apache.sshd.server.shell.ProcessShellFactory;
11 import org.argeo.util.OS;
12
13 /** A simple SSH server with some defaults. Supports SCP. */
14 @SuppressWarnings("restriction")
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 sshd.start();
41 } catch (Exception e) {
42 throw new RuntimeException("Cannot start SSH server on port " + port, e);
43 }
44 }
45
46 public void destroy() {
47 try {
48 sshd.stop();
49 } catch (IOException e) {
50 throw new RuntimeException("Cannot stop SSH server on port " + port, e);
51 }
52 }
53
54 public Integer getPort() {
55 return port;
56 }
57
58 public void setPort(Integer port) {
59 this.port = port;
60 }
61
62 public Path getHostKeyPath() {
63 return hostKeyPath;
64 }
65
66 public void setHostKeyPath(Path hostKeyPath) {
67 this.hostKeyPath = hostKeyPath;
68 }
69
70 public static void main(String[] args) {
71 int port = 2222;
72 Path hostKeyPath = Paths.get("hostkey.ser");
73 try {
74 if (args.length > 0)
75 port = Integer.parseInt(args[0]);
76 if (args.length > 1)
77 hostKeyPath = Paths.get(args[1]);
78 } catch (Exception e1) {
79 printUsage();
80 }
81
82 BasicSshServer sshServer = new BasicSshServer(port, hostKeyPath);
83 sshServer.init();
84 Runtime.getRuntime().addShutdownHook(new Thread("Shutdown SSH server") {
85
86 @Override
87 public void run() {
88 sshServer.destroy();
89 }
90 });
91 try {
92 synchronized (sshServer) {
93 sshServer.wait();
94 }
95 } catch (InterruptedException e) {
96 sshServer.destroy();
97 }
98
99 }
100
101 public static void printUsage() {
102 System.out.println("java " + BasicSshServer.class.getName() + " [port] [server key path]");
103 }
104
105 }