From a46514b397acdf42451d41398ef77eec60744256 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Wed, 21 Oct 2015 12:47:02 -0700 Subject: Cluster-geo launcher. --- .gitignore | 4 +- cluster-geo/cluster-geo.exe | 13 +++++ cluster-geo/cluster-geo/config.js.sample | 7 +++ cluster-geo/cluster-geo/index.js | 94 ++++++++++++++++++++++++++++++++ cluster-geo/cluster-geo/package.json | 16 ++++++ cluster-geo/config.js.sample | 7 --- cluster-geo/index.js | 94 -------------------------------- cluster-geo/package.json | 16 ------ 8 files changed, 132 insertions(+), 119 deletions(-) create mode 100755 cluster-geo/cluster-geo.exe create mode 100644 cluster-geo/cluster-geo/config.js.sample create mode 100644 cluster-geo/cluster-geo/index.js create mode 100644 cluster-geo/cluster-geo/package.json delete mode 100644 cluster-geo/config.js.sample delete mode 100644 cluster-geo/index.js delete mode 100644 cluster-geo/package.json diff --git a/.gitignore b/.gitignore index d314b3fc..06b06b7d 100755 --- a/.gitignore +++ b/.gitignore @@ -52,8 +52,8 @@ Thumbs.db node_modules # cluster-geo stuff -cluster-geo/config.js -cluster-geo/cache.* +cluster-geo/cluster-geo/config.js +cluster-geo/cluster-geo/cache.* # MacGap wrapper build files /ext/mac-ui-macgap1-wrapper/src/MacGap.xcodeproj/project.xcworkspace/xcuserdata/* diff --git a/cluster-geo/cluster-geo.exe b/cluster-geo/cluster-geo.exe new file mode 100755 index 00000000..ae720610 --- /dev/null +++ b/cluster-geo/cluster-geo.exe @@ -0,0 +1,13 @@ +#!/bin/bash + +export PATH=/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin + +cd `dirname $0` +if [ ! -d cluster-geo -o ! -f cluster-geo/index.js ]; then + echo 'Cannot find ./cluster-geo containing NodeJS script files.' + exit 1 +fi + +cd cluster-geo + +exec node index.js diff --git a/cluster-geo/cluster-geo/config.js.sample b/cluster-geo/cluster-geo/config.js.sample new file mode 100644 index 00000000..ec1ebfea --- /dev/null +++ b/cluster-geo/cluster-geo/config.js.sample @@ -0,0 +1,7 @@ +// MaxMind GeoIP2 config +module.exports.maxmind = { + userId: 1234, + licenseKey: 'asdf', + service: 'city', + requestTimeout: 1000 +}; diff --git a/cluster-geo/cluster-geo/index.js b/cluster-geo/cluster-geo/index.js new file mode 100644 index 00000000..0e903ade --- /dev/null +++ b/cluster-geo/cluster-geo/index.js @@ -0,0 +1,94 @@ +// +// GeoIP lookup service +// + +// GeoIP cache TTL in ms +var CACHE_TTL = (60 * 60 * 24 * 60 * 1000); // 60 days + +var config = require(__dirname + '/config.js'); + +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 cache = require('levelup')(__dirname + '/cache.leveldb'); + +function lookup(ip,callback) +{ + cache.get(ip,function(err,cachedEntryJson) { + if ((!err)&&(cachedEntryJson)) { + try { + var cachedEntry = JSON.parse(cachedEntryJson.toString()); + if (cachedEntry) { + var ts = cachedEntry.ts; + var r = cachedEntry.r; + if ((ts)&&(r)) { + if ((Date.now() - ts) < CACHE_TTL) { + r._cached = true; + return callback(null,r); + } + } + } + } catch (e) {} + } + + geo(ip,function(err,result) { + if (err) + return callback(err,null); + if ((!result)||(!result.location)) + return callback(new Error('null 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); + }); + }); + }); +}; + +var linebuf = ''; +process.stdin.on('readable',function() { + var chunk; + while (null !== (chunk = process.stdin.read())) { + for(var i=0;i 0) { + var ip = linebuf; + lookup(ip,function(err,result) { + if ((err)||(!result)||(!result.location)) { + return process.stdout.write(ip+',0,0,0,0,0,0\n'); + } else { + var lat = parseFloat(result.location.latitude); + var lon = parseFloat(result.location.longitude); + + // Convert to X,Y,Z coordinates from Earth's origin, Earth-as-sphere approximation. + var latRadians = lat * 0.01745329251994; // PI / 180 + var lonRadians = lon * 0.01745329251994; // PI / 180 + var cosLat = Math.cos(latRadians); + var x = Math.round((-6371.0) * cosLat * Math.cos(lonRadians)); // 6371 == Earth's approximate radius in kilometers + var y = Math.round(6371.0 * Math.sin(latRadians)); + var z = Math.round(6371.0 * cosLat * Math.sin(lonRadians)); + + return process.stdout.write(ip+',1,'+lat+','+lon+','+x+','+y+','+z+'\n'); + } + }); + } + linebuf = ''; + } else { + linebuf += String.fromCharCode(c); + } + } + } +}); + +process.stdin.on('end',function() { + cache.close(); + process.exit(0); +}); diff --git a/cluster-geo/cluster-geo/package.json b/cluster-geo/cluster-geo/package.json new file mode 100644 index 00000000..1927197e --- /dev/null +++ b/cluster-geo/cluster-geo/package.json @@ -0,0 +1,16 @@ +{ + "name": "cluster-geo", + "version": "1.0.0", + "description": "Cluster GEO-IP Query Service", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "ZeroTier, Inc.", + "license": "GPL-3.0", + "dependencies": { + "geoip2ws": "^1.7.1", + "leveldown": "^1.4.2", + "levelup": "^1.2.1" + } +} diff --git a/cluster-geo/config.js.sample b/cluster-geo/config.js.sample deleted file mode 100644 index ec1ebfea..00000000 --- a/cluster-geo/config.js.sample +++ /dev/null @@ -1,7 +0,0 @@ -// MaxMind GeoIP2 config -module.exports.maxmind = { - userId: 1234, - licenseKey: 'asdf', - service: 'city', - requestTimeout: 1000 -}; diff --git a/cluster-geo/index.js b/cluster-geo/index.js deleted file mode 100644 index 0e903ade..00000000 --- a/cluster-geo/index.js +++ /dev/null @@ -1,94 +0,0 @@ -// -// GeoIP lookup service -// - -// GeoIP cache TTL in ms -var CACHE_TTL = (60 * 60 * 24 * 60 * 1000); // 60 days - -var config = require(__dirname + '/config.js'); - -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 cache = require('levelup')(__dirname + '/cache.leveldb'); - -function lookup(ip,callback) -{ - cache.get(ip,function(err,cachedEntryJson) { - if ((!err)&&(cachedEntryJson)) { - try { - var cachedEntry = JSON.parse(cachedEntryJson.toString()); - if (cachedEntry) { - var ts = cachedEntry.ts; - var r = cachedEntry.r; - if ((ts)&&(r)) { - if ((Date.now() - ts) < CACHE_TTL) { - r._cached = true; - return callback(null,r); - } - } - } - } catch (e) {} - } - - geo(ip,function(err,result) { - if (err) - return callback(err,null); - if ((!result)||(!result.location)) - return callback(new Error('null 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); - }); - }); - }); -}; - -var linebuf = ''; -process.stdin.on('readable',function() { - var chunk; - while (null !== (chunk = process.stdin.read())) { - for(var i=0;i 0) { - var ip = linebuf; - lookup(ip,function(err,result) { - if ((err)||(!result)||(!result.location)) { - return process.stdout.write(ip+',0,0,0,0,0,0\n'); - } else { - var lat = parseFloat(result.location.latitude); - var lon = parseFloat(result.location.longitude); - - // Convert to X,Y,Z coordinates from Earth's origin, Earth-as-sphere approximation. - var latRadians = lat * 0.01745329251994; // PI / 180 - var lonRadians = lon * 0.01745329251994; // PI / 180 - var cosLat = Math.cos(latRadians); - var x = Math.round((-6371.0) * cosLat * Math.cos(lonRadians)); // 6371 == Earth's approximate radius in kilometers - var y = Math.round(6371.0 * Math.sin(latRadians)); - var z = Math.round(6371.0 * cosLat * Math.sin(lonRadians)); - - return process.stdout.write(ip+',1,'+lat+','+lon+','+x+','+y+','+z+'\n'); - } - }); - } - linebuf = ''; - } else { - linebuf += String.fromCharCode(c); - } - } - } -}); - -process.stdin.on('end',function() { - cache.close(); - process.exit(0); -}); diff --git a/cluster-geo/package.json b/cluster-geo/package.json deleted file mode 100644 index 1927197e..00000000 --- a/cluster-geo/package.json +++ /dev/null @@ -1,16 +0,0 @@ -{ - "name": "cluster-geo", - "version": "1.0.0", - "description": "Cluster GEO-IP Query Service", - "main": "index.js", - "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" - }, - "author": "ZeroTier, Inc.", - "license": "GPL-3.0", - "dependencies": { - "geoip2ws": "^1.7.1", - "leveldown": "^1.4.2", - "levelup": "^1.2.1" - } -} -- cgit v1.2.3