Skip to content

Instantly share code, notes, and snippets.

@lynzrand
Last active April 24, 2023 13:15
Show Gist options
  • Save lynzrand/49af7711778209e2c95f4d542d2c10ac to your computer and use it in GitHub Desktop.
Save lynzrand/49af7711778209e2c95f4d542d2c10ac to your computer and use it in GitHub Desktop.
const reverseProxyBase = {
acgnx: "share.acgnx.se",
acgrip: "acg.rip",
bangumi: "bangumi.moe",
dmhy: "share.dmhy.org",
mikan: "mikanani.me",
miobt: "miobt.com",
nyaa: "nyaa.si",
nyaa_sukebei: "sukebei.nyaa.si"
}
function noSuchWebsite(name) {
let available = ""
for (let key in reverseProxyBase){
available += key + ": " + reverseProxyBase[key] + "\r\n"
}
if(!name) name = "<empty>"
return new Response(`No website matching name "${name}"\r\n\r\nAvailable:\r\n\r\n${available}`)
}
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}
async function responseReplace(response) {
// REPLACE WITH YOUR WORKER
const thisWebsiteBase = "anime-rev-proxy.rynco.me/"
const { headers } = response;
const contentType = headers.get("content-type") || "";
if (
contentType.includes("application/json")
|| contentType.includes("application/text")
|| contentType.includes("text/html")
|| contentType.includes("application/xml")) {
// do a search and replace for each value
let text = await response.text();
for (let key in reverseProxyBase) {
const val = reverseProxyBase[key];
text = text.replaceAll(val, thisWebsiteBase + key);
}
return new Response(text, response);
} else {
return response;
}
}
export default {
async fetch(request, env) {
try {
const url = new URL(request.url);
const pathname = url.pathname
const paths = pathname.split("/");
if (paths.length <= 1) {
return noSuchWebsite("<empty path>");
}
const reverseProxyWebsite = reverseProxyBase[paths[1]]
if(!reverseProxyWebsite) {
return noSuchWebsite(paths[1])
}
url.pathname = pathname.substring(paths[1].length+1)
url.host = reverseProxyWebsite
var res = await fetch(new Request(url.toString(), request));
return await responseReplace(res);
} catch(e) {
return new Response(e.stack, { status: 500 })
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment