-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
60 lines (60 loc) · 2.2 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
59
60
addEventListener('fetch', event => {
return event.respondWith(handle(event));
});
/*
* @param event
* @return Response Object
* @author ZiAzusa
*/
async function handle(event) {
// 添加跨域和缓存控制响应头
const resHdrs = new Headers({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Headers': 'Content-Type,Content-Length,Accept-Encoding,X-Requested-with,Origin',
'Access-Control-Allow-Methods': 'GET,HEAD,OPTIONS',
'Cache-Control': 'public, max-age=31536000'
});
const cache = caches.default;
const url = new URL(event.request.url);
// 处理新浪图床链接
let sinaUrl = String(url.pathname).slice(1);
if (sinaUrl.startsWith("http")) {
sinaUrl = sinaUrl.replace(/https:(\/)*/,'').replace(/http:(\/)*/, '');
}
const newUrl = new URL("https://" + sinaUrl);
if (!newUrl.hostname.includes("sinaimg.cn")) {
return new Response('404 Not Found', {status: 404});
}
// 检查Cloudflare缓存
let response = await cache.match(newUrl);
if (response) {
resHdrs.set('Content-Type', response.headers.get("Content-Type"));
resHdrs.set('X-Worker-Cache', "true");
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: resHdrs
});
}
// 回源新浪图床
response = await fetch(newUrl, {
headers: {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0 Win64 x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.41",
"Referer": "https://www.weibo.com/"
},
cf: {polish: "lossless"}
});
if (!response.ok || !response.headers.get("Content-Type").includes("image")) {
return new Response('404 Not Found', {status: 404});
}
resHdrs.set('Content-Type', response.headers.get("Content-Type"));
response = new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: resHdrs
});
// 写入Cloudflare缓存
event.waitUntil(cache.put(newUrl, response.clone()));
return response;
}