X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms.lib.sshd%2Fsrc%2Forg%2Fargeo%2Fcms%2Fssh%2FBasicSshServer.java;fp=org.argeo.cms.lib.sshd%2Fsrc%2Forg%2Fargeo%2Fcms%2Fssh%2FBasicSshServer.java;h=9e9389342903deeeb3d68baa2da83a4d5535b6d8;hb=4e548693acc16f97b74eaaa95d6841054a172b85;hp=0000000000000000000000000000000000000000;hpb=7b242851c0094d13cbaca5b68261ad92c873a59f;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/BasicSshServer.java b/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/BasicSshServer.java new file mode 100644 index 000000000..9e9389342 --- /dev/null +++ b/org.argeo.cms.lib.sshd/src/org/argeo/cms/ssh/BasicSshServer.java @@ -0,0 +1,108 @@ +package org.argeo.cms.ssh; + +import java.io.IOException; +import java.nio.file.Path; +import java.nio.file.Paths; + +import org.apache.sshd.scp.server.ScpCommandFactory; +import org.apache.sshd.server.SshServer; +import org.apache.sshd.server.config.keys.DefaultAuthorizedKeysAuthenticator; +import org.apache.sshd.server.keyprovider.SimpleGeneratorHostKeyProvider; +import org.apache.sshd.server.shell.ProcessShellFactory; +import org.argeo.util.OS; + +/** A simple SSH server with some defaults. Supports SCP. */ +@SuppressWarnings("restriction") +public class BasicSshServer { + private Integer port; + private Path hostKeyPath; + + private SshServer sshd = null; + + public BasicSshServer(Integer port, Path hostKeyPath) { + this.port = port; + this.hostKeyPath = hostKeyPath; + } + + public void init() { + try { + sshd = SshServer.setUpDefaultServer(); + sshd.setPort(port); + if (hostKeyPath == null) + throw new IllegalStateException("An SSH server key must be set"); + sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(hostKeyPath)); + // sshd.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i", + // "-l" })); + String[] shellCommand = OS.LOCAL.getDefaultShellCommand(); + // FIXME transfer args +// sshd.setShellFactory(new ProcessShellFactory(shellCommand)); + sshd.setShellFactory(new ProcessShellFactory(shellCommand[0], shellCommand)); + sshd.setCommandFactory(new ScpCommandFactory()); + + sshd.setPublickeyAuthenticator(new DefaultAuthorizedKeysAuthenticator(true)); + sshd.start(); + } catch (Exception e) { + throw new RuntimeException("Cannot start SSH server on port " + port, e); + } + } + + public void destroy() { + try { + sshd.stop(); + } catch (IOException e) { + throw new RuntimeException("Cannot stop SSH server on port " + port, e); + } + } + + public Integer getPort() { + return port; + } + + public void setPort(Integer port) { + this.port = port; + } + + public Path getHostKeyPath() { + return hostKeyPath; + } + + public void setHostKeyPath(Path hostKeyPath) { + this.hostKeyPath = hostKeyPath; + } + + public static void main(String[] args) { + int port = 2222; + Path hostKeyPath = Paths.get("hostkey.ser"); + try { + if (args.length > 0) + port = Integer.parseInt(args[0]); + if (args.length > 1) + hostKeyPath = Paths.get(args[1]); + } catch (Exception e1) { + printUsage(); + } + + BasicSshServer sshServer = new BasicSshServer(port, hostKeyPath); + sshServer.init(); + Runtime.getRuntime().addShutdownHook(new Thread("Shutdown SSH server") { + + @Override + public void run() { + sshServer.destroy(); + } + }); + try { + synchronized (sshServer) { + sshServer.wait(); + } + } catch (InterruptedException e) { + sshServer.destroy(); + } + + } + + public static void printUsage() { + System.out.println("java " + BasicSshServer.class.getName() + " [port] [server key path]"); + } + +}