public void startServer() throws IOException { sshd = SshServer.setUpDefaultServer(); sshd.setHost("localhost"); sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider()); //Accept all keys for authentication sshd.setPublickeyAuthenticator((s, publicKey, serverSession) -> true); //Allow username/password authentication using pre-defined credentials sshd.setPasswordAuthenticator((username, password, serverSession) -> this.username.equals(username) && this.password.equals(password)); //Setup Virtual File System (VFS) //Ensure VFS folder exists Path dir = Paths.get(getVirtualFileSystemPath()); Files.createDirectories(dir); sshd.setFileSystemFactory(new VirtualFileSystemFactory(dir.toAbsolutePath())); //Add SFTP support List<NamedFactory<Command>> sftpCommandFactory = new ArrayList<>(); sftpCommandFactory.add(new SftpSubsystemFactory()); sshd.setSubsystemFactories(sftpCommandFactory); sshd.start(); }
@Override public void stop() { if (this.running) { try { server.stop(true); } catch (Exception e) { throw new IllegalStateException(e); } finally { this.running = false; } } }
private SshServer defaultSshServer() { SshServer sshServer = SshServer.setUpDefaultServer(); sshServer.setPort(randomPort()); ClassLoadableResourceKeyPairProvider fileKeyPairProvider = new ClassLoadableResourceKeyPairProvider(hostkey); sshServer.setKeyPairProvider(fileKeyPairProvider); sshServer.setPasswordAuthenticator(new PasswordAuthenticator() { @Override public boolean authenticate(String username, String password, ServerSession session) { return username.equals(password); } }); sshServer.setGSSAuthenticator(new BogusGSSAuthenticator()); sshServer.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory())); ScpCommandFactory commandFactory = new ScpCommandFactory(); commandFactory.setDelegateCommandFactory(new CommandFactory() { @Override public Command createCommand(String command) { return new ProcessShellFactory(command.split(" ")).create(); } }); sshServer.setCommandFactory(commandFactory); sshServer.setShellFactory(new ProcessShellFactory("ls")); return sshServer; }
@Override public void afterPropertiesSet() throws Exception { final PublicKey allowedKey = decodePublicKey(); this.server.setPublickeyAuthenticator((username, key, session) -> key.equals(allowedKey)); this.server.setPort(this.port); this.server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser"))); this.server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); final String pathname = System.getProperty("java.io.tmpdir") + File.separator + "sftptest" + File.separator; new File(pathname).mkdirs(); server.setFileSystemFactory(new VirtualFileSystemFactory(Paths.get(pathname))); }
public EmbeddedSSHServer(Path homeDir, int port) { this.sshServer = SshServer.setUpDefaultServer(); this.sshServer.setPort(port); this.sshServer.setCommandFactory(new EchoCommandFactory()); this.sshServer.setKeyPairProvider(new SimpleGeneratorHostKeyProvider()); this.sshServer.setPasswordAuthenticator((username, password, serverSession) -> username.equals("admin") && password.equals("admin")); this.sshServer.setPublickeyAuthenticator((s, publicKey, serverSession) -> true); this.homeDir = homeDir; if (EnvironmentUtils.isWindows()) { sshServer.setShellFactory(new ProcessShellFactory(new String[] { "cmd.exe " })); } else { sshServer.setShellFactory(new ProcessShellFactory(new String[] { "/bin/sh", "-i", "-s" })); } }
@BeforeClass public static void createServer() throws Exception { server = SshServer.setUpDefaultServer(); server.setPasswordAuthenticator((username, password, session) -> true); server.setPort(0); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); server.setFileSystemFactory(new VirtualFileSystemFactory(remoteTemporaryFolder.getRoot().toPath())); server.start(); port = server.getPort(); }
public void start(Consumer<TtyConnection> factory, Consumer<Throwable> doneHandler) { server = SshServer.setUpDefaultServer(); server.setIoServiceFactoryFactory(new NettyIoServiceFactoryFactory(childGroup)); server.setPort(port); server.setHost(host); server.setKeyPairProvider(keyPairProvider); server.setPasswordAuthenticator(passwordAuthenticator); server.setShellFactory(() -> new TtyCommand(charset, factory)); try { server.start(); } catch (Exception e) { doneHandler.accept(e); return; } doneHandler.accept(null); }
public void startServer(Integer timeout) throws Exception { if (server != null) { throw failure("Server already started"); } server = SshServer.setUpDefaultServer(); if (timeout != null) { server.getProperties().put(FactoryManager.AUTH_TIMEOUT, timeout.toString()); } server.setPort(5000); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); server.setPasswordAuthenticator((username, password, sess) -> authenticator.authenticate(username, password, sess)); server.setShellFactory(new EchoShellFactory()); server.setServiceFactories(Arrays.asList(ServerConnectionServiceFactory.INSTANCE, AsyncUserAuthServiceFactory.INSTANCE)); server.start(); }
private void start() throws IOException { server = serverBuilder.get(); server.setPort(port); server.setHost(ip); server.setShellFactory(new ShellFactoryImpl(shell)); server.setCommandFactory(new ScpCommandFactory.Builder() .withDelegate(command -> new ShellCommand(execute, command)).build()); server.setSubsystemFactories(Collections.singletonList( new SftpSubsystemFactory.Builder().build() )); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider()); server.start(); }
@Override protected void before() throws Throwable { sshd = SshServer.setUpDefaultServer(); sshd.setPort(findRandomOpenPortOnAllLocalInterfaces()); sshd.setKeyPairProvider( new ClassLoadableResourceKeyPairProvider(getClass().getClassLoader(), "server_key")); sshd.setCommandFactory(new ScpCommandFactory()); sshd.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); sshd.setUserAuthFactories(Collections.singletonList(new UserAuthPublicKeyFactory())); sshd.setHost("localhost"); sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE); sshd.setHostBasedAuthenticator(new StaticHostBasedAuthenticator(true)); VirtualFileSystemFactory fileSystemFactory = new VirtualFileSystemFactory(); fileSystemFactory.setDefaultHomeDir(currentFolder.get().toPath()); sshd.setFileSystemFactory(fileSystemFactory); sshd.start(); }
@Test public void testConnectFailSocketOpen() throws Exception { SshServer server = SshServer.setUpDefaultServer(); try { server.setPasswordAuthenticator((arg0, arg1, arg2) -> true); server.setPort(0); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); server.start(); f.setPort(server.getPort()); f.setUser("user"); f.setPassword("pass"); while (n++ < 100 && server.getActiveSessions().size() > 0) { Thread.sleep(100); assertEquals(0, server.getActiveSessions().size()); server.stop(true);
@Override protected void startService() throws Exception { String username=cfg.get("auth-username","admin"); String authorizedKeysFilename=cfg.get("authorized-keys-file","cfg/authorized_keys"); String hostKeys=cfg.get("hostkeys-file","cfg/hostkeys.ser"); int port=cfg.getInt("port",2222); checkAuthorizedKeys(authorizedKeysFilename); String[] prefixes= getPrefixes(); sshd = SshServer.setUpDefaultServer(); sshd.setPort(port); sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File(hostKeys))); CliShellFactory csf = new CliShellFactory(getServer(), prefixes); sshd.setShellFactory(csf); sshd.setCommandFactory(csf); sshd.setUserAuthFactories(Collections.singletonList(new UserAuthPublicKeyFactory())); sshd.setPublickeyAuthenticator(new AuthorizedKeysFileBasedPKA(username, authorizedKeysFilename)); sshd.start(); log.info("Started SSHD @ port "+port); }
private DefaultSftpSessionFactory createServerAndClient(SshServer server) throws IOException { server.setPublickeyAuthenticator((username, key, session) -> true); server.setPort(0); server.setSubsystemFactories(Collections.singletonList(new SftpSubsystemFactory())); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); server.start(); DefaultSftpSessionFactory f = new DefaultSftpSessionFactory(); f.setHost("localhost"); f.setPort(server.getPort()); f.setUser("user"); Resource privateKey = new ClassPathResource("id_rsa"); f.setPrivateKey(privateKey); return f; }
@Override public void afterPropertiesSet() throws Exception { this.sftpFolder.create(); this.localFolder.create(); this.server.setPasswordAuthenticator((username, password, session) -> true); this.server.setPort(this.port); this.server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser"))); this.server.setSubsystemFactories(Arrays.<NamedFactory<Command>>asList(new SftpSubsystemFactory())); this.server.setFileSystemFactory(new VirtualFileSystemFactory(sftpRootFolder.toPath())); this.server.start(); }
@Override protected void server(Consumer<TtyConnection> onConnect) { if (sshd != null) { throw failure("Already a server"); } try { sshd = createServer(); sshd.setPort(5000); sshd.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); sshd.setPasswordAuthenticator((username, password, session) -> true); sshd.setShellFactory(() -> createConnection(onConnect)); sshd.start(); } catch (Exception e) { throw failure(e); } }
private static SshServer createServer(int port, String repository) { SshServer s = SshServer.setUpDefaultServer(); s.setPort(port); s.setKeyPairProvider(new SimpleGeneratorHostKeyProvider()); s.setPublickeyAuthenticator((username, key, session) -> true); s.setCommandFactory(new GitPackCommandFactory(repository)); return s; } }
@Before public void setUp() throws Exception { sshd = setupTestServer(); PropertyResolverUtils.updateProperty(sshd, ServerFactoryManager.AUTH_METHODS, UserAuthPublicKeyFactory.NAME); sshd.setPublickeyAuthenticator(new PublickeyAuthenticator() { @SuppressWarnings("synthetic-access") @Override public boolean authenticate(String username, PublicKey key, ServerSession session) { return delegate.authenticate(username, key, session); } }); sshd.start(); port = sshd.getPort(); }
@Before public void setUp() throws Exception { sshd = setupTestServer(); PropertyResolverUtils.updateProperty(sshd, FactoryManager.IDLE_TIMEOUT, TIMEOUT); sshd.setShellFactory(new TestEchoShellFactory()); sshd.start(); port = sshd.getPort(); }