-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeoipTask.js
112 lines (103 loc) · 2.84 KB
/
geoipTask.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
/* */
const path = require('path');
const dns = require('dns');
const json2xml = require('js2xmlparser');
const lib = require(path.join(__dirname, '/_lib/lib.js'));
/* DATABASE IN MEMORY */
const MMDBReader = require('mmdb-reader');
const dbpath = path.join(__dirname, '/_data/GeoLite2-City.mmdb');
const reader = new MMDBReader(dbpath);
let data;
let geoData;
function getGeoData(req, res, format) {
cleanData();
let q = req.query.q;
if (!q) {
q = lib.getIP(req);
}
if (lib.isValidIP(q)) {
getDataFromRAM(req, res, format, q);
return;
}
if (lib.isValidHostname(q)) {
dns.lookup(q, function (err, ip) {
if (err) {
let text = `${q} is a unknown host, not a valid IP or hostname`;
sendResult(res, 'json', text, 400);
return;
}
if (ip === null || ip === undefined) {
let text = `${q} is a unknown host, not a valid IP or hostname`;
sendResult(res, 'json', text, 400);
return;
}
getDataFromRAM(req, res, format, ip);
return;
});
return;
}
let text = `${q} is a unknown host, not a valid IP or hostname`;
sendResult(res, 'json', text, 400);
}
function getDataFromRAM(req, res, format, ip) {
data = reader.lookup(ip);
if (data === null) { // not in database
sendResult(res, format, geoData, 200);
return;
}
geoData = handleGeoData(ip, data);
sendResult(res, format, geoData, 200);
return;
}
function sendResult(res, format, data, status) {
if (format === 'json') {
res.header('Content-Type', 'application/json');
res.status(status).send(JSON.stringify(data, null, 3));
} else if (format === 'xml') {
res.header('Content-Type', 'text/xml');
res.status(status).send(json2xml.parse('myGeoData', data));
}
cleanData();
return;
}
function handleGeoData(ip, data) {
geoData.ip = ip;
if (data.country !== undefined) {
geoData.country_code = data.country.iso_code || '';
geoData.country_name = data.country.names['en'] || '';
}
if (data.subdivisions !== undefined) {
geoData.region_code = data.subdivisions[0].iso_code || '';
geoData.region_name = data.subdivisions[0].names['en'] || '';
}
if (data.city !== undefined) {
geoData.city = data.city.names['en'] || '';
}
if (data.postal !== undefined) {
geoData.zip_code = data.postal.code || '';
}
if (data.location !== undefined) {
geoData.time_zone = data.location.time_zone || '';
geoData.latitude = data.location.latitude.toFixed(4) || '';
geoData.longitude = data.location.longitude.toFixed(4) || '';
}
return geoData;
}
function cleanData() {
data = {};
geoData = {
'ip': '',
'country_code': '',
'country_name': '',
'region_code': '',
'region_name': '',
'city': '',
'zip_code': '',
'time_zone': '',
'latitude': '',
'longitude': ''
};
}
module.exports = {
getGeoData: getGeoData
};