Skip to content

Commit

Permalink
added multi-interface listening
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Tiare Le Bigot committed Sep 16, 2011
1 parent 902dedd commit 8eba601
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 53 deletions.
4 changes: 2 additions & 2 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ var config = {
allow_ip_list: './config/allow_ip_list',
black_list: './config/black_list',
host_filters: './config/hostfilters.js',
proxy_port: 80,
proxy_ip: '0.0.0.0'
listen:[{ip:'91.121.154.113', port:80},
{ip:'2001:41d0:1:db71::1', port:80}]
};

exports.config = config;
Expand Down
26 changes: 21 additions & 5 deletions proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,11 @@ function server_cb(request, response) {
else{request.headers.host = host.host;}

//launch new request
var proxy = http.createClient(host.port || 80, host.host);
var proxy_request = proxy.request(request.method, request.url, request.headers);
try{
var proxy = http.createClient(host.port || 80, host.host);
var proxy_request = proxy.request(request.method, request.url, request.headers);

//proxies to FORWARD answer to real client
//proxies to FORWARD answer to real client
proxy_request.addListener('response', function(proxy_response) {
proxy_response.addListener('data', function(chunk) {
response.write(chunk, 'binary');
Expand All @@ -155,19 +156,34 @@ function server_cb(request, response) {
response.writeHead(proxy_response.statusCode, proxy_response.headers);
});



//proxies to SEND request to real server
request.addListener('data', function(chunk) {
proxy_request.write(chunk, 'binary');
});
request.addListener('end', function() {
proxy_request.end();
});

}catch(err){
console.log('ERROR: Caught exception: ' + err);
sys.log("Connection to "+host.host+":"+(host.port||80)+" failed");
}
}

//last chance error handler
process.on('uncaughtException', function (err) {
console.log('LAST ERROR: Caught exception: ' + err);
});

//startup + log
update_blacklist();
update_iplist();
update_hostfilters();

sys.log("Starting the proxy server on port '" + config.proxy_ip+':'+config.proxy_port);
http.createServer(server_cb).listen(config.proxy_port, config.proxy_ip);
config.listen.forEach(function(listen){
sys.log("Starting reverse proxy server on port '" + listen.ip+':'+listen.port);
http.createServer(server_cb).listen(listen.port, listen.ip);
});

93 changes: 47 additions & 46 deletions readme.txt
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
Nodejs-proxy is a simple HTTP proxy server written in node.js. It currently
just proxies all the requests on port 8080.

Nodejs-proxy was written by Peteris Krumins (peter@catonmat.net).
His blog is at http://www.catonmat.net -- good coders code, great reuse.

------------------------------------------------------------------------------

You'll need node.js to run it. Get it at www.nodejs.org, then compile and
install it:

$ ./configure
$ make
$ make install

Next, run proxy.js through node program:

$ node proxy.js

And that's it!

I have also added ip-based access control and by default the proxy will
deny all IPs. To add a new ip, just echo it to 'allow_ip_list' file:

$ echo '1.2.3.4' >> config/allow_ip_list

And you don't need to restart the server, it will see the changes and update
itself.

You can also block hosts based on a regex pattern, to do that, echo the hosts
you don't wish the proxy to serve to 'black_list' file:

$ echo 'facebook.com' >> config/black_list


More features coming later!


------------------------------------------------------------------------------

Happy proxying!

Sincerely,
Peteris Krumins
http://www.catonmat.net

Nodejs-proxy is a simple HTTP reverse proxy server written in node.js. It currently
allows some mid-complexity to handle the reverse proxy magic take place.

Nodejs-proxy was written by Peteris Krumins (peter@catonmat.net).
His blog is at http://www.catonmat.net -- good coders code, great reuse.

------------------------------------------------------------------------------

You'll need node.js to run it. Get it at www.nodejs.org, then compile and
install it:

$ ./configure
$ make
$ make install

Next, run proxy.js through node program:

$ node proxy.js

And that's it!

I have also added ip-based access control. As long as no ip is explicitly denied,
all will be allowed. If you need a specic access list just echo it to
'allow_ip_list' file:

$ echo '1.2.3.4' >> config/allow_ip_list

And you don't need to restart the server, it will see the changes and update
itself.

You can also block hosts based on a regex pattern, to do that, echo the hosts
you don't wish the proxy to serve to 'black_list' file:

$ echo 'facebook.com' >> config/black_list


More features coming later!


------------------------------------------------------------------------------

Happy proxying!

Sincerely,
Peteris Krumins
http://www.catonmat.net

0 comments on commit 8eba601

Please sign in to comment.