Skip to content

Commit

Permalink
cache ip lookup. add try/cache to socket writing
Browse files Browse the repository at this point in the history
  • Loading branch information
axiak committed Feb 4, 2012
1 parent 8ad7d48 commit fd4cea1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 7 additions & 0 deletions example/yaluandmike.js
Expand Up @@ -17,10 +17,17 @@ myProxy.on('enabledCheck', function (callback) {
});

/* Reject for Russian spies */
var ipCache = {};
myProxy.on('shouldReject', function (request, callback) {
var client_ip = request.connection.remoteAddress || request.connection.socket.remoteAddress;
var cached = ipCache[client_ip];
if (cached !== undefined) {
callback(cached);
return;
}
var result = geoip.lookup(client_ip);
var should_reject = result && result['country'] != 'US' && result['country'] != 'CA';
ipCache[client_ip] = (should_reject ? true : false);
callback(should_reject);
});

Expand Down
9 changes: 8 additions & 1 deletion lib/proxy.js
Expand Up @@ -213,7 +213,14 @@ module.exports.createProxyServer = function (opts) {
socket.write('HTTP/1.0 200 Connection established\r\n\r\n');
});

clientSocket.on('data', function (data) { socket.write(data); });
clientSocket.on('data', function (data) {
try {
socket.write(data);
} catch (error) {
emitter.emit('error', error);
}
});

clientSocket.on('end', function () { socket.end(); });

socket.on('data', function (data) {
Expand Down

0 comments on commit fd4cea1

Please sign in to comment.