-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptikai.js
174 lines (154 loc) · 6.72 KB
/
optikai.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
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
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);
const prefix = "/kfile/";
// Validate the file name
const fileName = url.pathname.substring(prefix.length);
if (!fileName.includes("OptiFine_") || !fileName.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"
}
});
}
// Cache handling
let cache = caches.default;
let cachedResponse = await cache.match(request);
if (cachedResponse) {
return cachedResponse;
}
const optifineUrl = `https://livzmc.net/optikai/downloadx?f=${fileName}`;
try {
const response = await fetch(optifineUrl);
const text = await response.text();
const downloadPattern = /<a rel="nofollow" href="([^"]+)" class="hover:underline text-lg">Download<\/a>/;
const match = text.match(downloadPattern);
if (!match) {
// Fetch version list from BMCLAPI2 if no download link is found
/*
const bmclapiUrl = "https://bmclapi2.bangbang93.com/optifine/versionList";
const bmclApiResponse = await fetch(bmclapiUrl);
const bmclApiJson = await bmclApiResponse.json();
const matchingModule = bmclApiJson.find(module => module.filename === fileName);
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;
const bmclApiRedirectUrl = `https://bmclapi2.bangbang93.com/optifine/${mcversion}/${type}/${patch}`;
// Create and cache the response
const cacheHeaders = {
"Cache-Control": "public, max-age=0, s-maxage=300"
};
const bmclApiRedirectResponse = new Response(null, {
"status": 302,
"headers": {
...cacheHeaders,
"Location": bmclApiRedirectUrl
}
});
cache.put(request, bmclApiRedirectResponse.clone());
return bmclApiRedirectResponse;
*/
return new Response(JSON.stringify({
"code": 404,
"message": "File not found."
}), {
"status": 404,
"headers": {
"Content-Type": "application/json"
}
});
}
// Validate the download link
const downloadLink = match[1];
const downloadResponse = await fetch(downloadLink, { method: 'HEAD' });
if (downloadResponse.status !== 200 || !downloadResponse.headers.has("Content-Disposition")) {
// Redirect to BMCLAPI2 if the download link is not valid
/*
const bmclapiUrl = "https://bmclapi2.bangbang93.com/optifine/versionList";
const bmclApiResponse = await fetch(bmclapiUrl);
const bmclApiJson = await bmclApiResponse.json();
const matchingModule = bmclApiJson.find(module => module.filename === fileName);
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;
const bmclApiRedirectUrl = `https://bmclapi2.bangbang93.com/optifine/${mcversion}/${type}/${patch}`;
// Create and cache the response
const cacheHeaders = {
"Cache-Control": "public, max-age=0, s-maxage=300"
};
const bmclApiRedirectResponse = new Response(null, {
"status": 302,
"headers": {
...cacheHeaders,
"Location": bmclApiRedirectUrl
}
});
cache.put(request, bmclApiRedirectResponse.clone());
return bmclApiRedirectResponse;
*/
return new Response(JSON.stringify({
"code": 404,
"message": "File not found."
}), {
"status": 404,
"headers": {
"Content-Type": "application/json"
}
});
}
// If the download link is valid, redirect to it
const cacheHeaders = {
"Cache-Control": "public, max-age=0, s-maxage=300"
};
const redirectResponse = new Response(null, {
"status": 302,
"headers": {
...cacheHeaders,
"Location": downloadLink
}
});
cache.put(request, redirectResponse.clone());
return redirectResponse;
} catch (error) {
return new Response(JSON.stringify({
"code": 500,
"message": "Internal server error."
}), {
"status": 500,
"headers": {
"Content-Type": "application/json"
}
});
}
}
};