反代更多的网站:zlibrary和Wikipedia

还是老朋友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);
  }
}
47 个赞

感谢分享,蹲一个反代claude的方法

4 个赞

反代腾讯阿里百度试试?

4 个赞

谢谢分享,反代一手zlibrary去

2 个赞

有通用的反代方法。请在github上搜索jsproxy和siteproxy。都是通过service worker的方式拦截转发的请求。前者有不少网站不支持。后者功能很全,很多网站都能登录,但是闭源,反正我是不敢用

7 个赞

不错

2 个赞

感谢马老师!

4 个赞

真好,感谢分享

1 个赞

感谢分享,同时感谢CF大善人

2 个赞

感谢分享

2 个赞

马老师!太强了!

2 个赞

您通往知识和文化的门户。 每个人都可以使用。:+1:

1 个赞

成功了!谢谢马老师 :heart:

1 个赞

蹲一个反代github

1 个赞

kkgithub.com

1 个赞

不建议自建github反代。我自建前几天被github发邮件警告了。说我是钓鱼网站,cloudflare也警告我停止这个服务

2 个赞

大佬 学习了明天试试

点赞、收藏

1 个赞

佬哥666

搞七捻三软件开发