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; }
@Test public void testCustomUserInfoFalse() throws Exception { SshServer server = SshServer.setUpDefaultServer(); try { DefaultSftpSessionFactory f = createServerAndClient(server); UserInfo userInfo = mock(UserInfo.class); when(userInfo.promptYesNo(anyString())).thenReturn(false); f.setUserInfo(userInfo); expectReject(f); } finally { server.stop(true); } }
@Test public void testCustomUserInfoTrue() throws Exception { SshServer server = SshServer.setUpDefaultServer(); try { DefaultSftpSessionFactory f = createServerAndClient(server); UserInfo userInfo = mock(UserInfo.class); when(userInfo.promptYesNo(anyString())).thenReturn(true); f.setUserInfo(userInfo); f.getSession().close(); } finally { server.stop(true); } }
@Test public void testDefaultUserInfoFalse() throws Exception { SshServer server = SshServer.setUpDefaultServer(); try { DefaultSftpSessionFactory f = createServerAndClient(server); expectReject(f); } finally { server.stop(true); } }
@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(); }
@Test public void testConnectFailSocketOpen() throws Exception { SshServer server = SshServer.setUpDefaultServer(); try { server.setPasswordAuthenticator((arg0, arg1, arg2) -> true);
@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); } }
@Test public void testUcPw() 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.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))); server.start(); DefaultSftpSessionFactory f = new DefaultSftpSessionFactory(); f.setHost("localhost"); f.setPort(server.getPort()); f.setUser("user"); f.setPassword("pass"); f.setAllowUnknownKeys(true); Session<LsEntry> session = f.getSession(); doTest(server, session); } finally { server.stop(true); } }
private void testKeyExchange(String pubKey, String privKey, String passphrase) throws Exception { SshServer server = SshServer.setUpDefaultServer(); final PublicKey allowedKey = decodePublicKey(pubKey); try { server.setPublickeyAuthenticator((username, key, session) -> key.equals(allowedKey)); server.setPort(0); server.setKeyPairProvider(new SimpleGeneratorHostKeyProvider(new File("hostkey.ser").toPath())); 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))); server.start(); DefaultSftpSessionFactory f = new DefaultSftpSessionFactory(); f.setHost("localhost"); f.setPort(server.getPort()); f.setUser("user"); f.setAllowUnknownKeys(true); InputStream stream = new ClassPathResource(privKey).getInputStream(); f.setPrivateKey(new ByteArrayResource(FileCopyUtils.copyToByteArray(stream))); f.setPrivateKeyPassphrase(passphrase); Session<LsEntry> session = f.getSession(); doTest(server, session); } finally { server.stop(true); } }
@Override protected SshServer createServer() { return SshServer.setUpDefaultServer(); } }
@Override protected SshServer createServer() { return SshServer.setUpDefaultServer(); } }
protected SshServer initSshServer(int port) { final SshServer sshd = SshServer.setUpDefaultServer(); sshd.setPort(port); sshd.setPasswordAuthenticator(new PasswordAuthenticator() { @Override public boolean authenticate(String username, String password, ServerSession session) { return USERNAME.equals(username) && PASSWORD.equals(password); } }); sshd.setPublickeyAuthenticator(new KeySetPublickeyAuthenticator(this.keys)); return sshd; }
@Override protected SshServer createServer() { SshServer sshd = SshServer.setUpDefaultServer(); sshd.setIoServiceFactoryFactory(new NettyIoServiceFactoryFactory(eventLoopGroup)); return sshd; }
@Override protected SshServer createServer() { SshServer sshd = SshServer.setUpDefaultServer(); sshd.setIoServiceFactoryFactory(new NettyIoServiceFactoryFactory(eventLoopGroup)); return sshd; }
private SshServer buildServer(Shell props) { SshServer server = SshServer.setUpDefaultServer(); server.setHost(props.getHost()); server.setPort(props.getPort()); configureAuthenticationPolicies(server, props); configureServer(server); return server; }
public SftpServer(int port) { this.port = port; configureSecurityProvider(); SftpSubsystemFactory factory = createFtpSubsystemFactory(); sshdServer = SshServer.setUpDefaultServer(); configureSshdServer(factory); }
@Override protected SshServer createServer() { SshServer sshd = SshServer.setUpDefaultServer(); sshd.setIoServiceFactoryFactory(new NettyIoServiceFactoryFactory(context.nettyEventLoop(), new VertxIoHandlerBridge(context))); return sshd; }
public static SshServer setupTestServer(Class<?> anchor) { SshServer sshd = SshServer.setUpDefaultServer(); sshd.setKeyPairProvider(createTestHostKeyProvider(anchor)); sshd.setPasswordAuthenticator(BogusPasswordAuthenticator.INSTANCE); sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE); sshd.setShellFactory(EchoShellFactory.INSTANCE); sshd.setCommandFactory(UnknownCommandFactory.INSTANCE); return sshd; }
public static SshServer setupTestServer(Class<?> anchor) { SshServer sshd = SshServer.setUpDefaultServer(); sshd.setKeyPairProvider(createTestHostKeyProvider(anchor)); sshd.setPasswordAuthenticator(BogusPasswordAuthenticator.INSTANCE); sshd.setPublickeyAuthenticator(AcceptAllPublickeyAuthenticator.INSTANCE); sshd.setShellFactory(EchoShellFactory.INSTANCE); sshd.setCommandFactory(UnknownCommandFactory.INSTANCE); return sshd; }