Last active
August 29, 2015 14:22
-
-
Save guileen/6ee860170992184d826c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var sys = require('sys'); | |
var fs = require('fs'); | |
var blacklist = []; | |
var iplist = []; | |
fs.watchFile('./blacklist', function(c,p) { update_blacklist(); }); | |
fs.watchFile('./iplist', function(c,p) { update_iplist(); }); | |
function update_blacklist() { | |
sys.log("Updating blacklist."); | |
blacklist = fs.readFileSync('./blacklist', 'utf-8').split('\n') | |
.filter(function(rx) { return rx.length }) | |
.map(function(rx) { return RegExp(rx) }); | |
} | |
function update_iplist() { | |
sys.log("Updating iplist."); | |
iplist = fs.readFileSync('./iplist', 'utf-8').split('\n') | |
.filter(function(rx) { return rx.length }); | |
} | |
function ip_allowed(ip) { | |
return true | |
for (i in iplist) { | |
if (iplist[i] == ip) { | |
return true; | |
} | |
} | |
return false; | |
} | |
function host_allowed(host) { | |
for (i in blacklist) { | |
if (blacklist[i].test(host)) { | |
return false; | |
} | |
} | |
return true; | |
} | |
function deny(response, msg) { | |
response.writeHead(401); | |
response.write(msg); | |
response.end(); | |
} | |
http.createServer(function(request, response) { | |
var ip = request.connection.remoteAddress; | |
if (!ip_allowed(ip)) { | |
msg = "IP " + ip + " is not allowed to use this proxy"; | |
deny(response, msg); | |
sys.log(msg); | |
return; | |
} | |
if (!host_allowed(request.url)) { | |
msg = "Host " + request.url + " has been denied by proxy configuration"; | |
deny(response, msg); | |
sys.log(msg); | |
return; | |
} | |
sys.log(ip + ": " + request.method + " " + request.url); | |
var proxy = http.createClient(80, request.headers['host']) | |
var proxy_request = proxy.request(request.method, request.url, request.headers); | |
proxy_request.addListener('response', function(proxy_response) { | |
proxy_response.addListener('data', function(chunk) { | |
contentType = proxy_response.headers['content-type'] | |
if(contentType.indexOf('image') == -1) { | |
// console.log('<--', chunk.toString('utf-8')); | |
} | |
response.write(chunk, 'binary'); | |
}); | |
proxy_response.addListener('end', function() { | |
console.log('<--', response.statusCode); | |
response.end(); | |
}); | |
response.writeHead(proxy_response.statusCode, proxy_response.headers); | |
}); | |
request.addListener('data', function(chunk) { | |
console.log('-->', chunk.toString('utf-8')); | |
proxy_request.write(chunk, 'binary'); | |
}); | |
request.addListener('end', function() { | |
proxy_request.end(); | |
}); | |
}).listen(8080); | |
update_blacklist(); | |
update_iplist(); | |
process.on('uncaughtException', function(err) { | |
console.log('Error:', err) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment