Skip to content

Commit

Permalink
Add support for charset conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
floatinghotpot committed Mar 14, 2016
1 parent f1e82dd commit 35904fe
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ var io = socketio.listen(httpserver);

// create webtelnet proxy and bind to io
var webtelnetd = webtelnet(io, conf.telnet.port, conf.telnet.host);

// if you need charset conversion from gbk to utf8
webtelnetd.setCharset('gbk');
```

## Usage as standalone proxy
Expand All @@ -40,11 +43,12 @@ $ [sudo] npm install -g webtelnet
```

```bash
$ webtelnet <http-port> <telnet-port> [-h <telnet-host>] [-w <path/to/www>]
$ webtelnet <http-port> <telnet-port> [-h <telnet-host>] [-w <path/to/www>] [-c <charset>]
```

* By default, telnet-host is 127.0.0.1. You can also proxy to other hosts.
* By default, path/to/www point to WebTelnet web app. You can use customized web app, for example, a web app optimized for specific MUD.
* By default, charset is utf8. You can try gbk or big5, if you need charset conversion.

Example, if you have a MUD server listening on port 4000, to map to http port 8080:

Expand Down
5 changes: 4 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ if(args._.length < 2) {
'Syntax: webtelnet <http-port> <telnet-port> [options]\n' +
'Options: \n' +
' [-h <telnet-host>]\n' +
' [-w <path/to/www>]\n' );
' [-w <path/to/www>]\n' +
' [-c <charset>]\n'
);
process.exit(0);
}

Expand All @@ -55,3 +57,4 @@ var io = socketio.listen(httpserver);

// create webtelnet proxy and bind to io
var webtelnetd = webtelnet(io, conf.telnet.port, conf.telnet.host);
if(args.c) webtelnetd.setCharset(args.c);
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webtelnet",
"version": "1.0.1",
"version": "1.1.0",
"description": "WebTelnet is a proxy server to bridge websocket to telnet, enable visiting telnet servers with browsers",
"main": "./webtelnet-proxy.js",
"bin": {
Expand All @@ -20,17 +20,20 @@
"mud",
"webmud"
],
"author": "Raymond Xie <[email protected]>",
"author": {
"name": "Raymond Xie",
"email": "[email protected]"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/mudchina/webtelnet/issues"
},
"homepage": "https://github.com/mudchina/webtelnet#README.md",
"dependencies": {
"minimist": "^1.2.0",
"express": "^4.13.4",
"iconv-lite": "^0.4.13",
"minimist": "^1.2.0",
"socket.io": "^1.4.4"
},
"devDependencies": {
}
"devDependencies": {}
}
47 changes: 46 additions & 1 deletion webtelnet-proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,36 @@

(function(){

var net = require('net');
var net = require('net'),
iconv = require('iconv-lite');

// 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)) {
Expand All @@ -27,10 +56,18 @@ WebTelnetProxy.prototype = {

this.port = 23;
this.host = '127.0.0.1';
this.charset = '';
return this;
},

showTraffic: function(y) {
this.logTraffic = y;
return this;
},

setCharset: function(cs) {
this.charset = cs;
return this;
},

bind: function(io, port, host) {
Expand Down Expand Up @@ -100,6 +137,11 @@ WebTelnetProxy.prototype = {
//console.log('telnet: ', buf.toString());
var peerSock = telnet.peerSock;
if(peerSock) {
if(proxy.charset && (proxy.charset !== 'utf8')) {
buf = iconv.decode(buf, proxy.charset);
console.log(buf);
buf = unicodeStringToTypedArray(buf);
}
var arrBuf = new ArrayBuffer(buf.length);
var view = new Uint8Array(arrBuf);
for(var i=0; i<buf.length; ++i) {
Expand Down Expand Up @@ -128,6 +170,9 @@ WebTelnetProxy.prototype = {

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) {
Expand Down

0 comments on commit 35904fe

Please sign in to comment.