io.termd.core.readline.Keymap Java Examples

The following examples show how to use io.termd.core.readline.Keymap. 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: TermImpl.java    From arthas with Apache License 2.0 6 votes vote down vote up
public TermImpl(Keymap keymap, TtyConnection conn) {
    this.conn = conn;
    readline = new Readline(keymap);
    readline.setHistory(FileUtils.loadCommandHistory(new File(Constants.CMD_HISTORY_FILE)));
    for (Function function : readlineFunctions) {
        readline.addFunction(function);
    }

    echoHandler = new DefaultTermStdinHandler(this);
    conn.setStdinHandler(echoHandler);
    conn.setEventHandler(new EventHandler(this));
}
 
Example #2
Source File: ReadlineFunctionExample.java    From termd with Apache License 2.0 5 votes vote down vote up
public static void handle(TtyConnection conn) {

    // The reverse function simply reverse the edit buffer
    Function reverseFunction = new ReverseFunction();

    ReadlineExample.readline(
        // Bind reverse to Ctrl-g to the reverse function
        new Readline(Keymap.getDefault().bindFunction("\\C-g", "reverse")).
            addFunctions(Functions.loadDefaults()).addFunction(reverseFunction),
        conn);
  }
 
Example #3
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
public void accept(final TtyConnection conn) {
  InputStream inputrc = Keymap.class.getResourceAsStream("inputrc");
  Keymap keymap = new Keymap(inputrc);
  Readline readline = new Readline(keymap);
  for (io.termd.core.readline.Function function : Helper.loadServices(Thread.currentThread().getContextClassLoader(), io.termd.core.readline.Function.class)) {
    readline.addFunction(function);
  }
  conn.write("Welcome to Term.d shell example\n\n");
  read(conn, readline);
}
 
Example #4
Source File: TermImpl.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
public TermImpl(Vertx vertx, Keymap keymap, TtyConnection conn) {
  this.vertx = vertx;
  this.conn = conn;
  readline = new Readline(keymap);
  readlineFunctions.forEach(readline::addFunction);
  echoHandler = codePoints -> {
    // Echo
    echo(codePoints);
    readline.queueEvent(codePoints);
  };
  conn.setStdinHandler(echoHandler);
  conn.setEventHandler((event, key) -> {
    switch (event) {
      case INTR:
        if (interruptHandler == null || !interruptHandler.deliver(key)) {
          echo(key, '\n');
        }
        break;
      case EOF:
        // Pseudo signal
        if (stdinHandler != null) {
          stdinHandler.handle(Helper.fromCodePoints(new int[]{key}));
        } else {
          echo(key);
          readline.queueEvent(new int[]{key});
        }
        break;
      case SUSP:
        if (suspendHandler == null || !suspendHandler.deliver(key)) {
          echo(key, 'Z' - 64);
        }
        break;
    }
  });
}
 
Example #5
Source File: TelnetTermServer.java    From vertx-shell with Apache License 2.0 5 votes vote down vote up
@Override
public TermServer listen(Handler<AsyncResult<Void>> listenHandler) {
  Charset charset = Charset.forName(options.getCharset());
  if (server == null) {
    server = vertx.createNetServer(options);
    Buffer inputrc = Helper.loadResource(vertx.fileSystem(), options.getIntputrc());
    if (inputrc == null) {
      listenHandler.handle(Future.failedFuture("Could not load inputrc from " + options.getIntputrc()));
      return this;
    }
    Keymap keymap = new Keymap(new ByteArrayInputStream(inputrc.getBytes()));
    TermConnectionHandler connectionHandler = new TermConnectionHandler(vertx, keymap, termHandler);
    server.connectHandler(new TelnetSocketHandler(vertx, () -> {
      return new TelnetTtyConnection(options.getInBinary(), options.getOutBinary(), charset, connectionHandler::handle);
    }));
    server.listen(ar -> {
      if (ar.succeeded()) {
        listenHandler.handle(Future.succeededFuture());
      } else {
        listenHandler.handle(Future.failedFuture(ar.cause()));
      }
    });
  } else {
    listenHandler.handle(Future.failedFuture("Already started"));
  }
  return this;
}
 
Example #6
Source File: ReadlineFunctionExample.java    From termd with Apache License 2.0 5 votes vote down vote up
public static void handle(TtyConnection conn) {

    // The reverse function simply reverse the edit buffer
    Function reverseFunction = new ReverseFunction();

    ReadlineExample.readline(
        // Bind reverse to Ctrl-g to the reverse function
        new Readline(Keymap.getDefault().bindFunction("\\C-g", "reverse")).
            addFunctions(Function.loadDefaults()).addFunction(reverseFunction),
        conn);
  }
 
Example #7
Source File: Shell.java    From termd with Apache License 2.0 5 votes vote down vote up
public void accept(final TtyConnection conn) {
  InputStream inputrc = Keymap.class.getResourceAsStream("inputrc");
  Keymap keymap = new Keymap(inputrc);
  Readline readline = new Readline(keymap);
  for (io.termd.core.readline.Function function : Helper.loadServices(Thread.currentThread().getContextClassLoader(), io.termd.core.readline.Function.class)) {
    readline.addFunction(function);
  }
  conn.write("Welcome to Term.d shell example\n\n");
  read(conn, readline);
}
 
Example #8
Source File: Helper.java    From bistoury with GNU General Public License v3.0 4 votes vote down vote up
public static Keymap loadKeymap() {
    return new Keymap(loadInputRcFile());
}
 
Example #9
Source File: ReadlineExample.java    From termd with Apache License 2.0 4 votes vote down vote up
public static void handle(TtyConnection conn) {
  readline(
      new Readline(Keymap.getDefault()).addFunctions(Functions.loadDefaults()),
      conn);
}
 
Example #10
Source File: Helper.java    From arthas with Apache License 2.0 4 votes vote down vote up
public static Keymap loadKeymap() {
    return new Keymap(loadInputRcFile());
}
 
Example #11
Source File: HttpTermServer.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
@Override
public TermServer listen(Handler<AsyncResult<Void>> listenHandler) {

  Charset charset = Charset.forName(options.getCharset());

  boolean createServer = false;
  if (router == null) {
    createServer = true;
    router = Router.router(vertx);
  }

  if (options.getAuthOptions() != null) {
    authProvider = ShellAuth.load(vertx, options.getAuthOptions());
  }

  if (options.getSockJSPath() != null && options.getSockJSHandlerOptions() != null) {
    if (authProvider != null) {
      AuthenticationHandler basicAuthHandler = BasicAuthHandler.create(authProvider);
      router.route(options.getSockJSPath()).handler(basicAuthHandler);
    }

    Buffer inputrc = Helper.loadResource(vertx.fileSystem(), options.getIntputrc());
    if (inputrc == null) {
      if (listenHandler != null) {
        listenHandler.handle(Future.failedFuture("Could not load inputrc from " + options.getIntputrc()));
      }
      return this;
    }
    Keymap keymap = new Keymap(new ByteArrayInputStream(inputrc.getBytes()));
    SockJSHandler sockJSHandler = SockJSHandler.create(vertx, options.getSockJSHandlerOptions());
    sockJSHandler.socketHandler(new SockJSTermHandlerImpl(vertx, charset, keymap).termHandler(termHandler));
    router.route(options.getSockJSPath()).handler(sockJSHandler);
  }

  if (options.getVertsShellJsResource() != null) {
    router.get("/vertxshell.js").handler(ctx -> ctx.response().putHeader("Content-Type", "application/javascript").end(options.getVertsShellJsResource()));
  }
  if (options.getTermJsResource() != null) {
    router.get("/term.js").handler(ctx -> ctx.response().putHeader("Content-Type", "application/javascript").end(options.getTermJsResource()));
  }
  if (options.getShellHtmlResource() != null) {
    router.get("/shell.html").handler(ctx -> ctx.response().putHeader("Content-Type", "text/html").end(options.getShellHtmlResource()));
  }

  if (createServer) {
    server = vertx.createHttpServer(options);
    server.requestHandler(router);
    server.listen(ar -> {
      if (listenHandler != null) {
        if (ar.succeeded()) {
          listenHandler.handle(Future.succeededFuture());
        } else {
          listenHandler.handle(Future.failedFuture(ar.cause()));
        }
      }
    });
  } else {
    if (listenHandler != null) {
      listenHandler.handle(Future.succeededFuture());
    }
  }
  return this;
}
 
Example #12
Source File: TermConnectionHandler.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
public TermConnectionHandler(Vertx vertx, Keymap keymap, Handler<Term> handler) {
  this.vertx = vertx;
  this.handler = handler;
  this.keymap = keymap;
}
 
Example #13
Source File: Helper.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
public static Keymap defaultKeymap() {
  Buffer buffer = Helper.loadResource(TelnetTermOptions.DEFAULT_INPUTRC);
  return new Keymap(new ByteArrayInputStream(buffer.getBytes()));
}
 
Example #14
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;
}
 
Example #15
Source File: SockJSTermHandlerImpl.java    From vertx-shell with Apache License 2.0 4 votes vote down vote up
public SockJSTermHandlerImpl(Vertx vertx, Charset charset, Keymap keymap) {
  this.charset = charset;
  this.vertx = vertx;
  this.keymap = keymap;
}
 
Example #16
Source File: ReadlineExample.java    From termd with Apache License 2.0 4 votes vote down vote up
public static void handle(TtyConnection conn) {
  readline(
      new Readline(Keymap.getDefault()).addFunctions(Function.loadDefaults()),
      conn);
}