Skip to content

Instantly share code, notes, and snippets.

@mscdex
Last active July 2, 2022 20:10
Show Gist options
  • Save mscdex/941f9c6af6733d18e6e7 to your computer and use it in GitHub Desktop.
Save mscdex/941f9c6af6733d18e6e7 to your computer and use it in GitHub Desktop.

Revisions

  1. mscdex revised this gist Feb 6, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion server.js
    Original file line number Diff line number Diff line change
    @@ -46,7 +46,7 @@ server.on('upgrade', function(request, socket, body) {
    }
    });

    server.listen(8000);
    server.listen(8000, '127.0.0.1');

    app.use(express.static(__dirname + '/public'));
    app.use(term.middleware());
  2. mscdex revised this gist Feb 6, 2016. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions _instructions.md
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,6 @@ Instructions:
    1. `npm install express faye-websocket ssh2 term.js`
    2. Create a `public` subdirectory
    3. Place `client.htm` in `public`
    4. Edit the ssh connection details in `server.js`
    4. Edit the ssh connection details in `server.js`. See the documentation for the `connect()` method in the [ssh2 readme](https://github.com/mscdex/ssh2) for more information.
    5. Start the server with `node server.js`
    6. Visit http://localhost:8000/client.htm in a WebSocket-capable browser
    6. Visit [http://localhost:8000/client.htm](http://localhost:8000/client.htm) in a WebSocket-capable browser
  3. mscdex renamed this gist Feb 6, 2016. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. mscdex created this gist Feb 6, 2016.
    42 changes: 42 additions & 0 deletions client.htm
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    <html>
    <head>
    <title>WebTerm</title>
    <script src="/term.js"></script>
    <script>
    window.addEventListener('load', function() {
    var Socket = window.MozWebSocket || window.WebSocket;
    socket = new Socket('ws://' + location.hostname + ':' + location.port);
    socket.onopen = function() {
    var term = new Terminal({
    cols: 250,
    rows: 100,
    convertEol: true,
    useStyle: true,
    cursorBlink: true,
    screenKeys: true
    });

    term.on('data', function(data) {
    socket.send(data);
    });

    term.on('title', function(title) {
    document.title = title;
    });

    term.open(document.body);

    socket.onmessage = function(event) {
    term.write(event.data);
    };

    socket.onclose = function() {
    term.destroy();
    };
    };
    }, false);
    </script>
    </head>
    <body>
    </body>
    </html>
    8 changes: 8 additions & 0 deletions instructions.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    Instructions:

    1. `npm install express faye-websocket ssh2 term.js`
    2. Create a `public` subdirectory
    3. Place `client.htm` in `public`
    4. Edit the ssh connection details in `server.js`
    5. Start the server with `node server.js`
    6. Visit http://localhost:8000/client.htm in a WebSocket-capable browser
    52 changes: 52 additions & 0 deletions server.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    var WebSocket = require('faye-websocket');
    var express = require('express');
    var app = express();
    var server = require('http').Server(app);
    var term = require('term.js');
    var ssh = require('ssh2');

    server.on('upgrade', function(request, socket, body) {
    if (WebSocket.isWebSocket(request)) {
    var ws = new WebSocket(request, socket, body);
    var conn;
    ws.on('open', function() {
    conn = new ssh();
    conn.on('ready', function() {
    ws.write('\n*** SSH CONNECTION ESTABLISHED ***\n');
    conn.shell(function(err, stream) {
    if (err) {
    ws.write('\n*** SSH SHELL ERROR: ' + err.message + ' ***\n');
    conn.end();
    return;
    }

    // Force binary strings to be sent over websocket to prevent
    // getting a Blob on the browser side which term.js cannot
    // currently handle
    stream.setEncoding('binary');

    stream.pipe(ws).pipe(stream).on('close', function() {
    conn.end();
    });
    });
    }).on('close', function() {
    ws.write('\n*** SSH CONNECTION CLOSED ***\n');
    ws.close();
    }).connect({
    host: '192.168.100.150',
    port: 22,
    username: 'foo',
    password: 'bar'
    });
    }).on('close', function(event) {
    try {
    conn && conn.end();
    } catch (ex) {}
    });
    }
    });

    server.listen(8000);

    app.use(express.static(__dirname + '/public'));
    app.use(term.middleware());