还是老朋友Cloudflare Worker。将反代进行到底,把一些被墙的常用网站反代一下可以给身边的朋友用一用。
zlibrary:
使用下面的代码反代,同时,根据自己情况添加自定义domain,比如 zlib.yourdomain.com
这个zlibrary反代支持登录账户。
export default {
async fetch(request, env) {
const url = new URL(request.url);
const originalHost = url.hostname;
const path = url.pathname + url.search;
if (!path.startsWith("/books-files/")) {
url.hostname = 'zh.singlelogin.re';
}
let response = await fetch(url, request);
let location = response.headers.get('Location');
if (location) {
const locationUrl = new URL(location);
if (!locationUrl.pathname.startsWith("/books-files/")) {
locationUrl.hostname = originalHost;
}
response = new Response(response.body, response);
response.headers.set('Location', locationUrl.toString());
}
let cookieHeaders = response.headers.get('Set-Cookie');
if (cookieHeaders) {
let cookies = cookieHeaders.split(',').map(cookie => cookie.trim());
response = new Response(response.body, response);
response.headers.delete('Set-Cookie');
cookies.forEach(cookie => {
let modifiedCookie = cookie.replace(/(domain=)([^;]+)(;|$)/gi, `$1${originalHost}$3`);
response.headers.append('Set-Cookie', modifiedCookie);
});
}
return response;
}
}
Wikipedia
使用下面的代码反代,同时,根据自己情况添加自定义domain,并添加到对应的Worker路由中,同时还需要在Worker中添加环境变量 CUSTOMED_DOMAIN = mydomain.com
。
比如,我添加了以下自定义域和路由:
域:
wiki.yourdomain.com
*.wiki.yourdomain.com(适配电脑端多语言)
*.m.wiki.yourdomain.com(用于移动端)
路由:
wiki.yourdomain.com/*
*.wiki.yourdomain.com/*
*.m.wiki.yourdomain.com/*
// 在 Cloudflare 中定义该环境变量,例如: CUSTOMED_DOMAIN = "mydomain.com"
export default {
async fetch(request, env) {
const customedDomain = env.CUSTOMED_DOMAIN;
const originalUrl = new URL(request.url);
let targetUrl = new URL(originalUrl.href.replace(originalUrl.host, "www.wikipedia.org"));
const subdomains = originalUrl.hostname.split('.');
if (subdomains[0] !== "wiki") {
if (subdomains[1] === "m") {
targetUrl.hostname = `${subdomains[0]}.m.wikipedia.org`;
} else {
targetUrl.hostname = `${subdomains[0]}.wikipedia.org`;
}
}
const newRequest = new Request(targetUrl, {
method: request.method,
headers: request.headers,
body: request.body,
redirect: 'manual'
});
const response = await fetch(newRequest);
if (response.status === 301 || response.status === 302) {
const location = response.headers.get('Location');
if (location) {
const locationUrl = new URL(location, targetUrl);
if (locationUrl.hostname.endsWith('wikipedia.org')) {
const parts = locationUrl.hostname.split('.');
const lang = parts[0];
const isMobile = parts[1] === "m";
locationUrl.hostname = isMobile ? `${lang}.m.wiki.${customedDomain}` : `${lang}.wiki.${customedDomain}`;
return new Response(null, {
status: 302,
headers: {
'Location': locationUrl.toString()
}
});
}
}
}
return new HTMLRewriter()
.on('a', {
element(element) {
const href = element.getAttribute("href");
if (href) {
const url = new URL(href, targetUrl);
if (url.hostname.endsWith("wikipedia.org")) {
const parts = url.hostname.split('.');
const lang = parts[0];
const isMobile = parts[1] === "m";
url.hostname = isMobile ? `${lang}.m.wiki.${customedDomain}` : `${lang}.wiki.${customedDomain}`;
element.setAttribute("href", url.href);
}
}
}
})
.on('form', {
element(element) {
const action = element.getAttribute("action");
if (action) {
const url = new URL(action, targetUrl);
if (url.hostname.endsWith("wikipedia.org")) {
const parts = url.hostname.split('.');
const lang = parts[0];
const isMobile = parts[1] === "m";
url.hostname = isMobile ? `${lang}.m.wiki.${customedDomain}` : `${lang}.wiki.${customedDomain}`;
element.setAttribute("action", url.href);
}
}
}
})
.transform(response);
}
}