2012-10-27 00:01:05 +01:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
2010-11-08 01:06:20 +00:00
|
|
|
// A WebSocket to TCP socket proxy
|
2012-02-23 23:06:35 +00:00
|
|
|
// Copyright 2012 Joel Martin
|
2010-11-08 01:06:20 +00:00
|
|
|
// Licensed under LGPL version 3 (see docs/LICENSE.LGPL-3)
|
|
|
|
|
2012-10-26 21:47:36 +01:00
|
|
|
// Known to work with node 0.8.9
|
2017-02-03 15:02:17 +00:00
|
|
|
// Requires node modules: ws and optimist
|
|
|
|
// npm install ws optimist
|
2012-10-26 21:47:36 +01:00
|
|
|
|
2010-11-08 01:06:20 +00:00
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
var argv = require('optimist').argv,
|
|
|
|
net = require('net'),
|
|
|
|
http = require('http'),
|
2012-10-29 21:59:46 +00:00
|
|
|
https = require('https'),
|
2012-02-23 23:06:35 +00:00
|
|
|
url = require('url'),
|
|
|
|
path = require('path'),
|
|
|
|
fs = require('fs'),
|
2017-10-12 17:28:30 +01:00
|
|
|
mime = require('mime-types'),
|
2010-11-08 01:06:20 +00:00
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
Buffer = require('buffer').Buffer,
|
|
|
|
WebSocketServer = require('ws').Server,
|
2010-11-08 01:06:20 +00:00
|
|
|
|
2012-10-29 21:59:46 +00:00
|
|
|
webServer, wsServer,
|
2012-02-23 23:06:35 +00:00
|
|
|
source_host, source_port, target_host, target_port,
|
|
|
|
web_path = null;
|
2010-11-08 01:06:20 +00:00
|
|
|
|
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
// Handle new WebSocket client
|
2017-05-17 15:37:07 +01:00
|
|
|
new_client = function(client, req) {
|
2012-10-26 21:47:36 +01:00
|
|
|
var clientAddr = client._socket.remoteAddress, log;
|
2017-10-21 19:01:55 +01:00
|
|
|
var start_time = new Date().getTime();
|
|
|
|
|
2017-05-18 10:08:54 +01:00
|
|
|
console.log(req ? req.url : client.upgradeReq.url);
|
2012-10-26 21:47:36 +01:00
|
|
|
log = function (msg) {
|
|
|
|
console.log(' ' + clientAddr + ': '+ msg);
|
|
|
|
};
|
|
|
|
log('WebSocket connection');
|
|
|
|
log('Version ' + client.protocolVersion + ', subprotocol: ' + client.protocol);
|
|
|
|
|
2017-10-21 19:01:55 +01:00
|
|
|
if (argv.record) {
|
2018-10-16 15:10:37 +01:00
|
|
|
var rs = fs.createWriteStream(argv.record + '/' + new Date().toISOString().replace(/:/g, "_"));
|
2017-10-21 19:01:55 +01:00
|
|
|
rs.write('var VNC_frame_data = [\n');
|
|
|
|
} else {
|
|
|
|
var rs = null;
|
|
|
|
}
|
|
|
|
|
2012-10-26 21:47:36 +01:00
|
|
|
var target = net.createConnection(target_port,target_host, function() {
|
|
|
|
log('connected to target');
|
2012-02-23 23:06:35 +00:00
|
|
|
});
|
|
|
|
target.on('data', function(data) {
|
2012-10-26 21:47:36 +01:00
|
|
|
//log("sending message: " + data);
|
2017-10-21 19:01:55 +01:00
|
|
|
|
|
|
|
if (rs) {
|
|
|
|
var tdelta = Math.floor(new Date().getTime()) - start_time;
|
|
|
|
var rsdata = '\'{' + tdelta + '{' + decodeBuffer(data) + '\',\n';
|
|
|
|
rs.write(rsdata);
|
|
|
|
}
|
|
|
|
|
2012-10-26 21:47:36 +01:00
|
|
|
try {
|
2017-01-28 13:50:48 +00:00
|
|
|
client.send(data);
|
2012-10-26 21:47:36 +01:00
|
|
|
} catch(e) {
|
|
|
|
log("Client closed, cleaning up target");
|
|
|
|
target.end();
|
|
|
|
}
|
2012-02-23 23:06:35 +00:00
|
|
|
});
|
|
|
|
target.on('end', function() {
|
2012-10-26 21:47:36 +01:00
|
|
|
log('target disconnected');
|
2013-06-27 22:03:16 +01:00
|
|
|
client.close();
|
2017-10-21 19:01:55 +01:00
|
|
|
if (rs) {
|
|
|
|
rs.end('\'EOF\'];\n');
|
|
|
|
}
|
2013-06-27 22:03:16 +01:00
|
|
|
});
|
|
|
|
target.on('error', function() {
|
|
|
|
log('target connection error');
|
|
|
|
target.end();
|
|
|
|
client.close();
|
2017-10-21 19:01:55 +01:00
|
|
|
if (rs) {
|
|
|
|
rs.end('\'EOF\'];\n');
|
|
|
|
}
|
2012-02-23 23:06:35 +00:00
|
|
|
});
|
2010-11-08 01:06:20 +00:00
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
client.on('message', function(msg) {
|
2012-10-26 21:47:36 +01:00
|
|
|
//log('got message: ' + msg);
|
2017-10-21 19:01:55 +01:00
|
|
|
|
|
|
|
if (rs) {
|
|
|
|
var rdelta = Math.floor(new Date().getTime()) - start_time;
|
|
|
|
var rsdata = ('\'}' + rdelta + '}' + decodeBuffer(msg) + '\',\n');
|
|
|
|
~ rs.write(rsdata);
|
|
|
|
}
|
|
|
|
|
2017-01-28 13:50:48 +00:00
|
|
|
target.write(msg);
|
2012-02-23 23:06:35 +00:00
|
|
|
});
|
|
|
|
client.on('close', function(code, reason) {
|
2012-10-26 21:47:36 +01:00
|
|
|
log('WebSocket client disconnected: ' + code + ' [' + reason + ']');
|
|
|
|
target.end();
|
2012-02-23 23:06:35 +00:00
|
|
|
});
|
|
|
|
client.on('error', function(a) {
|
2012-10-26 21:47:36 +01:00
|
|
|
log('WebSocket client error: ' + a);
|
|
|
|
target.end();
|
2012-02-23 23:06:35 +00:00
|
|
|
});
|
|
|
|
};
|
2010-11-08 01:06:20 +00:00
|
|
|
|
2017-10-21 19:01:55 +01:00
|
|
|
function decodeBuffer(buf) {
|
|
|
|
var returnString = '';
|
|
|
|
for (var i = 0; i < buf.length; i++) {
|
|
|
|
if (buf[i] >= 48 && buf[i] <= 90) {
|
|
|
|
returnString += String.fromCharCode(buf[i]);
|
|
|
|
} else if (buf[i] === 95) {
|
|
|
|
returnString += String.fromCharCode(buf[i]);
|
|
|
|
} else if (buf[i] >= 97 && buf[i] <= 122) {
|
|
|
|
returnString += String.fromCharCode(buf[i]);
|
|
|
|
} else {
|
|
|
|
var charToConvert = buf[i].toString(16);
|
|
|
|
if (charToConvert.length === 0) {
|
|
|
|
returnString += '\\x00';
|
|
|
|
} else if (charToConvert.length === 1) {
|
|
|
|
returnString += '\\x0' + charToConvert;
|
|
|
|
} else {
|
|
|
|
returnString += '\\x' + charToConvert;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnString;
|
|
|
|
}
|
2010-11-08 01:06:20 +00:00
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
// Send an HTTP error response
|
|
|
|
http_error = function (response, code, msg) {
|
|
|
|
response.writeHead(code, {"Content-Type": "text/plain"});
|
|
|
|
response.write(msg + "\n");
|
|
|
|
response.end();
|
|
|
|
return;
|
2010-11-08 01:06:20 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
// Process an HTTP static file request
|
|
|
|
http_request = function (request, response) {
|
|
|
|
// console.log("pathname: " + url.parse(req.url).pathname);
|
|
|
|
// res.writeHead(200, {'Content-Type': 'text/plain'});
|
|
|
|
// res.end('okay');
|
2010-11-08 01:06:20 +00:00
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
if (! argv.web) {
|
|
|
|
return http_error(response, 403, "403 Permission Denied");
|
2010-11-08 04:28:08 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
var uri = url.parse(request.url).pathname
|
|
|
|
, filename = path.join(argv.web, uri);
|
2017-05-17 15:37:07 +01:00
|
|
|
|
2012-09-21 14:19:40 +01:00
|
|
|
fs.exists(filename, function(exists) {
|
2012-02-23 23:06:35 +00:00
|
|
|
if(!exists) {
|
|
|
|
return http_error(response, 404, "404 Not Found");
|
2010-11-08 01:06:20 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
if (fs.statSync(filename).isDirectory()) {
|
|
|
|
filename += '/index.html';
|
2010-11-08 01:06:20 +00:00
|
|
|
}
|
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
fs.readFile(filename, "binary", function(err, file) {
|
|
|
|
if(err) {
|
|
|
|
return http_error(response, 500, err);
|
2010-11-08 01:06:20 +00:00
|
|
|
}
|
|
|
|
|
2017-10-12 17:28:30 +01:00
|
|
|
var headers = {};
|
|
|
|
var contentType = mime.contentType(path.extname(filename));
|
|
|
|
if (contentType !== false) {
|
|
|
|
headers['Content-Type'] = contentType;
|
|
|
|
}
|
|
|
|
|
|
|
|
response.writeHead(200, headers);
|
2012-02-23 23:06:35 +00:00
|
|
|
response.write(file, "binary");
|
|
|
|
response.end();
|
2010-11-08 04:28:08 +00:00
|
|
|
});
|
|
|
|
});
|
2012-02-23 23:06:35 +00:00
|
|
|
};
|
2010-11-08 01:06:20 +00:00
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
// parse source and target arguments into parts
|
2010-11-08 01:06:20 +00:00
|
|
|
try {
|
2012-09-21 14:19:40 +01:00
|
|
|
source_arg = argv._[0].toString();
|
|
|
|
target_arg = argv._[1].toString();
|
|
|
|
|
2010-11-08 01:06:20 +00:00
|
|
|
var idx;
|
|
|
|
idx = source_arg.indexOf(":");
|
|
|
|
if (idx >= 0) {
|
2010-11-08 04:28:08 +00:00
|
|
|
source_host = source_arg.slice(0, idx);
|
|
|
|
source_port = parseInt(source_arg.slice(idx+1), 10);
|
2010-11-08 01:06:20 +00:00
|
|
|
} else {
|
|
|
|
source_host = "";
|
|
|
|
source_port = parseInt(source_arg, 10);
|
|
|
|
}
|
|
|
|
|
|
|
|
idx = target_arg.indexOf(":");
|
|
|
|
if (idx < 0) {
|
|
|
|
throw("target must be host:port");
|
|
|
|
}
|
2010-11-08 04:28:08 +00:00
|
|
|
target_host = target_arg.slice(0, idx);
|
|
|
|
target_port = parseInt(target_arg.slice(idx+1), 10);
|
2010-11-08 01:06:20 +00:00
|
|
|
|
|
|
|
if (isNaN(source_port) || isNaN(target_port)) {
|
|
|
|
throw("illegal port");
|
|
|
|
}
|
|
|
|
} catch(e) {
|
2017-10-21 19:01:55 +01:00
|
|
|
console.error("websockify.js [--web web_dir] [--cert cert.pem [--key key.pem]] [--record dir] [source_addr:]source_port target_addr:target_port");
|
2010-11-08 01:06:20 +00:00
|
|
|
process.exit(2);
|
|
|
|
}
|
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
console.log("WebSocket settings: ");
|
|
|
|
console.log(" - proxying from " + source_host + ":" + source_port +
|
|
|
|
" to " + target_host + ":" + target_port);
|
|
|
|
if (argv.web) {
|
|
|
|
console.log(" - Web server active. Serving: " + argv.web);
|
|
|
|
}
|
|
|
|
|
2012-10-29 21:59:46 +00:00
|
|
|
if (argv.cert) {
|
|
|
|
argv.key = argv.key || argv.cert;
|
|
|
|
var cert = fs.readFileSync(argv.cert),
|
|
|
|
key = fs.readFileSync(argv.key);
|
|
|
|
console.log(" - Running in encrypted HTTPS (wss://) mode using: " + argv.cert + ", " + argv.key);
|
|
|
|
webServer = https.createServer({cert: cert, key: key}, http_request);
|
|
|
|
} else {
|
|
|
|
console.log(" - Running in unencrypted HTTP (ws://) mode");
|
|
|
|
webServer = http.createServer(http_request);
|
2012-10-26 21:47:36 +01:00
|
|
|
}
|
2012-10-29 21:59:46 +00:00
|
|
|
webServer.listen(source_port, function() {
|
2017-01-28 13:50:48 +00:00
|
|
|
wsServer = new WebSocketServer({server: webServer});
|
2012-02-23 23:06:35 +00:00
|
|
|
wsServer.on('connection', new_client);
|
|
|
|
});
|