org.apache.sshd.server.session.ServerConnectionServiceFactory Java Examples

The following examples show how to use org.apache.sshd.server.session.ServerConnectionServiceFactory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AsyncAuthTestBase.java    From termd with Apache License 2.0 7 votes vote down vote up
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(new PasswordAuthenticator() {
    @Override
    public boolean authenticate(String username, String password, ServerSession session) throws PasswordChangeRequiredException {
      return authenticator.authenticate(username, password, session);
    }
  });
  server.setShellFactory(new EchoShellFactory());
  server.setServiceFactories(Arrays.asList(ServerConnectionServiceFactory.INSTANCE, AsyncUserAuthServiceFactory.INSTANCE));
  server.start();
}
 
Example #2
Source File: AsyncAuthTestBase.java    From aesh-readline with Apache License 2.0 5 votes vote down vote up
public void startServer(Integer timeout) throws Exception {
  if (server != null) {
    throw failure("Server already started");
  }
  server = SshServer.setUpDefaultServer();
  if (timeout != null) {
    server.setProperties(Collections.singletonMap(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();
}
 
Example #3
Source File: AsyncAuthTestBase.java    From termd with Apache License 2.0 5 votes vote down vote up
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();
}
 
Example #4
Source File: SSHServer.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
public SSHServer listen(Handler<AsyncResult<Void>> listenHandler) {
  if (!status.compareAndSet(STATUS_STOPPED, STATUS_STARTING)) {
    listenHandler.handle(Future.failedFuture("Invalid state:" + status.get()));
    return this;
  }
  if (options.getAuthOptions() != null) {
    authProvider = ShellAuth.load(vertx, options.getAuthOptions());
  }
  Charset defaultCharset = Charset.forName(options.getDefaultCharset());
  listenContext = (ContextInternal) vertx.getOrCreateContext();
  vertx.executeBlocking(fut -> {

    try {
      KeyCertOptions ksOptions = options.getKeyPairOptions();
      KeyStoreHelper ksHelper = KeyStoreHelper.create((VertxInternal) vertx, ksOptions);
      if (ksHelper == null) {
        throw new VertxException("No key pair store configured");
      }
      KeyStore ks = ksHelper.store();

      String kpPassword = "";
      if (ksOptions instanceof JksOptions) {
        kpPassword = ((JksOptions) ksOptions).getPassword();
      } else if (ksOptions instanceof PfxOptions) {
        kpPassword = ((PfxOptions) ksOptions).getPassword();
      }

      List<KeyPair> keyPairs = new ArrayList<>();
      for (Enumeration<String> it = ks.aliases(); it.hasMoreElements(); ) {
        String alias = it.nextElement();
        Key key = ks.getKey(alias, kpPassword.toCharArray());
        if (key instanceof PrivateKey) {
          Certificate cert = ks.getCertificate(alias);
          PublicKey publicKey = cert.getPublicKey();
          keyPairs.add(new KeyPair(publicKey, (PrivateKey) key));
        }
      }
      KeyPairProvider provider = new AbstractKeyPairProvider() {
        @Override
        public Iterable<KeyPair> loadKeys() {
          return keyPairs;
        }
      };

      Buffer inputrc = Helper.loadResource(vertx.fileSystem(), options.getIntputrc());
      if (inputrc == null) {
        throw new VertxException("Could not load inputrc from " + options.getIntputrc());
      }
      Keymap keymap = new Keymap(new ByteArrayInputStream(inputrc.getBytes()));
      TermConnectionHandler connectionHandler = new TermConnectionHandler(vertx, keymap, termHandler);

      nativeServer = SshServer.setUpDefaultServer();
      nativeServer.setShellFactory(() -> new TtyCommand(defaultCharset, connectionHandler::handle));
      Handler<SSHExec> execHandler = this.execHandler;
      if (execHandler != null) {
        nativeServer.setCommandFactory(command -> new TtyCommand(defaultCharset, conn -> {
          execHandler.handle(new SSHExec(command, conn));
        }));
      }
      nativeServer.setHost(options.getHost());
      nativeServer.setPort(options.getPort());
      nativeServer.setKeyPairProvider(provider);
      nativeServer.setIoServiceFactoryFactory(new NettyIoServiceFactoryFactory(listenContext.nettyEventLoop(), new VertxIoHandlerBridge(listenContext)));
      nativeServer.setServiceFactories(Arrays.asList(ServerConnectionServiceFactory.INSTANCE, AsyncUserAuthServiceFactory.INSTANCE));

      //
      if (authProvider == null) {
        throw new VertxException("No authenticator");
      }

      nativeServer.setPasswordAuthenticator((username, userpass, session) -> {
        AsyncAuth auth = new AsyncAuth();
        listenContext.runOnContext(v -> {
          authProvider.authenticate(new JsonObject().put("username", username).put("password", userpass), ar -> {
            auth.setAuthed(ar.succeeded());
          });
        });
        throw auth;
      });

      //
      nativeServer.start();
      status.set(STATUS_STARTED);
      fut.complete();
    } catch (Exception e) {
      status.set(STATUS_STOPPED);
      fut.fail(e);
    }
  }, listenHandler);
  return this;
}