aboutsummaryrefslogtreecommitdiff
path: root/third_party/websockify/include/wstelnet.js
blob: fca45f3cc47842724addf3cc4848cf6448e648f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/*
 * WebSockets telnet client
 * Copyright (C) 2011 Joel Martin
 * Licensed under LGPL-3 (see LICENSE.txt)
 *
 * Includes VT100.js from:
 *   http://code.google.com/p/sshconsole
 * Which was modified from:
 *   http://fzort.org/bi/o.php#vt100_js
 *
 * Telnet protocol:
 *   http://www.networksorcery.com/enp/protocol/telnet.htm
 *   http://www.networksorcery.com/enp/rfc/rfc1091.txt
 *
 * ANSI escape sequeneces:
 *   http://en.wikipedia.org/wiki/ANSI_escape_code
 *   http://ascii-table.com/ansi-escape-sequences-vt-100.php
 *   http://www.termsys.demon.co.uk/vtansi.htm
 *   http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
 *
 * ASCII codes:
 *   http://en.wikipedia.org/wiki/ASCII
 *   http://www.hobbyprojects.com/ascii-table/ascii-table.html
 *
 * Other web consoles:
 *   http://stackoverflow.com/questions/244750/ajax-console-window-with-ansi-vt100-support
 */




function Telnet(target, connect_callback, disconnect_callback) {

var that = {},  // Public API interface
    vt100, ws, sQ = [];
    termType = "VT100";


Array.prototype.pushStr = function (str) {
    var n = str.length;
    for (var i=0; i < n; i++) {
        this.push(str.charCodeAt(i));
    }
}

function do_send() {
    if (sQ.length > 0) {
        Util.Debug("Sending " + sQ);
        ws.send(sQ);
        sQ = [];
    }
}

function do_recv() {
    //console.log(">> do_recv");
    var arr = ws.rQshiftBytes(ws.rQlen()), str = "",
        chr, cmd, code, value;

    Util.Debug("Received array '" + arr + "'");
    while (arr.length > 0) {
        chr = arr.shift();
        switch (chr) {
        case 255:   // IAC
            cmd = chr;
            code = arr.shift();
            value = arr.shift();
            switch (code) {
            case 254: // DONT
                Util.Debug("Got Cmd DONT '" + value + "', ignoring");
                break;
            case 253: // DO
                Util.Debug("Got Cmd DO '" + value + "'");
                if (value === 24) {
                    // Terminal type
                    Util.Info("Send WILL '" + value + "' (TERM-TYPE)");
                    sQ.push(255, 251, value);
                } else {
                    // Refuse other DO requests with a WONT
                    Util.Debug("Send WONT '" + value + "'");
                    sQ.push(255, 252, value);
                }
                break;
            case 252: // WONT
                Util.Debug("Got Cmd WONT '" + value + "', ignoring");
                break;
            case 251: // WILL
                Util.Debug("Got Cmd WILL '" + value + "'");
                if (value === 1) {
                    // Server will echo, turn off local echo
                    vt100.noecho();
                    // Affirm echo with DO
                    Util.Info("Send Cmd DO '" + value + "' (echo)");
                    sQ.push(255, 253, value);
                } else {
                    // Reject other WILL offers with a DONT
                    Util.Debug("Send Cmd DONT '" + value + "'");
                    sQ.push(255, 254, value);
                }
                break;
            case 250: // SB (subnegotiation)
                if (value === 24) {
                    Util.Info("Got IAC SB TERM-TYPE SEND(1) IAC SE");
                    // TERM-TYPE subnegotiation
                    if (arr[0] === 1 &&
                        arr[1] === 255 &&
                        arr[2] === 240) {
                        arr.shift(); arr.shift(); arr.shift();
                        Util.Info("Send IAC SB TERM-TYPE IS(0) '" + 
                                  termType + "' IAC SE");
                        sQ.push(255, 250, 24, 0); 
                        sQ.pushStr(termType);
                        sQ.push(255, 240);
                    } else {
                        Util.Info("Invalid subnegotiation received" + arr);
                    }
                } else {
                    Util.Info("Ignoring SB " + value);
                }
                break;
            default:
                Util.Info("Got Cmd " + cmd + " " + value + ", ignoring"); }
            continue;
        case 242:   // Data Mark (Synch)
            cmd = chr;
            code = arr.shift();
            value = arr.shift();
            Util.Info("Ignoring Data Mark (Synch)");
            break;
        default:   // everything else
            str += String.fromCharCode(chr);
        }
    }

    if (sQ) {
        do_send();
    }

    if (str) {
        vt100.write(str);
    }

    //console.log("<< do_recv");
}



that.connect = function(host, port, encrypt) {
    var host = host,
        port = port,
        scheme = "ws://", uri;

    Util.Debug(">> connect");
    if ((!host) || (!port)) {
        console.log("must set host and port");
        return;
    }

    if (ws) {
        ws.close();
    }

    if (encrypt) {
        scheme = "wss://";
    }
    uri = scheme + host + ":" + port;
    Util.Info("connecting to " + uri);

    ws.open(uri);

    Util.Debug("<< connect");
}

that.disconnect = function() {
    Util.Debug(">> disconnect");
    if (ws) {
        ws.close();
    }
    vt100.curs_set(true, false);

    disconnect_callback();
    Util.Debug("<< disconnect");
}


function constructor() {
    /* Initialize Websock object */
    ws = new Websock();

    ws.on('message', do_recv);
    ws.on('open', function(e) {
        Util.Info(">> WebSockets.onopen");
        vt100.curs_set(true, true);
        connect_callback();
        Util.Info("<< WebSockets.onopen");
    });
    ws.on('close', function(e) {
        Util.Info(">> WebSockets.onclose");
        that.disconnect();
        Util.Info("<< WebSockets.onclose");
    });
    ws.on('error', function(e) {
        Util.Info(">> WebSockets.onerror");
        that.disconnect();
        Util.Info("<< WebSockets.onerror");
    });

    /* Initialize the terminal emulator/renderer */

    vt100 = new VT100(80, 24, target);


    /*
     * Override VT100 I/O routines
     */

    // Set handler for sending characters
    vt100.getch(
        function send_chr(chr, vt) {
            var i;
            Util.Debug(">> send_chr: " + chr);
            for (i = 0; i < chr.length; i++) {
                sQ.push(chr.charCodeAt(i));
            }
            do_send();
            vt100.getch(send_chr);
        }
    );

    vt100.debug = function(message) {
        Util.Debug(message + "\n");
    }

    vt100.warn = function(message) {
        Util.Warn(message + "\n");
    }

    vt100.curs_set = function(vis, grab, eventist)
    {
        this.debug("curs_set:: vis: " + vis + ", grab: " + grab);
        if (vis !== undefined)
            this.cursor_vis_ = (vis > 0);
        if (eventist === undefined)
            eventist = window;
        if (grab === true || grab === false) {
            if (grab === this.grab_events_)
                return;
            if (grab) {
                this.grab_events_ = true;
                VT100.the_vt_ = this;
                Util.addEvent(eventist, 'keydown', vt100.key_down);
                Util.addEvent(eventist, 'keyup', vt100.key_up);
            } else {
                Util.removeEvent(eventist, 'keydown', vt100.key_down);
                Util.removeEvent(eventist, 'keyup', vt100.key_up);
                this.grab_events_ = false;
                VT100.the_vt_ = undefined;
            }
        }
    }

    vt100.key_down = function(e) {
        var vt = VT100.