-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathwebtelnet-proxy.js
201 lines (168 loc) · 4.81 KB
/
webtelnet-proxy.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
201
'use strict';
(function(){
var net = require('net'),
iconv = require('iconv-lite'),
TextDecoder = require('text-encoding').TextDecoder;
// string to uint array
function unicodeStringToTypedArray(s) {
var escstr = encodeURIComponent(s);
var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) {
return String.fromCharCode('0x' + p1);
});
var ua = new Uint8Array(binstr.length);
Array.prototype.forEach.call(binstr, function (ch, i) {
ua[i] = ch.charCodeAt(0);
});
return ua;
}
// uint array to string
function typedArrayToUnicodeString(ua) {
var binstr = Array.prototype.map.call(ua, function (ch) {
return String.fromCharCode(ch);
}).join('');
var escstr = binstr.replace(/(.)/g, function (m, p) {
var code = p.charCodeAt(p).toString(16).toUpperCase();
if (code.length < 2) {
code = '0' + code;
}
return '%' + code;
});
return decodeURIComponent(escstr);
}
function WebTelnetProxy(io, port, host) {
if(this && (this instanceof WebTelnetProxy)) {
this.reset();
if(io) this.bind(io, port, host);
} else {
return new WebTelnetProxy(io, port, host);
}
}
WebTelnetProxy.prototype = {
reset: function() {
this.io = null;
this.logTraffic = false;
this.isRunning = false;
this.timer = 0;
this.lastTick = 0;
this.sockets = {}; // sid -> socket
this.socketsCount = 0;
this.port = 23;
this.host = '127.0.0.1';
this.decoder = new TextDecoder('utf-8');
return this;
},
showTraffic: function(y) {
this.logTraffic = y;
return this;
},
setCharset: function(cs) {
this.charset = cs;
this.decoder = new TextDecoder(cs);
return this;
},
bind: function(io, port, host) {
if(this.isRunning) throw new Error('WebTelnetProxy is already running.');
var proxy = this;
proxy.io = io;
proxy.port = port;
proxy.host = host;
io.on('connection', function(sock){
proxy.onConnected(sock);
});
proxy.lastTick = Date.now();
proxy.isRunning = true;
// init tick() timer
proxy.tick();
proxy.timer = setInterval(function(){
proxy.tick();
}, 1000);
return this;
},
shutdown: function() {
if(!this.isRunning) return;
// clear tick() timer
if(this.timer) clearInterval(this.timer);
this.reset();
return this;
},
tick: function() {
var server = this;
server.lastTick = Date.now();
},
onDisconnected: function(webSock) {
var proxy = this;
var peerSock = webSock.peerSock;
if(peerSock) {
webSock.peerSock = null;
peerSock.peerSock = null;
peerSock.end();
}
delete proxy.sockets[ webSock.id ];
proxy.socketsCount --;
},
connectTelnet: function(webSock) {
var proxy = this;
var telnet = net.connect( proxy.port, proxy.host, function() {
if(proxy.logTraffic) console.log('telnet connected');
webSock.emit('status', 'Telnet connected.\n');
});
telnet.peerSock = webSock;
webSock.peerSock = telnet;
telnet.on('data', function(buf) {
//console.log('telnet: ', buf.toString());
var peerSock = telnet.peerSock;
if(peerSock) {
peerSock.emit('stream', proxy.decoder.decode(buf));
/*
if(proxy.charset && (proxy.charset !== 'utf8')) {
buf = iconv.decode(buf, proxy.charset);
buf = unicodeStringToTypedArray(buf);
}
var arrBuf = new ArrayBuffer(buf.length);
var view = new Uint8Array(arrBuf);
for(var i=0; i<buf.length; ++i) {
view[i] = buf[i];
}
peerSock.emit('stream', arrBuf);
*/
}
});
telnet.on('error', function(){
});
telnet.on('close', function(){
if(proxy.logTraffic) console.log('telnet disconnected');
webSock.emit('status', 'Telnet disconnected.\n');
});
telnet.on('end', function(){
var peerSock = telnet.peerSock;
if(peerSock) {
peerSock.peerSock = null;
telnet.peerSock = null;
}
});
},
onConnected: function(webSock) {
var proxy = this;
if(proxy.logTraffic) console.log('proxy client connected, socket id: ' + webSock.id);
webSock.on('stream', function(message) {
if(proxy.charset && (proxy.charset !== 'utf8')) {
message = iconv.encode(message, proxy.charset);
}
//console.log('websocket: ', message);
var peerSock = webSock.peerSock;
if(peerSock) {
peerSock.write(message);
} else {
proxy.connectTelnet(webSock);
}
});
webSock.on('disconnect', function(){
if(proxy.logTraffic) console.log('proxy client disconnected, socket id: ' + webSock.id);
proxy.onDisconnected(webSock);
});
proxy.sockets[webSock.id] = webSock;
proxy.socketsCount ++;
},
};
exports = module.exports = WebTelnetProxy;
})();