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-02-23 23:06:35 +00:00
|
|
|
// Known to work with node 0.6
|
|
|
|
// Requires node modules: ws, base64, optimist and policyfile
|
|
|
|
// npm install ws base64 optimist policyfile
|
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'),
|
|
|
|
url = require('url'),
|
|
|
|
path = require('path'),
|
|
|
|
fs = require('fs'),
|
|
|
|
policyfile = require('policyfile'),
|
2010-11-08 01:06:20 +00:00
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
base64 = require('base64/build/Release/base64'),
|
|
|
|
Buffer = require('buffer').Buffer,
|
|
|
|
WebSocketServer = require('ws').Server,
|
2010-11-08 01:06:20 +00:00
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
httpServer, wsServer,
|
|
|
|
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
|
|
|
|
new_client = function(client) {
|
|
|
|
console.log('WebSocket client connected');
|
|
|
|
//console.log('protocol: ' + client.protocol);
|
2010-11-08 01:06:20 +00:00
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
var target = net.createConnection(target_port,target_host);
|
|
|
|
target.on('begin', function() {
|
|
|
|
console.log('connected to target');
|
|
|
|
});
|
|
|
|
target.on('data', function(data) {
|
|
|
|
client.send(base64.encode(new Buffer(data)));
|
|
|
|
});
|
|
|
|
target.on('end', function() {
|
|
|
|
console.log('target disconnected');
|
|
|
|
});
|
2010-11-08 01:06:20 +00:00
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
client.on('message', function(msg) {
|
|
|
|
//console.log('got some message');
|
|
|
|
target.write(base64.decode(msg),'binary');
|
|
|
|
});
|
|
|
|
client.on('close', function(code, reason) {
|
|
|
|
console.log('WebSocket client disconnected: ' + code + ' [' + reason + ']');
|
|
|
|
});
|
|
|
|
client.on('error', function(a) {
|
|
|
|
console.log('WebSocket client error: ' + a);
|
|
|
|
});
|
|
|
|
};
|
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);
|
|
|
|
|
|
|
|
path.exists(filename, function(exists) {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
response.writeHead(200);
|
|
|
|
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
|
|
|
|
source_arg = argv._[0].toString();
|
|
|
|
target_arg = argv._[1].toString();
|
2010-11-08 01:06:20 +00:00
|
|
|
try {
|
|
|
|
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) {
|
|
|
|
console.error("wsproxy.py [source_addr:]source_port target_addr:target_port");
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
httpServer = http.createServer(http_request);
|
|
|
|
httpServer.listen(source_port, function() {
|
|
|
|
wsServer = new WebSocketServer({server: httpServer});
|
|
|
|
wsServer.on('connection', new_client);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Attach Flash policyfile answer service
|
|
|
|
policyfile.createServer().listen(-1, httpServer);
|