summaryrefslogtreecommitdiffstats
path: root/util/lib/geo/index.js
diff options
context:
space:
mode:
authormakefu <github@syntax-fehler.de>2013-02-07 20:26:15 +0100
committermakefu <github@syntax-fehler.de>2013-02-07 20:26:15 +0100
commitdd6ad821a5a5bcb633b28a6d35e5e612d1f59094 (patch)
tree51a5281d1cbba3423eb5e9a86eb16d847fac5a6e /util/lib/geo/index.js
parent12fa3df06555c4ad6973602998a42446fbb52e70 (diff)
parent41fb505a5dc5c81965cc45dd00d14fe4150b8a42 (diff)
Merge branch 'master' of github.com:krebscode/painload
Diffstat (limited to 'util/lib/geo/index.js')
-rw-r--r--util/lib/geo/index.js48
1 files changed, 48 insertions, 0 deletions
diff --git a/util/lib/geo/index.js b/util/lib/geo/index.js
new file mode 100644
index 00000000..0763b1a4
--- /dev/null
+++ b/util/lib/geo/index.js
@@ -0,0 +1,48 @@
+function slurp (stream, callback) {
+ var data = []
+ stream.on('data', function (chunk) {
+ data.push(chunk)
+ })
+ stream.on('end', function () {
+ callback(null, Buffer.concat(data))
+ })
+ stream.resume()
+}
+
+
+var path = require('path')
+var city_dat = path.join(__dirname, 'GeoLiteCity.dat')
+
+var geoip = require('geoip')
+var city = new geoip.City(city_dat)
+
+slurp(process.stdin, function (err, data) {
+ var lines = data.toString().split('\n')
+ // remove last, empty element (caused by the [mandatory] final \n)
+ if (lines.length > 1 && lines[lines.length - 1] === '') {
+ lines.pop()
+ }
+ var name = 0, addr = 1
+ lines
+ .map(function (line) { return line.split(' ') })
+ .forEach(function (host) {
+ //city.lookup(host[addr], function (err, loc) {
+ // if (err) {
+ // console.error('#', host[name], err.message)
+ // } else {
+ // console.log(host[name] + ': ' + loc.longitude + ',' + loc.latitude)
+ // }
+ //})
+ var loc = city.lookupSync(host[addr])
+ //if (!loc) { console.error('#', host[name]) }
+ if (loc) {
+ var a = loc.latitude
+ var b = loc.longitude
+ //var c = loc.altitude
+ //var geo = 'geo:' + [a,b,c].join(',')
+ var geo = 'geo:' + [a,b].join(',')
+
+ console.log(host[name], geo)
+ }
+ })
+})