forked from 9channel/dd-danmaku
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cf_worker.js
33 lines (30 loc) · 1.06 KB
/
cf_worker.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
const hostlist = { 'api.dandanplay.net': null };
async function handleRequest(request) {
const urlObj = new URL(request.url);
let url = urlObj.href.replace(urlObj.origin + '/cors/', '').trim();
if (0 !== url.indexOf('https://') && 0 === url.indexOf('https:')) {
url = url.replace('https:/', 'https://');
} else if (0 !== url.indexOf('http://') && 0 === url.indexOf('http:')) {
url = url.replace('http:/', 'http://');
}
let tUrlObj = new URL(url);
if (!(tUrlObj.hostname in hostlist)) {
return Forbidden(tUrlObj);
}
let response = await fetch(url, {
headers: request.headers,
body: request.body,
method: request.method,
});
response = new Response(await response.body, response);
response.headers.set('Access-Control-Allow-Origin', '*');
return response;
}
function Forbidden(url) {
return new Response(`Hostname ${url.hostname} not allowed.`, {
status: 403,
});
}
addEventListener('fetch', (event) => {
return event.respondWith(handleRequest(event.request));
});