Merge branch 'edge' of http://10.6.6.2/zerotier/zerotierone into edge

This commit is contained in:
Joseph Henry 2015-12-18 10:31:24 -08:00
commit c22c0a20f6

View File

@ -19,8 +19,8 @@ if (!config.maxmind) {
console.error('FATAL: only MaxMind GeoIP2 is currently supported and is not configured in config.js');
process.exit(1);
}
var geo = require('geoip2ws')(config.maxmind);
var geo = require('geoip2ws')(config.maxmind);
var cache = require('levelup')(__dirname + '/cache.leveldb');
function lookup(ip,callback)
@ -32,29 +32,40 @@ function lookup(ip,callback)
if (cachedEntry) {
let ts = cachedEntry.ts;
let r = cachedEntry.r;
if (ts) {
if ((Date.now() - ts) < CACHE_TTL)
return callback(null,r);
if ((ts)&&((Date.now() - ts) < CACHE_TTL)) {
//console.error(ip+': cached!');
return callback(null,(r) ? r : null);
}
}
} catch (e) {}
}
geo(ip,function(err,result) {
if (err)
return callback(err,null);
if (!result)
result = null;
cache.put(ip,JSON.stringify({
ts: Date.now() - (CACHE_TTL - 30000), // set ts to expire in 30 seconds while the query is in progress
r: null
}),function(err) {
cache.put(ip,JSON.stringify({
ts: Date.now(),
r: result
}),function(err) {
if (err)
console.error('Error saving to cache: '+err);
return callback(null,result);
geo(ip,function(err,result) {
if (err) {
//console.error(err);
return callback(err,null);
}
if (!result)
result = null;
cache.put(ip,JSON.stringify({
ts: Date.now(),
r: result
}),function(err) {
if (err)
console.error('Error saving to cache: '+err);
return callback(null,result);
});
});
});
});
};