Permalink
Browse files

geoip: check for plugin.geoip in register (vs connect)

provides an early error message, rather than later when a connection arrives
  • Loading branch information...
msimerson committed Jun 27, 2014
1 parent 70a5429 commit 24c065872a17faee7f09da5dee743af376e4482b
Showing with 33 additions and 26 deletions.
  1. +23 −18 plugins/connect.geoip.js
  2. +10 −8 tests/plugins/connect.geoip.js
View
@@ -10,6 +10,13 @@ exports.register = function () {
plugin.logerror("unable to load geoip-lite, try\n\n\t'npm install -g geoip-lite'\n\n");
return;
}
+
+ if (!plugin.geoip) {
+ // geoip-lite dropped node 0.8 support
+ plugin.logerror("unable to load geoip-lite");
+ return;
+ }
+
plugin.register_hook('connect', 'geoip_lookup');
plugin.register_hook('data_post', 'geoip_headers');
};
@@ -24,14 +31,8 @@ exports.geoip_lookup = function (next, connection) {
// city: 'San Francisco',
// ll: [37.7484, -122.4156]
- if (!plugin.geoip) {
- // geoip-lite dropped node 0.8 support
- connection.results.add(plugin, {err: "geoip-lite not loaded!"});
- return next();
- }
-
var r = plugin.geoip.lookup(connection.remote_ip);
- if (!r) return next();
+ if (!r) { return next(); }
connection.results.add(plugin, r);
@@ -47,9 +48,9 @@ exports.geoip_lookup = function (next, connection) {
}
var show = [ r.country ];
- if (r.region && plugin.cfg.main.show_region) show.push(r.region);
- if (r.city && plugin.cfg.main.show_city ) show.push(r.city);
- if (r.distance ) show.push(r.distance+'km');
+ if (r.region && plugin.cfg.main.show_region) { show.push(r.region); }
+ if (r.city && plugin.cfg.main.show_city ) { show.push(r.city); }
+ if (r.distance ) { show.push(r.distance+'km');}
connection.results.add(plugin, {human: show.join(', '), emit:true});
return next();
@@ -58,7 +59,7 @@ exports.geoip_lookup = function (next, connection) {
exports.geoip_headers = function (next, connection) {
var plugin = this;
var txn = connection.transaction;
- if (!txn) return;
+ if (!txn) { return; }
txn.remove_header('X-Haraka-GeoIP');
txn.remove_header('X-Haraka-GeoIP-Received');
var geoip = connection.results.get('connect.geoip');
@@ -69,11 +70,11 @@ exports.geoip_headers = function (next, connection) {
var received = [];
var rh = plugin.received_headers(connection);
- if (rh) received.push(rh);
- if (!rh) plugin.user_agent(connection); // No received headers.
+ if ( rh) { received.push(rh); }
+ if (!rh) { plugin.user_agent(connection); } // No received headers.
var oh = plugin.originating_headers(connection);
- if (oh) received.push(oh);
+ if (oh) { received.push(oh); }
// Add any received results to a trace header
if (received.length) {
@@ -86,6 +87,10 @@ exports.calculate_distance = function (connection, r_geoip) {
var plugin = this;
var cb = function (err, l_ip) {
+ if (err) {
+ connection.results.add(plugin, {err: err});
+ connection.logerror(plugin, err);
+ }
if (!plugin.local_ip) { plugin.local_ip = l_ip; }
if (!plugin.local_ip) { plugin.local_ip = plugin.cfg.main.public_ip; }
if (!plugin.local_ip) {
@@ -118,18 +123,18 @@ exports.haversine = function (lat1, lon1, lat2, lon2) {
// calculate the great circle distance using the haversine formula
// found here: http://www.movable-type.co.uk/scripts/latlong.html
var R = 6371; // km
- function toRad(v) { return v * Math.PI / 180; };
+ function toRad(v) { return v * Math.PI / 180; }
var dLat = toRad(lat2-lat1);
var dLon = toRad(lon2-lon1);
- var lat1 = toRad(lat1);
- var lat2 = toRad(lat2);
+ lat1 = toRad(lat1);
+ lat2 = toRad(lat2);
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2);
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
var d = R * c;
return d.toFixed(0);
-}
+};
exports.received_headers = function (connection) {
var plugin = this;
@@ -17,8 +17,7 @@ function _set_up(callback) {
try {
this.plugin.geoip = require('geoip-lite');
}
- catch (e) {
- }
+ catch (ignore) {}
this.connection = Connection.createConnection();
this.connection.results = new ResultStore(this.plugin);
@@ -35,7 +34,7 @@ function _tear_down(callback) {
exports.haversine = {
setUp : _set_up,
tearDown : _tear_down,
- 'WA to MI': function (test) {
+ 'WA to MI is 2000-2500km': function (test) {
test.expect(2);
var r = this.plugin.haversine(47.673, -122.3419, 38, -97);
test.equal(true, (r > 2000));
@@ -49,13 +48,15 @@ exports.geoip_lookup = {
setUp : _set_up,
tearDown : _tear_down,
- 'seattle: ': function (test) {
+ 'seattle: lat + long': function (test) {
var cb = function (rc) {
test.expect(1);
test.equal(undefined, rc);
if (this.plugin.geoip) {
- test.expect(2);
+ test.expect(4);
var r = this.connection.results.get('connect.geoip');
+ test.equal(47.6738, r.ll[0]);
+ test.equal(-122.3419, r.ll[1]);
// console.log(r);
test.ok(r);
}
@@ -64,14 +65,15 @@ exports.geoip_lookup = {
this.connection.remote_ip='192.48.85.146';
this.plugin.geoip_lookup(cb, this.connection);
},
- 'michigan: ': function (test) {
- test.expect(2);
+ 'michigan: lat + long': function (test) {
var cb = function (rc) {
test.expect(1);
test.equal(undefined, rc);
if (this.plugin.geoip) {
- test.expect(2);
+ test.expect(4);
var r = this.connection.results.get('connect.geoip');
+ test.equal(38, r.ll[0]);
+ test.equal(-97, r.ll[1]);
// console.log(r);
test.ok(r);
}

0 comments on commit 24c0658

Please sign in to comment.