-
Notifications
You must be signed in to change notification settings - Fork 73
/
geo.js
89 lines (69 loc) · 2.14 KB
/
geo.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
// if you know of a better way or a public geolocation API, please modify this!
var http = require('http')
function Geo(options) {
var self = this;
function request(options, callback)
{
http_handler = http;
var req = http_handler.request(options, function(res) {
res.setEncoding('utf8');
var result = '';
res.on('data', function (data) {
result += data;
});
res.on('end', function () {
callback(null, result);
});
});
req.on('socket', function (socket) {
socket.setTimeout(options.timeout);
socket.on('timeout', function() {
req.abort();
});
});
req.on('error', function(e) {
callback(e);
});
req.end();
}
function extract_geo(html) {
// if you have a better way of doing this
// or know of a free geoip locator, then
// please change this!
html = html.replace(/[\r\n]/g, "");
var b = html.match(/Country:.*absmiddle/gm);
var c = b[0].match(/_blank.*\<\/a\>/g);
var d = c[0]
var country = d.substring(8,d.length-4);
var e = b[0].match(/src=\'.*alt/g);
var f = e[0];
var img = "http://www.geoiptool.com/"+f.substring(6, f.length-5);
var o = {
country : country,
img : img
}
return o;
}
self.get = function(ip, callback) {
// console.log("QUERYING IP:",ip);
var options = {
host : 'www.geoiptool.com',
port : 80,
path: '/en/?IP='+ip,
method: 'GET'
}
request(options, function(err, response) {
if(err)
return callback(err);
var geo = null;
try {
var geo = extract_geo(response);
// console.log(geo.country," ",geo.img);
} catch(ex) {
console.error(ex);
}
return callback(null, geo);
}, true);
}
}
module.exports = Geo;