/xterm-addon-ssh

Ssh addon for xterm.js

Primary LanguageTypeScriptMIT LicenseMIT

version downloads license vulnerabilities

xterm-addon-ssh

Overview

This library sends to the socket server for communication with SSH.

If you want to communicate to SSH directly, use xterm-addon-attach

Install

npm install --save xterm-addon-ssh

Usage

  • Javascript
import { Terminal } from 'xterm';
import { SshAddon } from 'xterm-addon-ssh';
import * as SockJS from 'sockjs-client';

const sockjs = new SockJS('https://127.0.0.1:8080');

const terminal = new Terminal();
const sshAddon = new SshAddon(sockjs, {
  serverUuid: '123e4567-e89b-12d3-a456-426614174000',
  header: {
    Authorization:
      'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
  },
  connectImmediately: true,
});

terminal.loadAddon(sshAddon);
  • React with Typescript
import * as React from 'react';
import { QPXterm } from 'qp-xtermjs';
import { SshAddon, TerminalKeyEvent } from 'xterm-addon-ssh';
import * as SockJS from 'sockjs-client';

const sockjs = new SockJS('https://127.0.0.1:8080');

const Term: React.FC = () => {
  const terminalRef = useRef<QPXterm | null>();
  const sshAddon = React.useRef(
    new SshAddon(sockjs, {
      serverUuid: '123e4567-e89b-12d3-a456-426614174000',
      header: {
        Authorization:
          'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
      },
      connectImmediately: true,
      onKey: console.log,
    }),
  ).current;

  const onDidMount = React.useCallback((terminal: QPXterm) => {
    terminalRef.current = terminal;
  }, []);

  React.useEffect(() => {
    const callback = (event: Event | TerminalKeyEvent) => {
      console.log(event);
    };

    sshAddon.addEventListener('connect', callback);
    sshAddon.addEventListener('message', callback);
    sshAddon.addEventListener('key', callback);
    sshAddon.addEventListener('error', callback);
    sshAddon.addEventListener('close', callback);

    return () => {
      sshAddon.removeEventListener('connect', callback);
      sshAddon.removeEventListener('message', callback);
      sshAddon.removeEventListener('key', callback);
      sshAddon.removeEventListener('error', callback);
      sshAddon.removeEventListener('close', callback);
    };
  }, []);

  React.useEffect(() => {
    return () => {
      sshAddon.removeAllListeners();
    };
  });

  return <QPXterm onDidMount={onDidMount} addons={[sshAddon]} />;
};

export default Term;