forked from jason5ng32/MyIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathipapiis.js
63 lines (55 loc) · 2.08 KB
/
ipapiis.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
import { get } from 'https';
import { isValidIP } from '../common/valid-ip.js';
import { refererCheck } from '../common/referer-check.js';
export default (req, res) => {
// 限制只能从指定域名访问
const referer = req.headers.referer;
if (!refererCheck(referer)) {
return res.status(403).json({ error: referer ? 'Access denied' : 'What are you doing?' });
}
// 从请求中获取 IP 地址
const ipAddress = req.query.ip;
if (!ipAddress) {
return res.status(400).json({ error: 'No IP address provided' });
}
// 检查 IP 地址是否合法
if (!isValidIP(ipAddress)) {
return res.status(400).json({ error: 'Invalid IP address' });
}
const keys = (process.env.IPAPIIS_API_KEY).split(',');
const key = keys[Math.floor(Math.random() * keys.length)];
const url = `https://api.ipapi.is?q=${ipAddress}&key=${key}`;
get(url, apiRes => {
let data = '';
apiRes.on('data', chunk => data += chunk);
apiRes.on('end', () => {
try {
const originalJson = JSON.parse(data);
const modifiedJson = modifyJsonForIPAPI(originalJson);
res.json(modifiedJson);
} catch (e) {
res.status(500).json({ error: 'Error parsing JSON' });
}
});
}).on('error', (e) => {
res.status(500).json({ error: e.message });
});
};
function modifyJsonForIPAPI(json) {
let asn = json.asn || {};
const { ip, location, is_datacenter, is_proxy, is_vpn, is_tor } = json;
return {
ip: ip,
city: location.city || 'N/A',
region: location.state || 'N/A',
country: location.country_code || 'N/A',
country_name: location.country || 'N/A',
country_code: location.country_code || 'N/A',
latitude: location.latitude || 'N/A',
longitude: location.longitude || 'N/A',
asn: asn.asn === undefined ? 'N/A' : 'AS' + asn.asn,
org: asn.org || 'N/A',
isHosting: is_datacenter || false,
isProxy: is_proxy || is_vpn || is_tor || false
};
}