-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathworker.js
More file actions
318 lines (268 loc) · 9.21 KB
/
Copy pathworker.js
File metadata and controls
318 lines (268 loc) · 9.21 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import { runDailyShareMaintenance } from "./lib/share/daily-maintenance";
import { runHourlyTrendMaintenance } from "./lib/share/hourly-trend-maintenance";
import { trackShareViewRequest } from "./lib/share/view-stats";
import openNextWorker from "./.cf-build/.open-next/worker.js";
const TREND_ROLLUP_CRON = "30 * * * *";
const DAILY_MAINTENANCE_CRON = "5 16 * * *";
const BANGUMI_IMAGE_PROXY_PATH = "/api/image/bgm";
const BANGUMI_IMAGE_HOSTS = new Set(["lain.bgm.tv", "img.bgm.tv"]);
const ALLOWED_SITE_ROOT = "shatranj.space";
const LOCAL_DEVELOPMENT_HOSTS = new Set(["localhost", "127.0.0.1", "0.0.0.0", "::1", "[::1]"]);
const BANGUMI_IMAGE_CACHE_TTL_SECONDS = 60 * 60 * 24 * 30;
const BANGUMI_IMAGE_CACHE_CONTROL = `public, max-age=${BANGUMI_IMAGE_CACHE_TTL_SECONDS}, s-maxage=${BANGUMI_IMAGE_CACHE_TTL_SECONDS}, immutable`;
const BANGUMI_IMAGE_ERROR_CACHE_CONTROL = "public, max-age=300, s-maxage=300";
const BANGUMI_IMAGE_FORBIDDEN_CACHE_CONTROL = "no-store";
function bindRuntimeEnv(env) {
globalThis.__MY9_CF_ENV = env;
}
function normalizeBangumiImageProxyTarget(value) {
if (typeof value !== "string") return null;
const raw = value.trim();
if (!raw || raw.startsWith("data:") || raw.startsWith("blob:")) {
return null;
}
const normalized = raw.startsWith("//") ? `https:${raw}` : raw;
try {
const parsed = new URL(normalized);
if (!BANGUMI_IMAGE_HOSTS.has(parsed.hostname.toLowerCase())) {
return null;
}
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {
return null;
}
parsed.protocol = "https:";
parsed.hash = "";
return parsed.toString();
} catch {
return null;
}
}
function normalizeHostname(hostname) {
return hostname.trim().toLowerCase().replace(/\.$/, "");
}
function isAllowedSiteHostname(hostname) {
const normalized = normalizeHostname(hostname);
return normalized === ALLOWED_SITE_ROOT || normalized.endsWith(`.${ALLOWED_SITE_ROOT}`);
}
function isLocalDevelopmentHostname(hostname) {
return LOCAL_DEVELOPMENT_HOSTS.has(normalizeHostname(hostname));
}
function parseHeaderHostname(value) {
if (!value) return null;
try {
return new URL(value).hostname;
} catch {
return null;
}
}
function isAllowedSourceHostname(requestHostname, sourceHostname) {
if (isAllowedSiteHostname(sourceHostname)) {
return true;
}
return isLocalDevelopmentHostname(requestHostname) && isLocalDevelopmentHostname(sourceHostname);
}
function isAllowedBangumiImageProxyRequest(request) {
const requestHostname = new URL(request.url).hostname;
const originHostname = parseHeaderHostname(request.headers.get("origin"));
const refererHostname = parseHeaderHostname(request.headers.get("referer"));
if (originHostname) {
return isAllowedSourceHostname(requestHostname, originHostname);
}
if (refererHostname) {
return isAllowedSourceHostname(requestHostname, refererHostname);
}
const fetchSite = request.headers.get("sec-fetch-site")?.toLowerCase();
if (fetchSite === "same-origin") {
return isAllowedSiteHostname(requestHostname) || isLocalDevelopmentHostname(requestHostname);
}
if (fetchSite === "same-site") {
return isAllowedSiteHostname(requestHostname);
}
return isLocalDevelopmentHostname(requestHostname);
}
function getBangumiImageProxyCorsOrigin(request) {
const origin = request.headers.get("origin");
const originHostname = parseHeaderHostname(origin);
if (!origin || !originHostname) return null;
const requestHostname = new URL(request.url).hostname;
if (!isAllowedSourceHostname(requestHostname, originHostname)) {
return null;
}
try {
return new URL(origin).origin;
} catch {
return null;
}
}
function appendBangumiImageProxyAccessHeaders(headers, request) {
const corsOrigin = getBangumiImageProxyCorsOrigin(request);
if (corsOrigin) {
headers.set("Access-Control-Allow-Origin", corsOrigin);
} else {
headers.delete("Access-Control-Allow-Origin");
}
headers.set("Vary", "Origin, Referer, Sec-Fetch-Site");
}
function withBangumiImageProxyAccessHeaders(response, request) {
const headers = new Headers(response.headers);
appendBangumiImageProxyAccessHeaders(headers, request);
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers,
});
}
function buildBangumiImageProxyHeaders(upstream) {
const headers = new Headers();
const contentType = upstream.headers.get("content-type");
const etag = upstream.headers.get("etag");
const lastModified = upstream.headers.get("last-modified");
if (contentType) headers.set("Content-Type", contentType);
if (etag) headers.set("ETag", etag);
if (lastModified) headers.set("Last-Modified", lastModified);
headers.set(
"Cache-Control",
upstream.ok ? BANGUMI_IMAGE_CACHE_CONTROL : BANGUMI_IMAGE_ERROR_CACHE_CONTROL
);
headers.set(
"CDN-Cache-Control",
upstream.ok ? BANGUMI_IMAGE_CACHE_CONTROL : BANGUMI_IMAGE_ERROR_CACHE_CONTROL
);
headers.set("X-Content-Type-Options", "nosniff");
headers.set("X-My9-Image-Proxy", "bangumi");
return headers;
}
function toBangumiImageProxyCacheRequest(requestUrl, targetUrl) {
const cacheUrl = new URL(requestUrl);
cacheUrl.search = "";
cacheUrl.searchParams.set("url", targetUrl);
return new Request(cacheUrl.toString(), { method: "GET" });
}
async function handleBangumiImageProxy(request, ctx) {
const isAllowedRequest = isAllowedBangumiImageProxyRequest(request);
if (request.method === "OPTIONS") {
if (!isAllowedRequest) {
return new Response("Forbidden", {
status: 403,
headers: {
"Cache-Control": BANGUMI_IMAGE_FORBIDDEN_CACHE_CONTROL,
"Vary": "Origin, Referer, Sec-Fetch-Site",
},
});
}
const corsOrigin = getBangumiImageProxyCorsOrigin(request);
return new Response(null, {
status: 204,
headers: {
...(corsOrigin ? { "Access-Control-Allow-Origin": corsOrigin } : {}),
"Access-Control-Allow-Methods": "GET, HEAD, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type",
"Cache-Control": "no-store",
Vary: "Origin, Referer, Sec-Fetch-Site",
},
});
}
if (request.method !== "GET" && request.method !== "HEAD") {
return new Response("Method Not Allowed", {
status: 405,
headers: {
Allow: "GET, HEAD, OPTIONS",
"Cache-Control": "no-store",
},
});
}
if (!isAllowedRequest) {
return new Response("Forbidden", {
status: 403,
headers: {
"Cache-Control": BANGUMI_IMAGE_FORBIDDEN_CACHE_CONTROL,
"Vary": "Origin, Referer, Sec-Fetch-Site",
},
});
}
const requestUrl = new URL(request.url);
const targetUrl = normalizeBangumiImageProxyTarget(requestUrl.searchParams.get("url"));
if (!targetUrl) {
return new Response("Invalid Bangumi image URL", {
status: 400,
headers: {
"Cache-Control": "no-store",
},
});
}
const cache = typeof caches !== "undefined" ? caches.default : null;
const cacheRequest = toBangumiImageProxyCacheRequest(request.url, targetUrl);
if (request.method === "GET" && cache) {
const cached = await cache.match(cacheRequest);
if (cached) return withBangumiImageProxyAccessHeaders(cached, request);
}
try {
const upstream = await fetch(targetUrl, {
method: request.method === "HEAD" ? "HEAD" : "GET",
cf: {
cacheEverything: true,
cacheTtlByStatus: {
"200-299": BANGUMI_IMAGE_CACHE_TTL_SECONDS,
"300-399": 3600,
"404": 300,
"500-599": 0,
},
},
});
const response = new Response(request.method === "HEAD" ? null : upstream.body, {
status: upstream.status,
statusText: upstream.statusText,
headers: buildBangumiImageProxyHeaders(upstream),
});
if (request.method === "GET" && upstream.ok && cache) {
ctx.waitUntil(cache.put(cacheRequest, response.clone()));
}
return withBangumiImageProxyAccessHeaders(response, request);
} catch {
return new Response("Bangumi image fetch failed", {
status: 502,
headers: {
"Cache-Control": "no-store",
},
});
}
}
const worker = {
fetch(request, env, ctx) {
bindRuntimeEnv(env);
const requestUrl = new URL(request.url);
if (requestUrl.pathname === BANGUMI_IMAGE_PROXY_PATH) {
return handleBangumiImageProxy(request, ctx);
}
trackShareViewRequest(request, env.MY9_SHARE_VIEW_ANALYTICS ?? null);
return openNextWorker.fetch(request, env, ctx);
},
scheduled(controller, env, ctx) {
bindRuntimeEnv(env);
if (controller.cron === TREND_ROLLUP_CRON) {
ctx.waitUntil(
runHourlyTrendMaintenance({
logLabel: `[trend-cron:${controller.cron}]`,
}).catch((error) => {
console.error("[trend-cron] failed", error);
throw error;
})
);
return;
}
if (controller.cron !== DAILY_MAINTENANCE_CRON) {
console.warn(`[scheduled] unsupported cron ${controller.cron}`);
return;
}
ctx.waitUntil(
runDailyShareMaintenance({
env,
logLabel: `[daily-cron:${controller.cron}]`,
}).catch((error) => {
console.error("[daily-cron] failed", error);
throw error;
})
);
},
};
export default worker;