Skip to content

Instantly share code, notes, and snippets.

@chemdemo
Last active August 29, 2015 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chemdemo/9109712 to your computer and use it in GitHub Desktop.
Save chemdemo/9109712 to your computer and use it in GitHub Desktop.
simple proxy by Node.js
var http = require('http');
var fs = require('fs');
var cp = require('child_process');
var getSocketPath = function(cpu) {
return __dirname + '/' + cpu + '.sock';
};
var cpu = 0;
// node process was bound to the first cpu in this demo.
var socketPath = getSocketPath(cpu);
if(fs.existsSync(socketPath)) fs.unlinkSync(socketPath);
var server = http.createServer(function(req, res) {
var request = http.request({
socketPath: socketPath,
path: req.url,
method: req.method,
headers: req.headers,
agent: false
});
request.setNoDelay(false);
request
.on('request', function(response) {
res.writeHead(response.statusCode, response.headers);
res.setHeader('server-proxy', 'socket');
response.on('data', res.write);
response.on('end', res.end);
})
.on('error', function(err) {
request.abort();
});
if(undefined === req.headers['x-forworded-for']) {
req.headers['x-forworded-for'] = req.socket.remoteAddress;
}
req.on('data', request.write);
req.on('error', request.abort);
req.on('end', request.end);
});
// for http request
server.listen(8889, '0.0.0.0', function(err) {
console.log('port listen:', err);
});
// for socket request
server.listen(socketPath, function(err) {
console.log('socket listen:', err);
});
var shell = cp.spawn('taskset', ['-cp', cpu, process.pid]);
shell.stdout.on('data', function(buf) {
console.log(buf.toString('utf8'));
});
shell.stderr.on('data', function(buf) {
console.error(buf.toString('utf8'));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment