-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
98 lines (88 loc) · 4.45 KB
/
main.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
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
export default {
/**
* @param {Request} request
* @param {Env} env
* @param {ExecutionContext} ctx
* @returns {Promise<Response>}
*/
async fetch(request, env, ctx) {
const url = new URL(request.url),
prefix = "/file/";
if (!(url.pathname.includes("OptiFine_") || url.pathname.endsWith(".jar")) || !url.pathname.startsWith(prefix) || url.pathname.indexOf("/", prefix.length) !== -1) {
return new Response(JSON.stringify({
"code": 404,
"message": "File not found."
}), {
"status": 404,
"headers": {
"Content-Type": "application/json"
}
});
}
let cache = caches.default;
let cachedResponse = await cache.match(request);
if (cachedResponse) {
return cachedResponse;
}
return fetch("https://optifine.net/adloadx?f=" + url.pathname.substring(prefix.length))
.then(response => response.text())
.then(text => {
let start = text.indexOf("href='", text.indexOf("<div class=\"downloadButton\">")) + "href='".length;
let redirectUrl = "https://optifine.net/" + text.substring(start, text.indexOf("'", start));
return fetch(redirectUrl)
.then(async response => {
if (response.status !== 200 || !response.headers.has("Content-Disposition")) {
// Fetch information from BMCLAPI2 API
const bmclapiUrl = "https://bmclapi2.bangbang93.com/optifine/versionList";
const bmclApiResponse = await fetch(bmclapiUrl);
const bmclApiJson = await bmclApiResponse.json();
// Check if the requested file exists in the BMCLAPI2 response
const requestedFileName = url.pathname.substring(prefix.length);
const matchingModule = bmclApiJson.find(module => module.filename === requestedFileName);
if (!matchingModule) {
return new Response(JSON.stringify({
"code": 404,
"message": "File not found."
}), {
"status": 404,
"headers": {
"Content-Type": "application/json"
}
});
}
// Extract necessary information from BMCLAPI2 response
const { mcversion, patch, type } = matchingModule;
// Redirect to BMCLAPI2 URL
const bmclApiRedirectUrl = `https://bmclapi2.bangbang93.com/optifine/${mcversion}/${type}/${patch}`;
let cacheHeaders = {
"Cache-Control": "public, max-age=0, s-maxage=300"
};
let cachedResponse = new Response(response.body, {
"status": 302,
"headers": {
...response.headers,
...cacheHeaders,
"Location": bmclApiRedirectUrl
}
});
cache.put(request, cachedResponse.clone());
return cachedResponse;
}
// Cache the response for 300 seconds
let cacheHeaders = {
"Cache-Control": "public, max-age=0, s-maxage=300"
};
let cachedResponse = new Response(response.body, {
"status": 302,
"headers": {
...response.headers,
...cacheHeaders,
"Location": redirectUrl
}
});
cache.put(request, cachedResponse.clone());
return cachedResponse;
});
});
}
};