-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
geoip.js
166 lines (140 loc) · 4.36 KB
/
geoip.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict';
const assert = require('assert')
const path = require('path')
const fixtures = require('haraka-test-fixtures');
const plugin_name = 'geoip-lite';
describe('register', function () {
beforeEach(function (done) {
this.plugin = new fixtures.plugin('geoip');
this.plugin.register().then(done)
})
it('config loaded', function (done) {
assert.ok(this.plugin.cfg);
assert.ok(this.plugin.cfg.main);
done();
})
if (plugin_name === 'geoip') {
it('maxmind module loaded', function (done) {
assert.ok(this.plugin.maxmind);
done();
})
}
if (plugin_name === 'geoip-lite') {
it('geoip-lite module loads', function (done) {
this.plugin.register_geolite()
assert.ok(this.plugin.geoip);
done();
})
}
})
describe('database lookups', function () {
beforeEach(function (done) {
this.plugin = new fixtures.plugin('geoip');
this.plugin.register().then(() => {
this.connection = fixtures.connection.createConnection();
});
if (plugin_name === 'geoip') {
this.plugin.cfg.main.dbdir = path.resolve('test','fixtures');
this.plugin.load_dbs().then(done)
}
else {
done()
}
})
describe('get_geoip', function () {
it('no IP fails', function (done) {
assert.ok(!this.plugin.get_geoip());
done();
})
it('ipv4 private fails', function (done) {
assert.ok(!this.plugin.get_geoip('192.168.2.3'));
done();
})
it('ipv4 public passes', function (done) {
const r = this.plugin.get_geoip('192.48.85.146');
if (plugin_name === 'geoip') {
assert.equal(r.continent.code, 'NA');
assert.equal(r.country.iso_code, 'US');
}
if (plugin_name === 'geoip-lite') {
assert.equal(r.country, 'US');
}
done();
})
if (plugin_name === 'geoip') {
it('ipv6 public passes', function (done) {
const r = this.plugin.get_geoip('2607:f060:b008:feed::6');
assert.equal(r.continent.code, 'NA');
assert.equal(r.country.iso_code, 'US');
done();
})
}
})
describe('lookup', function () {
this.timeout(4000)
it('seattle: lat + long', function (done) {
this.connection.remote.ip='192.48.85.146';
this.plugin.lookup((rc) => {
const r = this.connection.results.get('geoip');
assert.equal('US', r.country);
if (r.continent) assert.equal('NA', r.continent);
done();
}, this.connection);
})
it('michigan: lat + long', function (done) {
this.connection.remote.ip='24.204.186.246';
this.plugin.lookup((rc) => {
const r = this.connection.results.get('geoip');
assert.equal(Math.floor(r.ll[0]), 44);
assert.equal(Math.floor(r.ll[1]), -86);
assert.equal('US', r.country);
if (r.continent) assert.equal('NA', r.continent);
done();
}, this.connection)
})
})
describe('calculate_distance', function () {
// ServedBy ll: [ 47.6738, -122.3419 ],
// WMISD [ 38, -97 ]
it('seattle to michigan', function (done) {
this.plugin.cfg.main.calc_distance=true;
this.plugin.local_ip='192.48.85.146';
this.connection.remote.ip='24.204.186.246';
delete this.plugin.local_geoip;
this.plugin.calculate_distance(this.connection, [38, -97], (err, d) => {
if (err) console.error(err);
assert.ok(d > 50 && d < 4000);
done();
})
})
it('congo to china', function (done) {
this.plugin.cfg.main.calc_distance=true;
this.plugin.local_ip='41.78.192.1';
this.connection.remote.ip='60.168.181.159';
delete this.plugin.local_geoip;
this.plugin.calculate_distance(this.connection, [38, -97], (err, d) => {
if (err) console.error(err);
assert.ok(d > 10000);
done();
})
})
})
})
describe('haversine', function () {
beforeEach(function (done) {
this.plugin = new fixtures.plugin('geoip');
done()
})
it('WA to MI is 2000-2500km', function (done) {
const r = this.plugin.haversine(47.673, -122.3419, 38, -97);
assert.equal((r > 2000), true, r);
assert.equal((r < 2500), true, r);
done();
})
it('DRC to China is 7,000-15,000km', function (done) {
const r = this.plugin.haversine(0, 25, 32, 117);
assert.equal((r > 10000), true, r);
assert.equal((r < 15000), true, r);
done();
})
})