-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_worker.js
More file actions
27 lines (22 loc) · 758 Bytes
/
_worker.js
File metadata and controls
27 lines (22 loc) · 758 Bytes
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
// noinspection JSUnresolvedReference
export default {
async fetch(request, env) {
const url = new URL(request.url);
if (url.pathname === '/ip') {
const ip = request.headers.get('CF-Connecting-IP') || 'unknown';
const cf = request.cf || {};
const city = cf.city || '';
const region = cf.region || '';
const country = cf.country || '';
const parts = [city, region, country].filter(Boolean);
const location = parts.length > 0 ? parts.join(', ') : 'unknown';
return new Response(JSON.stringify({ ip, location }), {
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*',
},
});
}
return env.ASSETS.fetch(request);
},
};