forked from jason5ng32/MyIP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.js
More file actions
80 lines (68 loc) · 3.1 KB
/
map.js
File metadata and controls
80 lines (68 loc) · 3.1 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
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
import { get } from 'https';
import { refererCheck } from '../common/referer-check.js';
// 验证请求合法性
function isValidRequest(req) {
const isLatitudeValid = /^-?\d+(\.\d+)?$/.test(req.query.latitude);
const isLongitudeValid = /^-?\d+(\.\d+)?$/.test(req.query.longitude);
const isLanguageValid = /^[a-z]{2}$/.test(req.query.language);
if (!isLatitudeValid || !isLongitudeValid || !isLanguageValid) {
return false;
} else {
return true;
}
}
// 定义白天模式和黑暗模式样式字符串
const styles = {
Dark: [
"feature:all|element:geometry.fill|color:0x242f3e",
"feature:all|element:labels.text.stroke|color:0x242f3e",
"feature:all|element:labels.text.fill|color:0x746855",
"feature:administrative.locality|element:labels.text.fill|color:0xd59563",
"feature:poi|element:labels.text.fill|color:0xd59563",
"feature:poi.park|element:geometry|color:0x263c3f",
"feature:poi.park|element:labels.text.fill|color:0x6b9a76",
"feature:road|element:geometry|color:0x38414e",
"feature:road|element:geometry.stroke|color:0x212a37",
"feature:road|element:labels.text.fill|color:0x9ca5b3",
"feature:road.highway|element:geometry|color:0x746855",
"feature:road.highway|element:geometry.stroke|color:0x1f2835",
"feature:road.highway|element:labels.text.fill|color:0xf3d19c",
"feature:transit|element:geometry|color:0x2f3948",
"feature:transit.station|element:labels.text.fill|color:0xd59563",
"feature:water|element:geometry|color:0x17263c",
"feature:water|element:labels.text.fill|color:0x515c6d",
"feature:all|element:labels.text.stroke|color:0x17263c"
]
};
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?' });
}
// 检查请求是否合法
if (!isValidRequest(req)) {
return res.status(400).json({ error: 'Invalid request' });
}
// 使用 req.query 获取参数
const { latitude, longitude, language, CanvasMode } = req.query;
if (!latitude || !longitude || !language) {
return res.status(400).json({ error: 'Missing latitude, longitude, or language' });
}
const mapSize = '500x400';
const fmt = 'jpg';
const scale = 2;
const zoom = 3;
const apiKeys = (process.env.GOOGLE_MAP_API_KEY || '').split(',');
const apiKey = apiKeys[Math.floor(Math.random() * apiKeys.length)];
let styleParam = '';
if (CanvasMode === 'Dark') {
styleParam = styles.Dark.join('&style=');
}
const url = `https://maps.googleapis.com/maps/api/staticmap?center=${latitude},${longitude}&markers=color:blue%7C${latitude},${longitude}&scale=${scale}&zoom=${zoom}&maptype=roadmap&language=${language}&format=${fmt}&size=${mapSize}&style=${styleParam}&key=${apiKey}`;
get(url, apiRes => {
apiRes.pipe(res);
}).on('error', (e) => {
res.status(500).json({ error: e.message });
});
};