-
Notifications
You must be signed in to change notification settings - Fork 104
/
client_term.js
200 lines (171 loc) · 5.16 KB
/
client_term.js
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
/* jslint node: true */
'use strict';
// ENiGMA½
const Log = require('./logger.js').log;
const renegadeToAnsi = require('./color_codes.js').renegadeToAnsi;
const Config = require('./config.js').get;
const iconv = require('iconv-lite');
const assert = require('assert');
const _ = require('lodash');
exports.ClientTerminal = ClientTerminal;
function ClientTerminal(output) {
this.output = output;
let outputEncoding = 'cp437';
assert(iconv.encodingExists(outputEncoding));
// convert line feeds such as \n -> \r\n
this.convertLF = true;
this.syncTermFontsEnabled = false;
//
// Some terminal we handle specially
// They can also be found in this.env{}
//
let termType = 'unknown';
let termHeight = 0;
let termWidth = 0;
let termClient = 'unknown';
this.currentSyncFont = 'not_set';
// Raw values set by e.g. telnet NAWS, ENVIRONMENT, etc.
this.env = {};
Object.defineProperty(this, 'outputEncoding', {
get: function () {
return outputEncoding;
},
set: function (enc) {
if (iconv.encodingExists(enc)) {
Log.info(
{ encoding: enc, currentEncoding: outputEncoding },
`Output encoding changed to ${enc}`
);
outputEncoding = enc;
} else {
Log.warn({ encoding: enc }, 'Unknown encoding');
}
},
});
Object.defineProperty(this, 'termType', {
get: function () {
return termType;
},
set: function (ttype) {
termType = ttype.toLowerCase();
Log.debug(
{ encoding: this.outputEncoding },
`Terminal type changed to ${termType}; Adjusting output encoding`
);
if (this.isNixTerm()) {
this.outputEncoding = 'utf8';
} else {
this.outputEncoding = 'cp437';
}
// :TODO: according to this: http://mud-dev.wikidot.com/article:telnet-client-identification
// Windows telnet will send "VTNT". If so, set termClient='windows'
// there are some others on the page as well
},
});
Object.defineProperty(this, 'termWidth', {
get: function () {
return termWidth;
},
set: function (width) {
if (width > 0) {
termWidth = width;
}
},
});
Object.defineProperty(this, 'termHeight', {
get: function () {
return termHeight;
},
set: function (height) {
if (height > 0) {
termHeight = height;
}
},
});
Object.defineProperty(this, 'termClient', {
get: function () {
return termClient;
},
set: function (tc) {
termClient = tc;
Log.debug({ termClient: this.termClient }, 'Set known terminal client');
},
});
}
ClientTerminal.prototype.disconnect = function () {
this.output = null;
};
ClientTerminal.prototype.isNixTerm = function () {
//
// Standard *nix type terminals
//
if (this.termType.startsWith('xterm')) {
return true;
}
const utf8TermList = Config().term.utf8TermList;
return utf8TermList.includes(this.termType);
};
ClientTerminal.prototype.isANSI = function () {
//
// ANSI terminals should be encoded to CP437
//
// Some terminal types provided by Mercyful Fate / Enthral:
// ANSI-BBS
// PC-ANSI
// QANSI
// SCOANSI
// VT100
// QNX
//
// Reports from various terminals
//
// NetRunner v2.00beta 20
// * This version adds 256 colors and reports as "ansi-256color"
//
// syncterm:
// * SyncTERM
//
// xterm:
// * PuTTY
//
// ansi-bbs:
// * fTelnet
//
// pcansi:
// * ZOC
//
// screen:
// * ConnectBot (Android)
//
// linux:
// * JuiceSSH (note: TERM=linux also)
//
const cp437TermList = Config().term.cp437TermList;
return cp437TermList.includes(this.termType);
};
// :TODO: probably need to update these to convert IAC (0xff) -> IACIAC (escape it)
ClientTerminal.prototype.write = function (s, convertLineFeeds, cb) {
this.rawWrite(this.encode(s, convertLineFeeds), cb);
};
ClientTerminal.prototype.rawWrite = function (s, cb) {
if (this.output && this.output.writable) {
this.output.write(s, err => {
if (cb) {
return cb(err);
}
if (err) {
Log.warn({ error: err.message }, 'Failed writing to socket');
}
});
}
};
ClientTerminal.prototype.pipeWrite = function (s, cb) {
this.write(renegadeToAnsi(s, this), null, cb); // null = use default for |convertLineFeeds|
};
ClientTerminal.prototype.encode = function (s, convertLineFeeds) {
convertLineFeeds = _.isBoolean(convertLineFeeds) ? convertLineFeeds : this.convertLF;
if (convertLineFeeds && _.isString(s)) {
s = s.replace(/\n/g, '\r\n');
}
return iconv.encode(s, this.outputEncoding);
};