ip-api.js
1.23 KB
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
const IP2Region = require('ip2region');
const logger = global.yoho.logger;
const _ = require('lodash');
const ipQuery = new IP2Region();
const PROVINCE = ['内蒙古', '新疆', '广西', '宁夏', '西藏'];
function getIsp(ip) {
try {
const result = ipQuery.search(ip);
if (_.endsWith(result.province, '省')) {
const len = result.region.length;
result.province = result.province.substr(0, len - 1);
}
if (_.endsWith(result.province, '市')) {
const len = result.region.length;
result.province = result.province.substr(0, len - 1);
}
for (let p of PROVINCE) {
if (result.province.split(p).length === 2) {
result.province = p;
break;
}
}
if (_.endsWith(result.city, '市')) {
const len = result.city.length;
result.city = result.city.substr(0, len - 1);
}
return {
isp: result.isp,
region: result.province,
city: result.city,
country: result.country
};
} catch (e) {
logger.error('[ip] find ip error [%s]', e);
return {};
}
}
module.exports = {
getIsp
};