1

I am setting up a discord channel to function as an SSH terminal. A NodeJS server will provide the connection. A custom command will spawn a new terminal instance, which can then be used as a shell.

I don't know how to spawn a terminal within a child process. I have tried using the screen and bash commands to no avail.

I am using CentOS 7.

// Code For Discord
var $discord = {
    currentInterface: null,
    send: (data) => {
        /* some code that sends data to a discord channel */
    },
    receive: (data) => {

        // Send Data To Terminal
        if ($discord.currentInterface) {
            $discord.currentInterface.send(data);
        } else {
            $discord.send('**Error:** Terminal has not been spawned.');
        }
    },
    command: (name, args) => {

        // Recieve Discord Commands
        switch (name) {
            case 'spawn':
                $discord.currentInterface = $interface();
            break;
        }
    }
};

// Create Interface
var $interface = function () {

    // Define object
    let x = {
        terminal: child_process.spawn('screen'),
        send: (data) => {

            // Send Input to Terminal
            x.process.stdin.write(data + '\n');
        },
        receive: (data) => {

            // Send Output to Discord
            $discord.send(data);
        }
    };

    // Process Output
    x.terminal.on('stdout', (data) => {
        x.receive(data);
    });

    // Process Errors
    x.terminal.on('stderr', (error) => {
        x.receive(`**Error:**\n${error}`);
    });

    // Return
    return x;
};

The problem lies with creating the terminal itself. How do you create an SSH-style shell within a child process?

spacefluff432
  • 566
  • 3
  • 13

2 Answers2

1

After realizing how much of an idiot I really am, I found a solution...

// Import Modules
const fs = require('fs');
const child_process = require('child_process');

// Create Interface
var interface = {
    terminal: child_process.spawn('/bin/sh'),
    handler: console.log,
    send: (data) => {
        interface.terminal.stdin.write(data + '\n');
    },
    cwd: () => {
        let cwd = fs.readlinkSync('/proc/' + interface.terminal.pid + '/cwd');
        interface.handler({ type: 'cwd', data: cwd });
    }
};

// Handle Data
interface.terminal.stdout.on('data', (buffer) => {
    interface.handler({ type: 'data', data: buffer });
});

// Handle Error
interface.terminal.stderr.on('data', (buffer) => {
    interface.handler({ type: 'error', data: buffer });
});

// Handle Closure
interface.terminal.on('close', () => {
    interface.handler({ type: 'closure', data: null });
});

Usage...

interface.handler = (output) => {
    let data = '';
    if (output.data) data += ': ' + output.data.toString();
    console.log(output.type + data);
};

interface.send('echo Hello World!');
// Returns: data: Hello World!

interface.send('cd /home');
interface.cwd();
// Returns: cwd: /home

interface.send('abcdef');
// Returns: error: bin/sh: line 2: abcdef: command not found

interface.send('exit');
// Returns: exit
spacefluff432
  • 566
  • 3
  • 13
0

I'd take a look at the documentation for child_process.execFile. There's an option to set the shell on, but it's disabled by default.

There's also this approach if you want to try setting up a batch script. This is set up for windows and the answer isn't set up for passing arguments, but you should be able to adapt it fairly easily.

GenericUser
  • 3,003
  • 1
  • 11
  • 17