Mestery
Mestery

Reputation: 1

Integrate a terminal in the browser and be able to execute commands in it

How can I integrate a terminal in browser and be able to execute commands in it, like for example the web terminal of glitch.com

Upvotes: 0

Views: 1730

Answers (1)

w00t
w00t

Reputation: 18281

This is how to do it with keystrokes, adapted from https://github.com/mscdex/ssh2/issues/429 and using https://github.com/steelbrain/node-ssh

;(async () => {
    const {NodeSSH} = require('node-ssh')

    const ssh = new NodeSSH()
    await ssh.connect({
        host: 'yourhost',
        username: 'yourusername',
        agent: process.env.SSH_AUTH_SOCK,
        compress: true,
    })

    const pipeStream = stream => {
        const {stdin, stdout, stderr} = process
        const {isTTY} = stdout

        if (isTTY && stdin.setRawMode) stdin.setRawMode(true)

        stream.pipe(stdout)
        stream.stderr.pipe(stderr)
        stdin.pipe(stream)

        const onResize =
            isTTY && (() => stream.setWindow(stdout.rows, stdout.columns, null, null))
        if (isTTY) {
            stream.once('data', onResize)
            process.stdout.on('resize', onResize)
        }
        stream.on('close', () => {
            if (isTTY) process.stdout.removeListener('resize', onResize)
            stream.unpipe()
            stream.stderr.unpipe()
            stdin.unpipe()
            if (stdin.setRawMode) stdin.setRawMode(false)
            stdin.unref()
        })
    }

    await new Promise((resolve, reject) => {
        ssh.connection.shell({term: process.env.TERM || 'vt100'}, (err, stream) => {
            if (err) {
                reject(err)
                return
            }
            pipeStream(stream)
            stream.on('close', () => resolve(true))
        })
    })

    ssh.dispose()
})()

Now it would be fun to figure out how to make this work with rsync via --rsh='node ssh.js'

Upvotes: 2

Related Questions