-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgeolocate.js
87 lines (71 loc) · 2.33 KB
/
geolocate.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
// # Geolocate
// Geolocates incoming requests and emits them as an event
//
// Geolocation is provided by http://freegeoip.net which
// is a free sevice rate limited to 10,000 queries per hour
// or 166 per minute or 2.7 per second
var util = require('util');
var EventEmitter = require('events').EventEmitter;
var superagent = require('superagent');
/**
{
"areacode": "",
"city": "London",
"country_code": "GB",
"country_name": "United Kingdom",
"ip": "5.65.148.167",
"latitude": 51.5142,
"longitude": -0.0931,
"metro_code": "",
"region_code": "H9",
"region_name": "London, City of",
"zipcode": ""
}
*/
function Geolocate () {
this.api = "http://freegeoip.net/json/";
this.lastRequest = 0;
this.rateLimit = 0.5;
}
Geolocate.prototype.getUrl = function ( ipaddress ) {
return this.api + ipaddress;
};
// Get the the location from an ip address
Geolocate.prototype.locationFromIpAddress = function ( ipAddress, callback ) {
var self = this;
var now = Date.now();
// Rate limit
if ( now - this.lastRequest > this.rateLimit ) {
this.lastRequest = now;
superagent.get( this.getUrl(ipAddress), function (err, res) {
if (err) {
callback( err );
return;
}
if (typeof res.body === 'undefined') {
callback( "Geolocate error: no body in responce from " + self.api );
return;
}
callback( err, res.body );
});
} else {
callback( Geolocate.ERROR_RATE_LIMITED );
}
};
// Gets the ipaddress from a request. Is rate limited.
Geolocate.prototype.locationFromReq = function ( req, callback ) {
this.locationFromIpAddress( this.ipaddressFromRequest(req), callback );
};
// #Static
Geolocate.ERROR_RATE_LIMITED = "Geolocate error: rate limited";
// Gets the ip address out of a node request
Geolocate.ipaddressFromRequest = function ( req ) {
if (req.headers['X-Forwarded-For']) return req.headers['X-Forwarded-For'];
if (req.connection.remoteAddress) return req.connection.remoteAddress;
if (req.ip) return req.ip;
if (req._remoteAddress) return req._remoteAddress;
var sock = req.socket;
if (sock.socket) return sock.socket.remoteAddress;
return sock.remoteAddress;
};
module.exports = Geolocate;