diff --git a/src/normaljar.ts b/src/normaljar.ts index 2f4b541..4f68396 100644 --- a/src/normaljar.ts +++ b/src/normaljar.ts @@ -1,12 +1,12 @@ import { RequestHandler } from "express"; -import { fetchDownloadUrl, getFetchedData, getRedirectUrl } from "./util"; +import { fetchDownloadUrl, getFetchedData } from "./util"; const normaljar: RequestHandler = async (req, res) => { const { id, file } = res.locals const response = await fetchDownloadUrl(id, file) if (response.ok) { - return res.redirect(getRedirectUrl(await getFetchedData(response))) + return res.redirect(await getFetchedData(response)) } else { return res.status(response.status).send(response.statusText) } diff --git a/src/testing.ts b/src/testing.ts index a1315c9..81996fb 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -1,7 +1,7 @@ import escapeHTML from 'escape-html'; import { RequestHandler } from 'express'; import createClassifierMap from './classifiermap'; -import { authFetch, getDownloadUrl, getFetchedData, getRedirectUrl } from './util'; +import { authFetch, getDownloadUrl, getFetchedData } from './util'; const testing: RequestHandler = async (req, res) => { diff --git a/src/util.ts b/src/util.ts index db1519a..39e1d19 100644 --- a/src/util.ts +++ b/src/util.ts @@ -9,40 +9,3 @@ export const getFetchedData = async (response: Response) => { const json = await response.json() return json.data } - -//Gets the redirect url for the given url. An example of this download url would be: https://edge.forgecdn.net/files/2724/420/jei_1.12.2-4.15.0.281.jar -// -//Due to an issue with apache's http client which gradle uses, some characters will be decoded, but not encoded. -//This in junction with curseforge's media server needing the correct encoding means redirecting to the media server won't always work. -//A fix for this is to redirect to `/download-binary/...`, instead of `https://media.forgecdn.net/files/...`. Doing this means I have to encode the -//Jar name twice (hopfully) meaning that the apache issues don't occur, as the special characters won't show up in the decoding. -//The `/download-binary/` is just a reverse proxy to the forge media server, however I'm not too sure that it doesn't use up bandwidth when downloading the jar, so I only use this when I need to. -// -//If the file name (jei_1.12.2-4.15.0.281.jar) contains any problematic characters then return a redirect to `/download-binary/`, otherwise it redirects to curseforge's media server. -// -// -//2022-01-02T06:14:58.021+0000 [DEBUG] [org.gradle.internal.resource.transport.http.HttpClientConfigurer$DowngradeProtectingRedirectStrategy] Redirect requested to location 'https://media.forgecdn.net/files/3335/93/BetterFoliage-2.6.5%2B368b50a-Fabric-1.16.5.jar' -//2022-01-02T06:14:58.022+0000 [DEBUG] [org.apache.http.impl.execchain.RedirectExec] Resetting target auth state -//2022-01-02T06:14:58.022+0000 [DEBUG] [org.apache.http.impl.execchain.RedirectExec] Redirecting to 'https://media.forgecdn.net/files/3335/93/BetterFoliage-2.6.5+368b50a-Fabric-1.16.5.jar' via {s}->https://media.forgecdn.net:443 -// -//Decoding is done at URLEncodedUtils#urlDecode -//Call stack is below (line numbers are the line of the next call): -//DefaultRedirectStrategy#getLocationURI L 97 -//URIUtils#normalizeSyntax L 196 -//new URIBuilder --> URIBuilder#digestURI --> URIBuilder#parsePath L 157 -//URLEncodedUtils#parsePathSegments L 236 -//URLEncodedUtils#urlDecode -export const getRedirectUrl = (url: string) => { - - //The file name will be the last one. The reason I pop to get the raw file name instead of just doing `split[6]`, is as `split` is joined back if there are no problematic chars. - var split = url.split('\/') - var rawFileName = split.pop() - var fileName = encodeURIComponent(rawFileName) - - // If there are problematic chars then redirect internally. - if (rawFileName.includes("+")) { - return `/download-binary/${split[4]}/${split[5]}/${encodeURIComponent(fileName)}` //We have to encode it twice for this to work with gradle - } else { - return split.join('/') + '/' + fileName - } -} \ No newline at end of file diff --git a/tests/download.test.ts b/tests/download.test.ts index ddf2a18..ad238b6 100644 --- a/tests/download.test.ts +++ b/tests/download.test.ts @@ -14,19 +14,6 @@ describe('Normal Download URL', () => { expect(res.status).toStrictEqual(302) expect(res.headers['location']).toStrictEqual("https://edge.forgecdn.net/files/2724/420/jei_1.12.2-4.15.0.281.jar") }) - - test('Normal Jar w/ problamatic chars should be correct', async () => { - //curse.maven:better-foliage-228529:3335093 - const res = await requestWithSupertest.get(downloadUrl('better-foliage', '228529', '3335093', '.jar')) - expect(res.status).toStrictEqual(302) - expect(res.headers['location']).toStrictEqual("/download-binary/3335/93/BetterFoliage-2.6.5%252B368b50a-Fabric-1.16.5.jar") - }) - - test("Normal Jar that doesn't exist should return 404", async () => { - //curse.maven:invalid-12345:12345 - const res = await requestWithSupertest.get(downloadUrl('invalid', '12345', '54321', '.jar')) - expect(res.status).toStrictEqual(404) - }) }) describe('Direct Download URL', () => { @@ -67,13 +54,6 @@ describe('Classifier Download URL', () => { expect(res.headers['location']).toStrictEqual("https://edge.forgecdn.net/files/2452/538/JustEnoughResources-1.12-0.8.2.20-api.jar") }) - test('Classifier Jar w/ problamatic chars should be correct', async () => { - //curse.maven:pehkui-319596:3577084-sources-dev-3577085:sources-dev - const res = await requestWithSupertest.get(downloadUrl('pehkui', '319596', '3577084-sources-dev-3577085', '-sources-dev.jar')) - expect(res.status).toStrictEqual(302) - expect(res.headers['location']).toStrictEqual("/download-binary/3577/85/Pehkui-3.1.0%252B1.18.1-forge-sources-dev.jar") - }) - test("Classifier Jar where original jar doesn't exist should return 404", async () => { //curse.maven:invalid-12345:54321-sources-54322:sources const res = await requestWithSupertest.get(downloadUrl('invalid', '12345', '54321', '-sources.jar')) diff --git a/vercel.json b/vercel.json index 545dfd3..005327f 100644 --- a/vercel.json +++ b/vercel.json @@ -22,10 +22,6 @@ "source": "/curse/maven/(.+)", "destination": "/src/app.ts" }, - { - "source": "/download-binary/(.+)%25(2B)(.+)", - "destination": "https://media.forgecdn.net/files/$1%$2$3" - }, { "source": "/test/(.+)", "destination": "/src/app.ts"