-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
58 lines (49 loc) · 1.69 KB
/
index.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
const cors = require('cors');
const helmet = require('helmet');
const express = require('express');
const bodyParser = require('body-parser');
const maxmind = require('./lib/maxmind-utility');
const countryList = require('./lib/countries');
const app = express();
const ipValidation = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
app.use(cors());
app.use(helmet());
app.use(bodyParser.json());
app.disable('x-powered-by');
app.use('/assets', express.static('resources/flags'));
const getIp = async ip => {
if (!ipValidation.test(ip)) {
return;
}
const reader = await maxmind;
return reader.get(ip);
}
app.options('/*', (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
res.send(200);
});
app.post('/ip', async (req, res) => {
const ipList = req.body.ip;
try {
if (!Array.isArray(ipList)) {
res.sendStatus(400);
}
const resolvedIPList = await Promise.all(ipList.map(ip => getIp(ip) || ''));
const flagList = resolvedIPList.map(ip => {
const code = ip?.country?.iso_code?.toLowerCase();
if (!code) {
return { imagePath: 'assets/unknown.png', locationName: '未知' };
}
const imagePath = `assets/${code}.png`;
const locationName = countryList[code];
return { imagePath, locationName };
});
res.json(flagList);
} catch (ex) {
console.error(ex);
res.sendStatus(500);
}
});
app.listen(9977, async () => console.log('App listening on port 9977!'));