Created
August 11, 2021 07:09
-
-
Save NasirNobin/0fa785a06475c124148e534904749c03 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| addEventListener("fetch", (event) => { | |
| event.respondWith( | |
| handleRequest(event.request).catch( | |
| (err) => new Response(err.stack, { status: 500 }) | |
| ) | |
| ); | |
| }); | |
| async function handleRequest(request) { | |
| const url = new URL(request.url); | |
| const pathname = url.pathname; | |
| const redirectUrl = url.searchParams.get('u'); | |
| // intercept http requests | |
| if(redirectUrl && redirectUrl.startsWith("http")){ | |
| const response = await fetch(redirectUrl); | |
| const contentType = response.headers.get('content-type'); | |
| // fuck medium js files | |
| if(redirectUrl.endsWith('.js') && redirectUrl.includes('medium.com')){ | |
| return new Response('', { | |
| headers: { | |
| "content-type": contentType, | |
| }, | |
| }) | |
| } | |
| if(contentType.startsWith('html') || contentType.startsWith('text')){ | |
| const responseText = await response.text(); | |
| const regex = /((http:\/\/|ftp:\/\/|https:\/\/|www\.)([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:\/~+#-]*[\w@?^=%&\/~+#-])?)/gim; | |
| const withUpdatedLinks = responseText.replace(regex, 'https://proxy.nobin.me/?u=$1'); | |
| return new Response(withUpdatedLinks, { | |
| headers: { | |
| "content-type": contentType, | |
| }, | |
| }) | |
| } | |
| return response; | |
| } | |
| return new Response(JSON.stringify({ "error" : "Request must include a `u` parameter." }), { | |
| headers: { "Content-Type": "application/json" }, status: 422, | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment