public int getSSHPort(){ return sshd.getPort(); }
public void start() throws IOException { if (!started.getAndSet(true)) { server.start(); } }
@Override protected void configureServer(SshServer server) { server.setKeyExchangeFactories(Collections.singletonList(serverFactory)); } }
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(); }
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))); }
@Test public void testDefaultUserInfoFalse() throws Exception { SshServer server = SshServer.setUpDefaultServer(); try { DefaultSftpSessionFactory f = createServerAndClient(server); expectReject(f); } finally { server.stop(true); } }
@Before public void setPasswordAuthenticator() throws IOException { fixture.getServer().setUserAuthFactories(Collections.<NamedFactory<UserAuth>>singletonList(new NamedFactory<UserAuth>() { @Override public String getName() { fixture.getServer().setPasswordAuthenticator(new PasswordAuthenticator() { @Override public boolean authenticate(String username, String password, ServerSession session) { fixture.getServer().start();
@Override public void stop() { if (this.running) { try { server.stop(true); } catch (Exception e) { throw new IllegalStateException(e); } finally { this.running = false; } } }
@Override public void shutdown() { exited.set(true); callback.onExit(0); super.shutdown(); } };
public String getProperty(String key) { return env.getEnv().get(key); } }
@Before public void setUp() throws IOException { fixture.getServer().setForwardingFilter(new AcceptAllForwardingFilter()); File file = httpServer.getDocRoot().newFile("index.html"); FileUtil.writeToFile(file, "<html><head/><body><h1>Hi!</h1></body></html>"); }
@Test public void testDefaultUserInfoTrue() throws Exception { SshServer server = SshServer.setUpDefaultServer(); try { DefaultSftpSessionFactory f = createServerAndClient(server); f.setAllowUnknownKeys(true); f.getSession().close(); } finally { server.stop(true); } }
public void stopServer() { if (started.getAndSet(false)) { try { server.stop(true); } catch (IOException e) { throw new RuntimeException(e); } } }
public int getWidth() { String s = env.getEnv().get(Environment.ENV_COLUMNS); int width = -1; if (s != null) { try { width = Integer.parseInt(s); } catch (NumberFormatException e) { log.log(Level.WARNING, "Could not parse ssh term width " + s); } } return width; }
public void stopServer() throws IOException { if(sshd == null) return; sshd.stop(true); //Delete Virtual File System folder Path dir = Paths.get(getVirtualFileSystemPath()); FileUtils.deleteDirectory(dir.toFile()); } }
public int getHeight() { String s = env.getEnv().get(Environment.ENV_LINES); int height = -1; if (s != null) { try { height = Integer.parseInt(s); } catch (NumberFormatException e) { log.log(Level.WARNING, "Could not parse ssh term height " + s); } } return height; }
@AfterClass public static void stopServer() throws Exception { server.stop(); File hostKey = new File("hostkey.ser"); if (hostKey.exists()) { hostKey.delete(); } }