forked from jason5ng32/MyIP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeycdn.js
68 lines (59 loc) · 1.99 KB
/
keycdn.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
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' });
}
// 构建请求 keycdn.com 的 URL
const url = new URL(`https://tools.keycdn.com/geo.json?host=${ipAddress}`);
// 设置请求选项,包括 User-Agent
const options = {
hostname: url.hostname,
path: url.pathname + url.search,
headers: {
'User-Agent': 'keycdn-tools:' + process.env.KEYCDN_USER_AGENT
}
};
get(options, apiRes => {
let data = '';
apiRes.on('data', chunk => data += chunk);
apiRes.on('end', () => {
try {
const originalJson = JSON.parse(data);
const modifiedJson = modifyJsonForKeyCDN(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 modifyJsonForKeyCDN(json) {
const { data: { geo: { ip, city, region_name, country_name, country_code, latitude, longitude, isp, asn } } } = json;
return {
ip,
city,
region: region_name ? region_name : city,
country: country_code,
country_name,
country_code,
latitude,
longitude,
asn: "AS" + asn,
org: isp
};
}