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
|
2012-02-23 23:06:35 +00:00
|
|
|
// Requires node modules: ws, base64, optimist and policyfile
|
|
|
|
// npm install ws base64 optimist policyfile
|
2012-10-26 21:47:36 +01:00
|
|
|
//
|
|
|
|
// NOTE:
|
|
|
|
// This version requires a patched version of einaros/ws that supports
|
|
|
|
// subprotocol negotiation. You can use the patched version like this:
|
|
|
|
//
|
|
|
|
// cd websockify/other
|
|
|
|
// git clone https://github.com/kanaka/ws
|
|
|
|
// npm link ./ws
|
|
|
|
|
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) {
|
2012-10-26 21:47:36 +01:00
|
|
|
var clientAddr = client._socket.remoteAddress, log;
|
|
|
|
log = function (msg) {
|
|
|
|
console.log(' ' + clientAddr + ': '+ msg);
|
|
|
|
};
|
|
|
|
log('WebSocket connection');
|
|
|
|
log('Version ' + client.protocolVersion + ', subprotocol: ' + client.protocol);
|
|
|
|
|
|
|
|
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);
|
|
|
|
try {
|
|
|
|
if (client.protocol === 'base64') {
|
|
|
|
client.send(base64.encode(new Buffer(data)));
|
|
|
|
} else {
|
|
|
|
client.send(data,{binary: true});
|
|
|
|
}
|
|
|
|
} 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');
|
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);
|
|
|
|
if (client.protocol === 'base64') {
|
|
|
|
target.write(base64.decode(msg),'binary');
|
|
|
|
} else {
|
|
|
|
target.write(msg,'binary');
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
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
|
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) {
|
2012-10-27 00:01:05 +01:00
|
|
|
console.error("websockify.js [--web web_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-26 21:47:36 +01:00
|
|
|
function selectProtocol(protocols, callback) {
|
|
|
|
var plist = protocols ? protocols.split(',') : "";
|
|
|
|
var plist = protocols.split(',');
|
|
|
|
if (plist.indexOf('binary') >= 0) {
|
|
|
|
callback(true, 'binary');
|
|
|
|
} else if (plist.indexOf('base64') >= 0) {
|
|
|
|
callback(true, 'base64');
|
|
|
|
} else {
|
|
|
|
console.log("Client must support 'binary' or 'base64' protocol");
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 23:06:35 +00:00
|
|
|
httpServer = http.createServer(http_request);
|
|
|
|
httpServer.listen(source_port, function() {
|
2012-10-26 21:47:36 +01:00
|
|
|
wsServer = new WebSocketServer({server: httpServer,
|
|
|
|
handleProtocols: selectProtocol});
|
2012-02-23 23:06:35 +00:00
|
|
|
wsServer.on('connection', new_client);
|
|
|
|
});
|
|
|
|
|
|
|
|
// Attach Flash policyfile answer service
|
|
|
|
policyfile.createServer().listen(-1, httpServer);
|