forked from PenguiLoader/MC-SRV-DL-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
319 lines (244 loc) · 9.91 KB
/
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
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
319
/*
Made by Aleksander Wegrzyn under the Code Credit License.
Modified by setup.md to work in Cloudflare Workers
*/
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
})
const VALID_SOFTWARES = ["vanilla", "bedrock", "paper", "waterfall", "velocity", "folia", "purpur", "mohistmc"];
async function handleRequest(request) {
const { pathname, searchParams } = new URL(request.url);
if (pathname === "/") {
return Response.redirect("https://setup.md/service/jar-api", 301);
}
if (pathname.startsWith("/download/")) {
const [, , software, version, build] = pathname.split("/");
if (!VALID_SOFTWARES.includes(software)) {
return new Response(JSON.stringify({ error: true, message: "Invalid software type." }), { status: 400 });
}
let versionToUse;
switch (version) {
case "latest":
switch (software) {
case "vanilla":
versionToUse = await getLatestVanillaVersion();
break;
case "bedrock":
versionToUse = await getLatestVanillaVersion();
break;
case "purpur":
versionToUse = await getLatestPurpurVersion();
break;
case "paper":
versionToUse = await getLatestPaperVersion();
break;
case "folia":
versionToUse = await getLatestFoliaVersion();
break;
case "waterfall":
versionToUse = await getLatestWaterfallVersion();
break;
case "velocity":
versionToUse = await getLatestVelocityVersion();
break;
case "mohistmc":
versionToUse = await getLatestMohistVersion();
break;
}
break;
default:
versionToUse = version;
}
switch (software) {
case "vanilla":
return handleVanilla(versionToUse);
case "bedrock":
return handleBedrock(versionToUse);
case "purpur":
return handlePurpur(versionToUse, build);
case "paper":
return handlePaper(versionToUse, build);
case "waterfall":
return handleWaterfall(versionToUse, build);
case "folia":
return handleFolia(versionToUse, build);
case "velocity":
return handleVelocity(versionToUse, build);
case "mohistmc":
return handleMohist(versionToUse);
}
}
return new Response(null, { status: 404 });
}
/* VANILLA */
async function getVersionManifest() {
const response = await fetch("https://launchermeta.mojang.com/mc/game/version_manifest.json");
return await response.json();
}
async function getLatestVanillaVersion() {
const versionManifest = await getVersionManifest();
return versionManifest.latest.release;
}
/* BEDROCK
This is still a WIP, due to worker limitations
*/
/* PURPUR */
async function getLatestPurpurVersion() {
const response = await fetch("https://api.purpurmc.org/v2/purpur/");
const data = await response.json();
return data.versions[data.versions.length - 1];
}
/* PAPER */
async function getLatestPaperVersion() {
const response = await fetch("https://api.papermc.io/v2/projects/paper");
const data = await response.json();
return data.versions[data.versions.length - 1];
}
/* FOLIA */
async function getLatestFoliaVersion() {
const response = await fetch("https://api.papermc.io/v2/projects/folia");
const data = await response.json();
return data.versions[data.versions.length - 1];
}
/* WATERFALL */
async function getLatestWaterfallVersion() {
const response = await fetch("https://api.papermc.io/v2/projects/waterfall");
const data = await response.json();
return data.versions[data.versions.length - 1];
}
/* VELOCITY */
async function getLatestVelocityVersion() {
const response = await fetch("https://api.papermc.io/v2/projects/velocity");
const data = await response.json();
return data.versions[data.versions.length - 1];
}
/* MOHIST */
async function getLatestMohistVersion() {
const response = await fetch("https://mohistmc.com/api/versions");
const versions = await response.json();
return versions[versions.length - 1];
}
/* DOWNLOAD JAR */
/* VANILLA */
async function handleVanilla(version) {
const versionManifest = await getVersionManifest();
const vanillaVersion = versionManifest.versions.find(v => v.id === version);
if (!vanillaVersion) {
return new Response(JSON.stringify({ error: true, message: "Version not found." }), { status: 400 });
}
const response = await fetch(vanillaVersion.url);
const data = await response.json();
return Response.redirect(data.downloads.server.url, 302);
}
/* BEDROCK
async function handleBedrock(version) {
const bedrockData = await fetch(`https://minecraft.azureedge.net/bin-linux/bedrock-server-${version}.zip`);
if (bedrockData.error) {
return new Response(JSON.stringify({ error: true, message: bedrockData.error }), { status: 400 });
}
return Response.redirect(bedrockData.url, 302);
}
*/
async function handleBedrock(version) {
if (version.toLowerCase() === 'latest') {
return new Response(JSON.stringify({ error: true, message: 'You cannot currently specify "latest" as a version number for Bedrock. Please specify an actual version number.' }), { status: 400 });
} else {
const bedrockDataResponse = await fetch(`https://minecraft.azureedge.net/bin-linux/bedrock-server-${version}.zip`);
if (!bedrockDataResponse.ok) {
return new Response(JSON.stringify({ error: true, message: 'Please specify a valid build number for Bedrock edition.' }), { status: 404 });
}
return Response.redirect(bedrockDataResponse.url, 302);
}
}
/* PURPUR */
async function handlePurpur(version, build) {
if (!build) {
return new Response(JSON.stringify({ error: true, message: "Build parameter is required for Purpur." }), { status: 400 });
}
const purpurData = await fetch(`https://api.purpurmc.org/v2/purpur/${version}/${build}`).then(res => res.json());;
if (purpurData.error) {
return new Response(JSON.stringify({ error: true, message: purpurData.error }), { status: 400 });
}
const purpurRedir = `https://api.purpurmc.org/v2/purpur/${version}/${build}/download`;
return Response.redirect(purpurRedir, 302);
}
/* PAPER */
async function handlePaper(version, build) {
const paperBuilds = await fetch(`https://api.papermc.io/v2/projects/paper/versions/${version}/builds`).then(res => res.json());
if (paperBuilds.error) {
return new Response(JSON.stringify({ error: true, message: paperBuilds.error }), { status: 400 });
}
let finalBuild;
if (build === "latest") {
finalBuild = paperBuilds.builds[paperBuilds.builds.length - 1];
} else {
finalBuild = paperBuilds.builds.find(b => b.build === build);
if (!finalBuild) {
return new Response(JSON.stringify({ error: true, message: "Invalid build." }), { status: 400 });
}
}
let filename = finalBuild.downloads.application.name;
return Response.redirect(`https://api.papermc.io/v2/projects/paper/versions/${version}/builds/${finalBuild.build}/downloads/${filename}`, 302);
}
/* FOLIA */
async function handleFolia(version, build) {
const foliaBuilds = await fetch(`https://api.papermc.io/v2/projects/folia/versions/${version}/builds`).then(res => res.json());
if (foliaBuilds.error) {
return new Response(JSON.stringify({ error: true, message: foliaBuilds.error }), { status: 400 });
}
let finalBuild;
if (build === "latest") {
finalBuild = foliaBuilds.builds[foliaBuilds.builds.length - 1];
} else {
finalBuild = foliaBuilds.builds.find(b => b.build === build);
if (!finalBuild) {
return new Response(JSON.stringify({ error: true, message: "Invalid build." }), { status: 400 });
}
}
let filename = finalBuild.downloads.application.name;
return Response.redirect(`https://api.papermc.io/v2/projects/folia/versions/${version}/builds/${finalBuild.build}/downloads/${filename}`, 302);
}
/* WATERFALL */
async function handleWaterfall(version, build) {
const waterfallBuilds = await fetch(`https://api.papermc.io/v2/projects/waterfall/versions/${version}/builds`).then(res => res.json());
if (waterfallBuilds.error) {
return new Response(JSON.stringify({ error: true, message: waterfallBuilds.error }), { status: 400 });
}
let finalBuild;
if (build === "latest") {
finalBuild = waterfallBuilds.builds[waterfallBuilds.builds.length - 1];
} else {
finalBuild = waterfallBuilds.builds.find(b => b.build === build);
if (!finalBuild) {
return new Response(JSON.stringify({ error: true, message: "Invalid build." }), { status: 400 });
}
}
let filename = finalBuild.downloads.application.name;
return Response.redirect(`https://api.papermc.io/v2/projects/waterfall/versions/${version}/builds/${finalBuild.build}/downloads/${filename}`, 302);
}
/* VELOCITY */
async function handleVelocity(version, build) {
const velocityBuilds = await fetch(`https://api.papermc.io/v2/projects/velocity/versions/${version}/builds`).then(res => res.json());
if (velocityBuilds.error) {
return new Response(JSON.stringify({ error: true, message: velocityBuilds.error }), { status: 400 });
}
let finalBuild;
if (build === "latest") {
finalBuild = velocityBuilds.builds[velocityBuilds.builds.length - 1];
} else {
finalBuild = velocityBuilds.builds.find(b => b.build === build);
if (!finalBuild) {
return new Response(JSON.stringify({ error: true, message: "Invalid build." }), { status: 400 });
}
}
let filename = finalBuild.downloads.application.name;
return Response.redirect(`https://api.papermc.io/v2/projects/velocity/versions/${version}/builds/${finalBuild.build}/downloads/${filename}`, 302);
}
/* MOHIST */
async function handleMohist(version) {
const mohistData = await fetch(`https://mohistmc.com/api/${version}/latest`).then(res => res.json());
if (mohistData.error) {
return new Response(JSON.stringify({ error: true, message: mohistData.error }), { status: 400 });
}
return Response.redirect(mohistData.url, 302);
}